jacinle.utils.debug#

Functions

decorate_exception_hook(func)

A decorator to decorate the exception hook when calling a function.

exception_hook([enable])

A context manager to temporarily enable the ipdb exception hook.

hook_exception_ipdb()

Add a hook to ipdb when an exception is raised.

indent_log(string)

Print a log message with the current indent level.

indent_print(*args[, sep, end])

Print a message with the current indent level.

log_function([init_function, verbose, ...])

A decorator to log the function call.

profile([field, top_k])

A context manager to profile the code in the context.

time([name])

A context manager to time the code in the context.

timeout_ipdb(locals_[, timeout])

A context manager that enters ipdb when timeout.

unhook_exception_ipdb()

Remove the hook to ipdb when an exception is raised.

Functions

decorate_exception_hook(func)[source]#

A decorator to decorate the exception hook when calling a function.

Parameters:

func (Callable)

Return type:

Callable

exception_hook(enable=True)[source]#

A context manager to temporarily enable the ipdb exception hook.

Parameters:

enable (bool)

hook_exception_ipdb()[source]#

Add a hook to ipdb when an exception is raised.

indent_log(string)[source]#

Print a log message with the current indent level. The indent level is managed by log_function.

indent_print(*args, sep=' ', end='\n')[source]#

Print a message with the current indent level. The indent level is managed by log_function.

log_function(init_function=None, *, verbose=False, is_generator=False)[source]#

A decorator to log the function call. This is useful to debug the function call stack. The call stack will be formated with indentations.

Parameters:
  • init_function (Callable | None) – the function to be wrapped. If not specified, this function will return a decorator.

  • verbose (bool) – whether to print detailed log. By default, only the function name is printed.

  • is_generator (bool) – whether the function is a generator. If True, the function will be treated as a generator.

Example

@log_function
def foo():
    bar()

@log_function
def bar():
    pass

foo()
Output:
Entering: foo
    Entering: bar
    Exiting: bar
    Return: None
Exiting: foo
Return: None
profile(field='tottime', top_k=10)[source]#

A context manager to profile the code in the context. After profiling is done, the top k functions will be printed.

Parameters:
  • field – the field to sort the profile result. See cProfile.Profile.print_stats for more details.

  • top_k – the number of top functions to print.

Example

with profile() as profiler:
    foo()
print(profiler)  # If you need to print additional information inside the context
time(name=None)[source]#

A context manager to time the code in the context. After timing is done, the time will be printed.

Parameters:

name – the name of the context. If None, the default name 'DEFAULT' will be used.

Example

with time():
    foo()
timeout_ipdb(locals_, timeout=3)[source]#

A context manager that enters ipdb when timeout. This is useful when you want to debug a function that is stuck in a loop.

Example

with timeout_ipdb(locals(), timeout=3):
    while True:
        pass
Parameters:
  • locals – the locals() of the function.

  • timeout (float) – the timeout in seconds.

unhook_exception_ipdb()[source]#

Remove the hook to ipdb when an exception is raised.