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:
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)
......
......@@ -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]:
......
......@@ -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)
......
......@@ -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__':
......
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