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