misc.frame_parameters(): Add function
frame_parameters() returns a dictionary of name -> value pairs with the parameters of the current call frames. They can be limited by specifying exclude_names and exclude_values which do the obvious thing. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
581bb3531e
commit
89b4e23bc1
1 changed files with 28 additions and 0 deletions
|
|
@ -151,6 +151,34 @@ def load_function(module_path: str, name: str) -> Any: # export
|
|||
mod = importlib.import_module(module_path)
|
||||
return getattr(mod, name)
|
||||
|
||||
def frame_parameters( # export
|
||||
exclude_names: Iterable[str] = ['self', 'cls'],
|
||||
exclude_values: Iterable[Any] = [None],
|
||||
) -> dict[str, Any]:
|
||||
current_frame = inspect.currentframe()
|
||||
# E: Item "None" of "FrameType | None" has no attribute "f_back" [union-attr]
|
||||
assert current_frame is not None
|
||||
frame = current_frame.f_back
|
||||
# E: Item "None" of "FrameType | None" has no attribute "f_code" [union-attr]
|
||||
assert frame is not None
|
||||
code = frame.f_code
|
||||
names = list(code.co_varnames[:code.co_argcount + code.co_kwonlyargcount])
|
||||
index = code.co_argcount + code.co_kwonlyargcount
|
||||
if code.co_flags & inspect.CO_VARARGS:
|
||||
names.append(code.co_varnames[index])
|
||||
index += 1
|
||||
if code.co_flags & inspect.CO_VARKEYWORDS:
|
||||
names.append(code.co_varnames[index])
|
||||
ret: dict[str, Any] = {}
|
||||
for name in names:
|
||||
val = frame.f_locals[name]
|
||||
if val in exclude_values:
|
||||
continue
|
||||
if name in exclude_names:
|
||||
continue
|
||||
ret[name] = val
|
||||
return ret
|
||||
|
||||
def commit_tmpfile(tmp: str, path: str) -> None: # export
|
||||
caller = log.get_caller_pos()
|
||||
if os.path.isfile(path) and filecmp.cmp(tmp, path):
|
||||
|
|
|
|||
Loading…
Reference in a new issue