Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/IPython/utils/frame.py: 35%
17 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1# encoding: utf-8
2"""
3Utilities for working with stack frames.
4"""
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#-----------------------------------------------------------------------------
13#-----------------------------------------------------------------------------
14# Imports
15#-----------------------------------------------------------------------------
17import sys
19#-----------------------------------------------------------------------------
20# Code
21#-----------------------------------------------------------------------------
23def extract_vars(*names,**kw):
24 """Extract a set of variables by name from another frame.
26 Parameters
27 ----------
28 *names : str
29 One or more variable names which will be extracted from the caller's
30 frame.
31 **kw : integer, optional
32 How many frames in the stack to walk when looking for your variables.
33 The default is 0, which will use the frame where the call was made.
35 Examples
36 --------
37 ::
39 In [2]: def func(x):
40 ...: y = 1
41 ...: print(sorted(extract_vars('x','y').items()))
42 ...:
44 In [3]: func('hello')
45 [('x', 'hello'), ('y', 1)]
46 """
48 depth = kw.get('depth',0)
50 callerNS = sys._getframe(depth+1).f_locals
51 return dict((k,callerNS[k]) for k in names)
54def extract_vars_above(*names):
55 """Extract a set of variables by name from another frame.
57 Similar to extractVars(), but with a specified depth of 1, so that names
58 are extracted exactly from above the caller.
60 This is simply a convenience function so that the very common case (for us)
61 of skipping exactly 1 frame doesn't have to construct a special dict for
62 keyword passing."""
64 callerNS = sys._getframe(2).f_locals
65 return dict((k,callerNS[k]) for k in names)
68def debugx(expr,pre_msg=''):
69 """Print the value of an expression from the caller's frame.
71 Takes an expression, evaluates it in the caller's frame and prints both
72 the given expression and the resulting value (as well as a debug mark
73 indicating the name of the calling function. The input must be of a form
74 suitable for eval().
76 An optional message can be passed, which will be prepended to the printed
77 expr->value pair."""
79 cf = sys._getframe(1)
80 print('[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
81 eval(expr,cf.f_globals,cf.f_locals)))
84# deactivate it by uncommenting the following line, which makes it a no-op
85#def debugx(expr,pre_msg=''): pass
87def extract_module_locals(depth=0):
88 """Returns (module, locals) of the function `depth` frames away from the caller"""
89 f = sys._getframe(depth + 1)
90 global_ns = f.f_globals
91 module = sys.modules[global_ns['__name__']]
92 return (module, f.f_locals)