padl.dumptools.inspector
Utilities for inspecting frames.
-
class padl.dumptools.inspector.CallInfo(origin: Literal[
'nextmodule','here'] = 'nextmodule', drop_n: int = 0, ignore_scope: bool = False) Information about the calling context.
- Contains the following information:
the function from which that call was made
the scope (see
symfinder.Scope
)
- Parameters
origin – Where to look for the call, can be - “nextmodule”: use the first frame not in the module the object was created in - “here”: use the frame the object was created in
drop_n – Drop n levels from the calling scope.
ignore_scope – Don’t try to determine the scope (use the toplevel scope instead).
- property module
The calling module.
- padl.dumptools.inspector.caller_frame() frame
Get the callers frame.
- padl.dumptools.inspector.caller_module() module
Get the first module of the caller.
- padl.dumptools.inspector.get_segment_from_frame(caller_frame: frame, segment_type, return_locs=False) str
Get a segment of a given type from a frame.
NOTE: All this is rather hacky and should be changed as soon as python 3.11 becomes widely available as then it will be possible to get column information from frames (see inline comments).
segment_type can be ‘call’, ‘attribute’, ‘getitem’.
- padl.dumptools.inspector.get_statement(source: str, lineno: int)
Get complete (potentially multi-line) statement at line lineno out of source.
- Returns
A tuple of statement and offset. The offset is a tuple of row offset and col offset. It can be used to determine the location of the statement within the source.
- padl.dumptools.inspector.get_surrounding_block(source: str, lineno: int)
Get the code block surrounding the line at lineno in source.
The code block surrounding a line is the largest block of lines with the same or larger indentation as the line itself.
The result will be unindented.
Raises a ValueError if the line at lineno is empty.
- Parameters
source – The source to extract the block from.
lineno – Number of the line for extracting the block.
- Returns
A tuple containing the block itself and the line number of the target line within the block and the number of spaces removed from the front.
- padl.dumptools.inspector.non_init_caller_frameinfo() inspect.FrameInfo
Get the FrameInfo for the first outer frame that is not of an “__init__” method.
- padl.dumptools.inspector.outer_caller_frameinfo(module_name: str) inspect.FrameInfo
Get the first level of the stack before entering the module with name module_name.
- padl.dumptools.inspector.trace_this(tracefunc: Callable, frame: Optional[frame] = None, *args, **kwargs)
Call in a function body to trace the rest of the function execution with function tracefunc. tracefunc must match the requirements for the argument of sys.settrace (in the documentation of which more details can be found).
Example:
>>> def tracefunc(frame, event, arg): ... if 'event' == 'return': ... print('returning', arg)
>>> def myfunction(): ... [...] ... _trace_this(tracefunc) ... return 123
- Parameters
tracefunc – Trace function (see documentation of sys.settrace for details).
frame – The frame to trace (defaults to the caller’s frame).