Skip to content

Commit

Permalink
Merge pull request #2432 from bfroehle/revert_1831
Browse files Browse the repository at this point in the history
Revert #1831, the `__file__` injection in safe_execfile / safe_execfile_ipy.

This reverts commit 2717feb, reversing changes made to ea4f608.

Pull request #1831 (fix #1814 set __file__ when running .ipy files) has been the source of a lot of grief:

#2279: Setting __file__ to None breaks Mayavi import
#2429: Using warnings.warn() results in TypeError
In general the patch was inappropriate because it:
1. Fails to properly restore the context, by setting __file__ to None rather than deleting it.
2. Sets __file__ in the wrong dictionary (self.user_ns rather than where[0]).
  • Loading branch information
Carreau committed Sep 30, 2012
2 parents 0ed37f8 + 41feaae commit d3d37a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
10 changes: 0 additions & 10 deletions IPython/core/interactiveshell.py
Expand Up @@ -2455,9 +2455,6 @@ def safe_execfile(self, fname, *where, **kw):
dname = os.path.dirname(fname)

with prepended_to_syspath(dname):
# Ensure that __file__ is always defined to match Python behavior
save_fname = self.user_ns.get('__file__',None)
self.user_ns['__file__'] = fname
try:
py3compat.execfile(fname,*where)
except SystemExit as status:
Expand All @@ -2478,8 +2475,6 @@ def safe_execfile(self, fname, *where, **kw):
if kw['raise_exceptions']:
raise
self.showtraceback()
finally:
self.user_ns['__file__'] = save_fname

def safe_execfile_ipy(self, fname):
"""Like safe_execfile, but for .ipy files with IPython syntax.
Expand All @@ -2506,9 +2501,6 @@ def safe_execfile_ipy(self, fname):
dname = os.path.dirname(fname)

with prepended_to_syspath(dname):
# Ensure that __file__ is always defined to match Python behavior
save_fname = self.user_ns.get('__file__',None)
self.user_ns['__file__'] = fname
try:
with open(fname) as thefile:
# self.run_cell currently captures all exceptions
Expand All @@ -2519,8 +2511,6 @@ def safe_execfile_ipy(self, fname):
except:
self.showtraceback()
warn('Unknown failure executing file: <%s>' % fname)
finally:
self.user_ns['__file__'] = save_fname

def safe_run_module(self, mod_name, where):
"""A safe version of runpy.run_module().
Expand Down
12 changes: 10 additions & 2 deletions IPython/core/shellapp.py
Expand Up @@ -277,12 +277,20 @@ def _exec_file(self, fname):
sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
try:
if os.path.isfile(full_filename):
self.log.info("Running file in user namespace: %s" % full_filename)
if full_filename.endswith('.ipy'):
self.log.info("Running file in user namespace: %s" %
full_filename)
self.shell.safe_execfile_ipy(full_filename)
else:
# default to python, even without extension
self.shell.safe_execfile(full_filename, self.shell.user_ns)
self.log.info("Running file in user namespace: %s" %
full_filename)
# Ensure that __file__ is always defined to match Python behavior
self.shell.user_ns['__file__'] = fname
try:
self.shell.safe_execfile(full_filename, self.shell.user_ns)
finally:
del self.shell.user_ns['__file__']
finally:
sys.argv = save_argv

Expand Down

0 comments on commit d3d37a3

Please sign in to comment.