Skip to content

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: Info

original

The original exception being wrapped

TYPE: Exception

*args, **kwargs

Additional arguments passed to the parent Exception class

original instance-attribute

original = original

infos instance-attribute

infos = []

set_info

set_info(info: Info)

Updates the tracer information and recalculates line offsets.

PARAMETER DESCRIPTION
info

New tracer information to use

TYPE: Info

__str__

__str__()

Generates a formatted traceback string with proper context.

RETURNS DESCRIPTION

A string containing the formatted traceback with source code context

indent

indent(source: List[str], indent: int = 1)

Indents each line in the source list by a specified number of indentation levels.

PARAMETER DESCRIPTION
source

List of strings to indent

TYPE: List[str]

indent

Number of indentation levels to apply (default: 1)

TYPE: int DEFAULT: 1

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

suppress_all_output

suppress_all_output()

get_dependencies

get_dependencies(fn: Callable)

Extracts global dependencies used by a function.

PARAMETER DESCRIPTION
fn

The function to analyze for dependencies

TYPE: Callable

RETURNS DESCRIPTION

Dictionary mapping names to their corresponding global objects used by the function

wrap_exception

wrap_exception(exception: Exception, info: Info)

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: Exception

info

Tracer information containing context about where the exception occurred

TYPE: Info

RETURNS DESCRIPTION

A wrapped exception with enhanced traceback information

get_non_nnsight_frame

get_non_nnsight_frame() -> FrameType

push_variables

push_variables(frame: FrameType, variables: Dict)