diff --git a/IPython/__init__.py b/IPython/__init__.py index 6bf231b..df54346 100644 --- a/IPython/__init__.py +++ b/IPython/__init__.py @@ -49,7 +49,7 @@ from .core.error import TryNext from .core.interactiveshell import InteractiveShell from .testing import test from .utils.sysinfo import sys_info -from .utils.frame import caller_module_and_locals +from .utils.frame import extract_module_locals_above # Release data __author__ = '' @@ -60,7 +60,7 @@ __version__ = release.version def embed_kernel(module=None, local_ns=None): """Call this to embed an IPython kernel at the current point in your program. """ - (caller_module, caller_locals) = caller_module_and_locals() + (caller_module, caller_locals) = extract_module_locals_above() if module is None: module = caller_module if local_ns is None: diff --git a/IPython/utils/frame.py b/IPython/utils/frame.py index 3b19072..cd3563a 100644 --- a/IPython/utils/frame.py +++ b/IPython/utils/frame.py @@ -85,10 +85,16 @@ def debugx(expr,pre_msg=''): # deactivate it by uncommenting the following line, which makes it a no-op #def debugx(expr,pre_msg=''): pass -def caller_module_and_locals(): - """Returns (module, locals) of the caller""" - caller = sys._getframe(2) - global_ns = caller.f_globals +def extract_module_locals(depth=0): + """Returns (module, locals) of the funciton `depth` frames away from the caller""" + f = sys._getframe(depth + 1) + global_ns = f.f_globals module = sys.modules[global_ns['__name__']] - return (module, caller.f_locals) + return (module, f.f_locals) + +def extract_module_locals_above(): + """Returns (module, locals) of the funciton calling the caller. + Like extract_module_locals() with a specified depth of 1.""" + # we're one frame away from the target, the function we call would be two frames away + return extract_module_locals(1 + 1)