diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2014-07-05 14:42:49 -0400 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2014-07-05 14:42:49 -0400 |
commit | c4ecbd1e58a3384a5ab730450fa5487e48b68721 (patch) | |
tree | 48c2ea3b73cd70fdb47d2c1e4d2ee966c36b5377 /setuptools | |
parent | f3829d8bd0cd852be133f8822aa4493a7a12fd51 (diff) | |
download | external_python_setuptools-c4ecbd1e58a3384a5ab730450fa5487e48b68721.tar.gz external_python_setuptools-c4ecbd1e58a3384a5ab730450fa5487e48b68721.tar.bz2 external_python_setuptools-c4ecbd1e58a3384a5ab730450fa5487e48b68721.zip |
Since Python 3 will always need the _execfile functionality (to fulfill the test in test_sandbox), this functionality should become part of the core implementation.
Diffstat (limited to 'setuptools')
-rw-r--r-- | setuptools/compat.py | 10 | ||||
-rwxr-xr-x | setuptools/sandbox.py | 23 | ||||
-rw-r--r-- | setuptools/tests/doctest.py | 8 | ||||
-rw-r--r-- | setuptools/tests/test_sandbox.py | 2 |
4 files changed, 22 insertions, 21 deletions
diff --git a/setuptools/compat.py b/setuptools/compat.py index 507f8e6c..45c2f24c 100644 --- a/setuptools/compat.py +++ b/setuptools/compat.py @@ -10,7 +10,6 @@ if PY2: import ConfigParser from StringIO import StringIO BytesIO = StringIO - execfile = execfile func_code = lambda o: o.func_code func_globals = lambda o: o.func_globals im_func = lambda o: o.im_func @@ -63,15 +62,6 @@ if PY3: ) filterfalse = itertools.filterfalse - def execfile(fn, globs=None, locs=None): - if globs is None: - globs = globals() - if locs is None: - locs = globs - with open(fn, 'rb') as f: - source = f.read() - exec(compile(source, fn, 'exec'), globs, locs) - def reraise(tp, value, tb=None): if value.__traceback__ is not tb: raise value.with_traceback(tb) diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py index dc6e54bf..7985e9ee 100755 --- a/setuptools/sandbox.py +++ b/setuptools/sandbox.py @@ -20,12 +20,23 @@ _open = open from distutils.errors import DistutilsError from pkg_resources import working_set -from setuptools.compat import builtins, execfile +from setuptools.compat import builtins __all__ = [ "AbstractSandbox", "DirectorySandbox", "SandboxViolation", "run_setup", ] +def _execfile(filename, globals, locals=None): + """ + Python 3 implementation of execfile. + """ + with open(filename, 'rb') as stream: + script = stream.read() + if locals is None: + locals = globals + code = compile(script, filename, 'exec') + exec(code, globals, locals) + def run_setup(setup_script, args): """Run a distutils setup script, sandboxed in its directory""" old_dir = os.getcwd() @@ -46,12 +57,10 @@ def run_setup(setup_script, args): # reset to include setup dir, w/clean callback list working_set.__init__() working_set.callbacks.append(lambda dist:dist.activate()) - DirectorySandbox(setup_dir).run( - lambda: execfile( - "setup.py", - {'__file__':setup_script, '__name__':'__main__'} - ) - ) + def runner(): + ns = dict(__file__=setup_script, __name__='__main__') + _execfile(setup_script, ns) + DirectorySandbox(setup_dir).run(runner) except SystemExit: v = sys.exc_info()[1] if v.args and v.args[0]: diff --git a/setuptools/tests/doctest.py b/setuptools/tests/doctest.py index 47293c3c..75a1ff54 100644 --- a/setuptools/tests/doctest.py +++ b/setuptools/tests/doctest.py @@ -109,7 +109,8 @@ import __future__ import sys, traceback, inspect, linecache, os, re, types import unittest, difflib, pdb, tempfile import warnings -from setuptools.compat import StringIO, execfile, func_code, im_func +from setuptools.compat import StringIO, func_code, im_func +from setuptools import sandbox # Don't whine about the deprecated is_private function in this # module's tests. @@ -2554,14 +2555,15 @@ def debug_script(src, pm=False, globs=None): if pm: try: - execfile(srcfilename, globs, globs) + sandbox._execfile(srcfilename, globs) except: print(sys.exc_info()[1]) pdb.post_mortem(sys.exc_info()[2]) else: # Note that %r is vital here. '%s' instead can, e.g., cause # backslashes to get treated as metacharacters on Windows. - pdb.run("execfile(%r)" % srcfilename, globs, globs) + cmd = "sandbox._execfile(%r, globals())" % srcfilename + pdb.run(cmd, globs, globs) finally: os.remove(srcfilename) diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 3dad1376..06b3d434 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -72,7 +72,7 @@ class TestSandbox(unittest.TestCase): target = pkg_resources.resource_filename(__name__, 'script-with-bom.py') namespace = types.ModuleType('namespace') - setuptools.sandbox.execfile(target, vars(namespace)) + setuptools.sandbox._execfile(target, vars(namespace)) assert namespace.result == 'passed' if __name__ == '__main__': |