##// END OF EJS Templates
Merge branch 'execfile'
Thomas Kluyver -
r5134:87e2f91d merge
parent child Browse files
Show More
@@ -2257,8 +2257,12 b' class InteractiveShell(SingletonConfigurable, Magic):'
2257 2257 exit_ignore : bool (False)
2258 2258 If True, then silence SystemExit for non-zero status (it is always
2259 2259 silenced for zero status, as it is so common).
2260 raise_exceptions : bool (False)
2261 If True raise exceptions everywhere. Meant for testing.
2262
2260 2263 """
2261 2264 kw.setdefault('exit_ignore', False)
2265 kw.setdefault('raise_exceptions', False)
2262 2266
2263 2267 fname = os.path.abspath(os.path.expanduser(fname))
2264 2268
@@ -2288,9 +2292,13 b' class InteractiveShell(SingletonConfigurable, Magic):'
2288 2292 # 0
2289 2293 # For other exit status, we show the exception unless
2290 2294 # explicitly silenced, but only in short form.
2295 if kw['raise_exceptions']:
2296 raise
2291 2297 if status.code not in (0, None) and not kw['exit_ignore']:
2292 2298 self.showtraceback(exception_only=True)
2293 2299 except:
2300 if kw['raise_exceptions']:
2301 raise
2294 2302 self.showtraceback()
2295 2303
2296 2304 def safe_execfile_ipy(self, fname):
@@ -1,3 +1,4 b''
1 # -*- coding: utf-8 -*-
1 2 """Tests for the key interactiveshell module.
2 3
3 4 Historically the main classes in interactiveshell have been under-tested. This
@@ -19,7 +20,11 b' Authors'
19 20 # Imports
20 21 #-----------------------------------------------------------------------------
21 22 # stdlib
23 import os
24 import shutil
25 import tempfile
22 26 import unittest
27 from os.path import join
23 28 from StringIO import StringIO
24 29
25 30 from IPython.testing import decorators as dec
@@ -193,3 +198,25 b' class InteractiveShellTestCase(unittest.TestCase):'
193 198 assert name not in ip.user_ns_hidden, name
194 199 assert ip.user_ns['b'] == 12
195 200 ip.reset()
201
202 class TestSafeExecfileNonAsciiPath(unittest.TestCase):
203
204 def setUp(self):
205 self.BASETESTDIR = tempfile.mkdtemp()
206 self.TESTDIR = join(self.BASETESTDIR, u"åäö")
207 os.mkdir(self.TESTDIR)
208 with open(join(self.TESTDIR, u"åäötestscript.py"), "w") as sfile:
209 sfile.write("pass\n")
210 self.oldpath = os.getcwdu()
211 os.chdir(self.TESTDIR)
212 self.fname = u"åäötestscript.py"
213
214
215 def tearDown(self):
216 os.chdir(self.oldpath)
217 shutil.rmtree(self.BASETESTDIR)
218
219 def test_1(self):
220 """Test safe_execfile with non-ascii path
221 """
222 _ip.shell.safe_execfile(self.fname, raise_exceptions=True)
@@ -1,5 +1,6 b''
1 1 # coding: utf-8
2 2 """Compatibility tricks for Python 3. Mainly to do with unicode."""
3 import __builtin__
3 4 import functools
4 5 import sys
5 6 import re
@@ -142,6 +143,7 b' else:'
142 143 def doctest_refactor_print(func_or_str):
143 144 return func_or_str
144 145
146
145 147 # Abstract u'abc' syntax:
146 148 @_modify_str_or_docstring
147 149 def u_format(s):
@@ -149,3 +151,22 b' else:'
149 151
150 152 Accepts a string or a function, so it can be used as a decorator."""
151 153 return s.format(u='u')
154
155 if sys.platform == 'win32':
156 def execfile(fname, glob=None, loc=None):
157 loc = loc if (loc is not None) else glob
158 scripttext = __builtin__.open(fname).read()
159 # compile converts unicode filename to str assuming
160 # ascii. Let's do the conversion before calling compile
161 if isinstance(fname, unicode):
162 filename = unicode_to_str(fname)
163 else:
164 filename = fname
165 exec compile(scripttext, filename, 'exec') in glob, loc
166 else:
167 def execfile(fname, *where):
168 if isinstance(fname, unicode):
169 filename = fname.encode(sys.getfilesystemencoding())
170 else:
171 filename = fname
172 __builtin__.execfile(filename, *where)
General Comments 0
You need to be logged in to leave comments. Login now