From f12b9485fc16db7afe1d47489adb8cea1b7efa8e 2020-07-13 15:21:52 From: Quentin Peter Date: 2020-07-13 15:21:52 Subject: [PATCH] Move Extraction of local scope to a method When calling ipython magic from pdb, the locals need to be set from the pdb locals, which is different from the frame locals. Moving this call to a method enables a subclass to change the behaviour of `get_local_scope` to make this possible. This would enable using `%timeit` while debugging in Spyder for example --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 0bbc18a..9171f24 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2322,11 +2322,21 @@ class InteractiveShell(SingletonConfigurable): kwargs = {} # Grab local namespace if we need it: if getattr(fn, "needs_local_scope", False): - kwargs['local_ns'] = sys._getframe(stack_depth).f_locals + kwargs['local_ns'] = self.get_local_scope(stack_depth) with self.builtin_trap: result = fn(*args, **kwargs) return result + def get_local_scope(self, stack_depth): + """Get local scope at given stack depth. + + Parameters + ---------- + stack_depth : int + Depth relative to calling frame + """ + return sys._getframe(stack_depth + 1).f_locals + def run_cell_magic(self, magic_name, line, cell): """Execute the given cell magic.