cardinal_pythonlib.debugging


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Functions for debugging.

cardinal_pythonlib.debugging.cause_segfault() None[source]

This function will induce a segmentation fault and CRASH the application. Method as per https://docs.python.org/3/library/faulthandler.html

cardinal_pythonlib.debugging.debug_object(obj, log_level: int = 10) None[source]

Sends details about a Python to the log, specifically its repr() representation, and all of its attributes with their name, value, and type.

Parameters:
  • obj – object to debug

  • log_level – log level to use; default is logging.DEBUG

cardinal_pythonlib.debugging.get_caller_name(back: int = 0) str[source]

Return details about the CALLER OF THE CALLER (plus n calls further back) of this function.

So, if your function calls get_caller_name(), it will return the name of the function that called your function! (Or back calls further back.)

Example:

from cardinal_pythonlib.debugging import get_caller_name

def who_am_i():
    return get_caller_name()

class MyClass(object):
    def classfunc(self):
        print("I am: " + who_am_i())
        print("I was called by: " + get_caller_name())
        print("That was called by: " + get_caller_name(back=1))

def f2():
    x = MyClass()
    x.classfunc()

def f1():
    f2()

f1()

will produce:

I am: MyClass.classfunc
I was called by: f2
That was called by: f1
cardinal_pythonlib.debugging.get_caller_stack_info(start_back: int = 1) List[str][source]

Retrieves a textual representation of the call stack.

Parameters:

start_back – number of calls back in the frame stack (starting from the frame stack as seen by get_caller_stack_info()) to begin with

Returns:

list of descriptions

Example:

from cardinal_pythonlib.debugging import get_caller_stack_info

def who_am_i():
    return get_caller_name()

class MyClass(object):
    def classfunc(self):
        print("Stack info:\n" + "\n".join(get_caller_stack_info()))

def f2():
    x = MyClass()
    x.classfunc()

def f1():
    f2()

f1()

if called from the Python prompt will produce:

Stack info:
<module>()
... defined at <stdin>:1
... line 1 calls next in stack; code is:

f1()
... defined at <stdin>:1
... line 2 calls next in stack; code is:

f2()
... defined at <stdin>:1
... line 3 calls next in stack; code is:

classfunc(self=<__main__.MyClass object at 0x7f86a009c6d8>)
... defined at <stdin>:2
... line 3 calls next in stack; code is:

and if called from a Python file will produce:

Stack info:
<module>()
... defined at /home/rudolf/tmp/stack.py:1
... line 17 calls next in stack; code is:
f1()

f1()
... defined at /home/rudolf/tmp/stack.py:14
... line 15 calls next in stack; code is:
    f2()

f2()
... defined at /home/rudolf/tmp/stack.py:10
... line 12 calls next in stack; code is:
    x.classfunc()

classfunc(self=<__main__.MyClass object at 0x7fd7a731f358>)
... defined at /home/rudolf/tmp/stack.py:7
... line 8 calls next in stack; code is:
        print("Stack info:\n" + "\n".join(get_caller_stack_info()))
cardinal_pythonlib.debugging.get_class_name_from_frame(fr: frame) str | None[source]

A frame contains information about a specific call in the Python call stack; see https://docs.python.org/3/library/inspect.html.

If the call was to a member function of a class, this function attempts to read the class’s name. It returns None otherwise.

cardinal_pythonlib.debugging.pdb_run(func: Callable, *args: Any, **kwargs: Any) Any[source]

Calls func(*args, **kwargs); if it raises an exception, break into the pdb debugger.