Commit c4ecbd1e authored by Jason R. Coombs's avatar Jason R. Coombs

Since Python 3 will always need the _execfile functionality (to fulfill the...

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.
parent f3829d8b
...@@ -10,7 +10,6 @@ if PY2: ...@@ -10,7 +10,6 @@ if PY2:
import ConfigParser import ConfigParser
from StringIO import StringIO from StringIO import StringIO
BytesIO = StringIO BytesIO = StringIO
execfile = execfile
func_code = lambda o: o.func_code func_code = lambda o: o.func_code
func_globals = lambda o: o.func_globals func_globals = lambda o: o.func_globals
im_func = lambda o: o.im_func im_func = lambda o: o.im_func
...@@ -63,15 +62,6 @@ if PY3: ...@@ -63,15 +62,6 @@ if PY3:
) )
filterfalse = itertools.filterfalse 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): def reraise(tp, value, tb=None):
if value.__traceback__ is not tb: if value.__traceback__ is not tb:
raise value.with_traceback(tb) raise value.with_traceback(tb)
......
...@@ -20,12 +20,23 @@ _open = open ...@@ -20,12 +20,23 @@ _open = open
from distutils.errors import DistutilsError from distutils.errors import DistutilsError
from pkg_resources import working_set from pkg_resources import working_set
from setuptools.compat import builtins, execfile from setuptools.compat import builtins
__all__ = [ __all__ = [
"AbstractSandbox", "DirectorySandbox", "SandboxViolation", "run_setup", "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): def run_setup(setup_script, args):
"""Run a distutils setup script, sandboxed in its directory""" """Run a distutils setup script, sandboxed in its directory"""
old_dir = os.getcwd() old_dir = os.getcwd()
...@@ -46,12 +57,10 @@ def run_setup(setup_script, args): ...@@ -46,12 +57,10 @@ def run_setup(setup_script, args):
# reset to include setup dir, w/clean callback list # reset to include setup dir, w/clean callback list
working_set.__init__() working_set.__init__()
working_set.callbacks.append(lambda dist:dist.activate()) working_set.callbacks.append(lambda dist:dist.activate())
DirectorySandbox(setup_dir).run( def runner():
lambda: execfile( ns = dict(__file__=setup_script, __name__='__main__')
"setup.py", _execfile(setup_script, ns)
{'__file__':setup_script, '__name__':'__main__'} DirectorySandbox(setup_dir).run(runner)
)
)
except SystemExit: except SystemExit:
v = sys.exc_info()[1] v = sys.exc_info()[1]
if v.args and v.args[0]: if v.args and v.args[0]:
......
...@@ -109,7 +109,8 @@ import __future__ ...@@ -109,7 +109,8 @@ import __future__
import sys, traceback, inspect, linecache, os, re, types import sys, traceback, inspect, linecache, os, re, types
import unittest, difflib, pdb, tempfile import unittest, difflib, pdb, tempfile
import warnings 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 # Don't whine about the deprecated is_private function in this
# module's tests. # module's tests.
...@@ -2554,14 +2555,15 @@ def debug_script(src, pm=False, globs=None): ...@@ -2554,14 +2555,15 @@ def debug_script(src, pm=False, globs=None):
if pm: if pm:
try: try:
execfile(srcfilename, globs, globs) sandbox._execfile(srcfilename, globs)
except: except:
print(sys.exc_info()[1]) print(sys.exc_info()[1])
pdb.post_mortem(sys.exc_info()[2]) pdb.post_mortem(sys.exc_info()[2])
else: else:
# Note that %r is vital here. '%s' instead can, e.g., cause # Note that %r is vital here. '%s' instead can, e.g., cause
# backslashes to get treated as metacharacters on Windows. # 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: finally:
os.remove(srcfilename) os.remove(srcfilename)
......
...@@ -72,7 +72,7 @@ class TestSandbox(unittest.TestCase): ...@@ -72,7 +72,7 @@ class TestSandbox(unittest.TestCase):
target = pkg_resources.resource_filename(__name__, target = pkg_resources.resource_filename(__name__,
'script-with-bom.py') 'script-with-bom.py')
namespace = types.ModuleType('namespace') namespace = types.ModuleType('namespace')
setuptools.sandbox.execfile(target, vars(namespace)) setuptools.sandbox._execfile(target, vars(namespace))
assert namespace.result == 'passed' assert namespace.result == 'passed'
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment