aboutsummaryrefslogtreecommitdiffstats
path: root/setuptools/sandbox.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2017-05-21 11:42:04 -0400
committerJason R. Coombs <jaraco@jaraco.com>2017-05-21 11:52:32 -0400
commit20567bef2a36b259d2209cc76fd11a61ad288853 (patch)
treee70e86c5d21e5399c7999cb2c36ab6404780e06d /setuptools/sandbox.py
parentd48cb39b4666477da1d954d3604023095c233869 (diff)
downloadexternal_python_setuptools-20567bef2a36b259d2209cc76fd11a61ad288853.tar.gz
external_python_setuptools-20567bef2a36b259d2209cc76fd11a61ad288853.tar.bz2
external_python_setuptools-20567bef2a36b259d2209cc76fd11a61ad288853.zip
Implement AbstractSandbox as a context manager.
Diffstat (limited to 'setuptools/sandbox.py')
-rwxr-xr-xsetuptools/sandbox.py31
1 files changed, 16 insertions, 15 deletions
diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py
index 53964f4b..14f18d74 100755
--- a/setuptools/sandbox.py
+++ b/setuptools/sandbox.py
@@ -249,11 +249,9 @@ def run_setup(setup_script, args):
setup_script.encode(sys.getfilesystemencoding())
)
- def runner():
+ with DirectorySandbox(setup_dir):
ns = dict(__file__=dunder_file, __name__='__main__')
_execfile(setup_script, ns)
-
- DirectorySandbox(setup_dir).run(runner)
except SystemExit as v:
if v.args and v.args[0]:
raise
@@ -275,21 +273,24 @@ class AbstractSandbox:
for name in self._attrs:
setattr(os, name, getattr(source, name))
+ def __enter__(self):
+ self._copy(self)
+ if _file:
+ builtins.file = self._file
+ builtins.open = self._open
+ self._active = True
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self._active = False
+ if _file:
+ builtins.file = _file
+ builtins.open = _open
+ self._copy(_os)
+
def run(self, func):
"""Run 'func' under os sandboxing"""
- try:
- self._copy(self)
- if _file:
- builtins.file = self._file
- builtins.open = self._open
- self._active = True
+ with self:
return func()
- finally:
- self._active = False
- if _file:
- builtins.file = _file
- builtins.open = _open
- self._copy(_os)
def _mk_dual_path_wrapper(name):
original = getattr(_os, name)