##// END OF EJS Templates
remove unnecessarily specific extract_module_locals_above
MinRK -
Show More
@@ -1,70 +1,70 b''
1 1 # encoding: utf-8
2 2 """
3 3 IPython: tools for interactive and parallel computing in Python.
4 4
5 5 http://ipython.org
6 6 """
7 7 #-----------------------------------------------------------------------------
8 8 # Copyright (c) 2008-2011, IPython Development Team.
9 9 # Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
10 10 # Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
11 11 # Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
12 12 #
13 13 # Distributed under the terms of the Modified BSD License.
14 14 #
15 15 # The full license is in the file COPYING.txt, distributed with this software.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21 from __future__ import absolute_import
22 22
23 23 import os
24 24 import sys
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Setup everything
28 28 #-----------------------------------------------------------------------------
29 29
30 30 # Don't forget to also update setup.py when this changes!
31 31 if sys.version[0:3] < '2.6':
32 32 raise ImportError('Python Version 2.6 or above is required for IPython.')
33 33
34 34 # Make it easy to import extensions - they are always directly on pythonpath.
35 35 # Therefore, non-IPython modules can be added to extensions directory.
36 36 # This should probably be in ipapp.py.
37 37 sys.path.append(os.path.join(os.path.dirname(__file__), "extensions"))
38 38
39 39 #-----------------------------------------------------------------------------
40 40 # Setup the top level names
41 41 #-----------------------------------------------------------------------------
42 42
43 43 from .config.loader import Config
44 44 from .core import release
45 45 from .core.application import Application
46 46 from .frontend.terminal.embed import embed
47 47
48 48 from .core.error import TryNext
49 49 from .core.interactiveshell import InteractiveShell
50 50 from .testing import test
51 51 from .utils.sysinfo import sys_info
52 from .utils.frame import extract_module_locals_above
52 from .utils.frame import extract_module_locals
53 53
54 54 # Release data
55 55 __author__ = ''
56 56 for author, email in release.authors.itervalues():
57 57 __author__ += author + ' <' + email + '>\n'
58 58 __license__ = release.license
59 59 __version__ = release.version
60 60
61 61 def embed_kernel(module=None, local_ns=None):
62 62 """Call this to embed an IPython kernel at the current point in your program. """
63 (caller_module, caller_locals) = extract_module_locals_above()
63 (caller_module, caller_locals) = extract_module_locals(1)
64 64 if module is None:
65 65 module = caller_module
66 66 if local_ns is None:
67 67 local_ns = caller_locals
68 68 # Only import .zmq when we really need it
69 69 from .zmq.ipkernel import embed_kernel as real_embed_kernel
70 70 real_embed_kernel(module, local_ns)
@@ -1,100 +1,94 b''
1 1 # encoding: utf-8
2 2 """
3 3 Utilities for working with stack frames.
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 import sys
18 18 from IPython.utils import py3compat
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Code
22 22 #-----------------------------------------------------------------------------
23 23
24 24 @py3compat.doctest_refactor_print
25 25 def extract_vars(*names,**kw):
26 26 """Extract a set of variables by name from another frame.
27 27
28 28 :Parameters:
29 29 - `*names`: strings
30 30 One or more variable names which will be extracted from the caller's
31 31 frame.
32 32
33 33 :Keywords:
34 34 - `depth`: integer (0)
35 35 How many frames in the stack to walk when looking for your variables.
36 36
37 37
38 38 Examples:
39 39
40 40 In [2]: def func(x):
41 41 ...: y = 1
42 42 ...: print extract_vars('x','y')
43 43 ...:
44 44
45 45 In [3]: func('hello')
46 46 {'y': 1, 'x': 'hello'}
47 47 """
48 48
49 49 depth = kw.get('depth',0)
50 50
51 51 callerNS = sys._getframe(depth+1).f_locals
52 52 return dict((k,callerNS[k]) for k in names)
53 53
54 54
55 55 def extract_vars_above(*names):
56 56 """Extract a set of variables by name from another frame.
57 57
58 58 Similar to extractVars(), but with a specified depth of 1, so that names
59 59 are exctracted exactly from above the caller.
60 60
61 61 This is simply a convenience function so that the very common case (for us)
62 62 of skipping exactly 1 frame doesn't have to construct a special dict for
63 63 keyword passing."""
64 64
65 65 callerNS = sys._getframe(2).f_locals
66 66 return dict((k,callerNS[k]) for k in names)
67 67
68 68
69 69 def debugx(expr,pre_msg=''):
70 70 """Print the value of an expression from the caller's frame.
71 71
72 72 Takes an expression, evaluates it in the caller's frame and prints both
73 73 the given expression and the resulting value (as well as a debug mark
74 74 indicating the name of the calling function. The input must be of a form
75 75 suitable for eval().
76 76
77 77 An optional message can be passed, which will be prepended to the printed
78 78 expr->value pair."""
79 79
80 80 cf = sys._getframe(1)
81 81 print '[DBG:%s] %s%s -> %r' % (cf.f_code.co_name,pre_msg,expr,
82 82 eval(expr,cf.f_globals,cf.f_locals))
83 83
84 84
85 85 # deactivate it by uncommenting the following line, which makes it a no-op
86 86 #def debugx(expr,pre_msg=''): pass
87 87
88 88 def extract_module_locals(depth=0):
89 89 """Returns (module, locals) of the funciton `depth` frames away from the caller"""
90 90 f = sys._getframe(depth + 1)
91 91 global_ns = f.f_globals
92 92 module = sys.modules[global_ns['__name__']]
93 93 return (module, f.f_locals)
94 94
95 def extract_module_locals_above():
96 """Returns (module, locals) of the funciton calling the caller.
97 Like extract_module_locals() with a specified depth of 1."""
98 # we're one frame away from the target, the function we call would be two frames away
99 return extract_module_locals(1 + 1)
100
General Comments 0
You need to be logged in to leave comments. Login now