Skip to content
Browse files

Merge branch 'execfile'

  • Loading branch information...
2 parents c7eb002 + 4adf668 commit 87e2f91dfe24f759db1a234054cd6a8700a5a2b3 @takluyver takluyver committed
Showing with 56 additions and 0 deletions.
  1. +8 −0 IPython/core/interactiveshell.py
  2. +27 −0 IPython/core/tests/test_interactiveshell.py
  3. +21 −0 IPython/utils/py3compat.py
View
8 IPython/core/interactiveshell.py
@@ -2257,8 +2257,12 @@ def safe_execfile(self, fname, *where, **kw):
exit_ignore : bool (False)
If True, then silence SystemExit for non-zero status (it is always
silenced for zero status, as it is so common).
+ raise_exceptions : bool (False)
+ If True raise exceptions everywhere. Meant for testing.
+
"""
kw.setdefault('exit_ignore', False)
+ kw.setdefault('raise_exceptions', False)
fname = os.path.abspath(os.path.expanduser(fname))
@@ -2288,9 +2292,13 @@ def safe_execfile(self, fname, *where, **kw):
# 0
# For other exit status, we show the exception unless
# explicitly silenced, but only in short form.
+ if kw['raise_exceptions']:
+ raise
if status.code not in (0, None) and not kw['exit_ignore']:
self.showtraceback(exception_only=True)
except:
+ if kw['raise_exceptions']:
+ raise
self.showtraceback()
def safe_execfile_ipy(self, fname):
View
27 IPython/core/tests/test_interactiveshell.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
"""Tests for the key interactiveshell module.
Historically the main classes in interactiveshell have been under-tested. This
@@ -19,7 +20,11 @@
# Imports
#-----------------------------------------------------------------------------
# stdlib
+import os
+import shutil
+import tempfile
import unittest
+from os.path import join
from StringIO import StringIO
from IPython.testing import decorators as dec
@@ -193,3 +198,25 @@ def test_drop_by_id(self):
assert name not in ip.user_ns_hidden, name
assert ip.user_ns['b'] == 12
ip.reset()
+
+class TestSafeExecfileNonAsciiPath(unittest.TestCase):
+
+ def setUp(self):
+ self.BASETESTDIR = tempfile.mkdtemp()
+ self.TESTDIR = join(self.BASETESTDIR, u"åäö")
+ os.mkdir(self.TESTDIR)
+ with open(join(self.TESTDIR, u"åäötestscript.py"), "w") as sfile:
+ sfile.write("pass\n")
+ self.oldpath = os.getcwdu()
+ os.chdir(self.TESTDIR)
+ self.fname = u"åäötestscript.py"
+
+
+ def tearDown(self):
+ os.chdir(self.oldpath)
+ shutil.rmtree(self.BASETESTDIR)
+
+ def test_1(self):
+ """Test safe_execfile with non-ascii path
+ """
+ _ip.shell.safe_execfile(self.fname, raise_exceptions=True)
View
21 IPython/utils/py3compat.py
@@ -1,5 +1,6 @@
# coding: utf-8
"""Compatibility tricks for Python 3. Mainly to do with unicode."""
+import __builtin__
import functools
import sys
import re
@@ -142,6 +143,7 @@ def MethodType(func, instance):
def doctest_refactor_print(func_or_str):
return func_or_str
+
# Abstract u'abc' syntax:
@_modify_str_or_docstring
def u_format(s):
@@ -149,3 +151,22 @@ def u_format(s):
Accepts a string or a function, so it can be used as a decorator."""
return s.format(u='u')
+
+ if sys.platform == 'win32':
+ def execfile(fname, glob=None, loc=None):
+ loc = loc if (loc is not None) else glob
+ scripttext = __builtin__.open(fname).read()
+ # compile converts unicode filename to str assuming
+ # ascii. Let's do the conversion before calling compile
+ if isinstance(fname, unicode):
+ filename = unicode_to_str(fname)
+ else:
+ filename = fname
+ exec compile(scripttext, filename, 'exec') in glob, loc
+ else:
+ def execfile(fname, *where):
+ if isinstance(fname, unicode):
+ filename = fname.encode(sys.getfilesystemencoding())
+ else:
+ filename = fname
+ __builtin__.execfile(filename, *where)

0 comments on commit 87e2f91

Please sign in to comment.
Something went wrong with that request. Please try again.