From 7a9f6c35c91f92b05c3e84a5a616d356a4cacbee 2013-01-10 14:05:00 From: Thomas Kluyver Date: 2013-01-10 14:05:00 Subject: [PATCH] Merge pull request #2766 from bfroehle/edit_file_attribute Define __file__ when we %edit a real file. --- diff --git a/IPython/core/magics/code.py b/IPython/core/magics/code.py index 30b4b96..e8f05aa 100644 --- a/IPython/core/magics/code.py +++ b/IPython/core/magics/code.py @@ -29,6 +29,7 @@ from IPython.core.oinspect import find_file, find_source_lines from IPython.testing.skipdoctest import skip_doctest from IPython.utils import openpy from IPython.utils import py3compat +from IPython.utils.contexts import preserve_keys from IPython.utils.io import file_read from IPython.utils.path import get_py_filename, unquote_filename from IPython.utils.warn import warn @@ -536,12 +537,15 @@ class CodeMagics(Magics): print else: print 'done. Executing edited code...' - if 'r' in opts: # Untranslated IPython code - self.shell.run_cell(file_read(filename), - store_history=False) - else: - self.shell.safe_execfile(filename, self.shell.user_ns, - self.shell.user_ns) + with preserve_keys(self.shell.user_ns, '__file__'): + if not is_temp: + self.shell.user_ns['__file__'] = filename + if 'r' in opts: # Untranslated IPython code + self.shell.run_cell(file_read(filename), + store_history=False) + else: + self.shell.safe_execfile(filename, self.shell.user_ns, + self.shell.user_ns) if is_temp: try: