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