| 1 |
# encoding: utf-8 |
| 2 |
""" |
| 3 |
Utilities for working with stack frames. |
| 4 |
""" |
| 5 |
|
| 6 |
#----------------------------------------------------------------------------- |
| 7 |
# Copyright (C) 2008-2011 The IPython Development Team |
| 8 |
# |
| 9 |
# Distributed under the terms of the BSD License. The full license is in |
| 10 |
# the file COPYING, distributed as part of this software. |
| 11 |
#----------------------------------------------------------------------------- |
| 12 |
|
| 13 |
#----------------------------------------------------------------------------- |
| 14 |
# Imports |
| 15 |
#----------------------------------------------------------------------------- |
| 16 |
|
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
import sys |
| 20 |
from types import ModuleType |
| 21 |
from typing import Any |
| 22 |
|
| 23 |
#----------------------------------------------------------------------------- |
| 24 |
# Code |
| 25 |
#----------------------------------------------------------------------------- |
| 26 |
|
| 27 |
def extract_vars(*names: str, **kw: Any) -> dict[str, Any]: |
| 28 |
"""Extract a set of variables by name from another frame. |
| 29 |
|
| 30 |
Parameters |
| 31 |
---------- |
| 32 |
*names : str |
| 33 |
One or more variable names which will be extracted from the caller's |
| 34 |
frame. |
| 35 |
**kw : integer, optional |
| 36 |
How many frames in the stack to walk when looking for your variables. |
| 37 |
The default is 0, which will use the frame where the call was made. |
| 38 |
|
| 39 |
Examples |
| 40 |
-------- |
| 41 |
:: |
| 42 |
|
| 43 |
In [2]: def func(x): |
| 44 |
...: y = 1 |
| 45 |
...: print(sorted(extract_vars('x','y').items())) |
| 46 |
...: |
| 47 |
|
| 48 |
In [3]: func('hello') |
| 49 |
[('x', 'hello'), ('y', 1)] |
| 50 |
""" |
| 51 |
|
| 52 |
depth = kw.get('depth',0) |
| 53 |
|
| 54 |
callerNS = sys._getframe(depth+1).f_locals |
| 55 |
return dict((k,callerNS[k]) for k in names) |
| 56 |
|
| 57 |
|
| 58 |
def extract_vars_above(*names: str) -> dict[str, Any]: |
| 59 |
"""Extract a set of variables by name from another frame. |
| 60 |
|
| 61 |
Similar to extractVars(), but with a specified depth of 1, so that names |
| 62 |
are extracted exactly from above the caller. |
| 63 |
|
| 64 |
This is simply a convenience function so that the very common case (for us) |
| 65 |
of skipping exactly 1 frame doesn't have to construct a special dict for |
| 66 |
keyword passing.""" |
| 67 |
|
| 68 |
callerNS = sys._getframe(2).f_locals |
| 69 |
return dict((k,callerNS[k]) for k in names) |
| 70 |
|
| 71 |
|
| 72 |
def debugx(expr: str, pre_msg: str = "") -> None: |
| 73 |
"""Print the value of an expression from the caller's frame. |
| 74 |
|
| 75 |
Takes an expression, evaluates it in the caller's frame and prints both |
| 76 |
the given expression and the resulting value (as well as a debug mark |
| 77 |
indicating the name of the calling function. The input must be of a form |
| 78 |
suitable for eval(). |
| 79 |
|
| 80 |
An optional message can be passed, which will be prepended to the printed |
| 81 |
expr->value pair.""" |
| 82 |
|
| 83 |
cf = sys._getframe(1) |
| 84 |
print('[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr, |
| 85 |
eval(expr,cf.f_globals,cf.f_locals))) |
| 86 |
|
| 87 |
|
| 88 |
# deactivate it by uncommenting the following line, which makes it a no-op |
| 89 |
#def debugx(expr,pre_msg=''): pass |
| 90 |
|
| 91 |
|
| 92 |
def extract_module_locals(depth: int = 0) -> tuple[ModuleType, dict[str, Any]]: |
| 93 |
"""Returns (module, locals) of the function `depth` frames away from the caller""" |
| 94 |
f = sys._getframe(depth + 1) |
| 95 |
global_ns = f.f_globals |
| 96 |
module = sys.modules[global_ns['__name__']] |
| 97 |
return (module, f.f_locals) |