From 1b786a02a766287131bcd54051143c83ba5d20d7 2011-10-18 19:40:27 From: Jörgen Stenarson Date: 2011-10-18 19:40:27 Subject: [PATCH] Fix for 2.x execfile that can not handle non-ascii filenames. Special case fix for windows where we can not in general use sys.getfilesystemencoding() to get a str that can be used to access all files. --- diff --git a/IPython/utils/py3compat.py b/IPython/utils/py3compat.py index 002ce5c..ef2c4a4 100644 --- a/IPython/utils/py3compat.py +++ b/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 @@ -151,13 +152,21 @@ else: Accepts a string or a function, so it can be used as a decorator.""" return s.format(u='u') - def execfile(fname, glob, loc=None): - loc = loc if (loc is not None) else glob - scripttext = file(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 + if sys.platform == 'win32': + def execfile(fname, glob, loc=None): + loc = loc if (loc is not None) else glob + scripttext = file(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, glob, loc=None): + if isinstance(fname, unicode): + filename = fname.encode(sys.getfilesystemencoding()) + else: + filename = fname + __builtin__.execfile(filename, glob, loc)