util¶
util
¶
ExceptionWrapper
¶
ExceptionWrapper(info: Info, original: Exception, *args, **kwargs)
Bases: Exception
Wrapper for exceptions that provides additional details for tracer created code.
This class helps provide better error messages by including source code context and proper line numbers from the original code being traced.
| PARAMETER | DESCRIPTION |
|---|---|
info
|
Tracer information containing context about where the exception occurred
TYPE:
|
original
|
The original exception being wrapped
TYPE:
|
*args, **kwargs
|
Additional arguments passed to the parent Exception class
|
set_info
¶
set_info(info: Info)
Updates the tracer information and recalculates line offsets.
| PARAMETER | DESCRIPTION |
|---|---|
info
|
New tracer information to use
TYPE:
|
format_traceback
¶
Format the traceback as a string.
| PARAMETER | DESCRIPTION |
|---|---|
outer_tb
|
Optional outer traceback to include user frames from
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Formatted traceback string |
print_exception
¶
Print the formatted traceback.
| PARAMETER | DESCRIPTION |
|---|---|
file
|
Output file (default: sys.stderr)
DEFAULT:
|
outer_tb
|
Optional outer traceback to include user frames from
DEFAULT:
|
indent
¶
Indents each line in the source list by a specified number of indentation levels.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
List of strings to indent
TYPE:
|
indent
|
Number of indentation levels to apply (default: 1)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
List of indented strings |
try_catch
¶
try_catch(source: List[str], exception_source: List[str] = ['raise\n'], else_source: List[str] = ['pass\n'], finally_source: List[str] = ['pass\n'])
Wraps source code in a try-except-else-finally block.
Args:
source: The code to be wrapped in the try block
exception_source: Code for the except block (default: ["raise
"]) else_source: Code for the else block (default: ["pass "]) finally_source: Code for the finally block (default: ["pass "])
Returns:
List of strings representing the complete try-catch block, properly indented
get_dependencies
¶
Extracts global dependencies used by a function.
| PARAMETER | DESCRIPTION |
|---|---|
fn
|
The function to analyze for dependencies
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Dictionary mapping names to their corresponding global objects used by the function |
wrap_exception
¶
wrap_exception(exception: Exception, info: Info = None)
Wraps an exception with additional context from the tracer.
This function either updates an existing ExceptionWrapper or creates a new dynamically-typed exception class that inherits from both the original exception type and ExceptionWrapper.
| PARAMETER | DESCRIPTION |
|---|---|
exception
|
The exception to wrap
TYPE:
|
info
|
Tracer information containing context about where the exception occurred
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
A wrapped exception with enhanced traceback information |
get_entered_frame
¶
Return the frame that executed the user's with statement.
Designed to be called from within a __enter__ implementation.
Starts from the caller (the immediate __enter__ frame) and
walks past any chain of __enter__ frames — both subclasses
calling super().__enter__() and user-defined context-manager
wrappers whose __enter__ opens the inner with block. The
first non-__enter__ frame above is the user's actual frame.