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