Fix an incompatibility between the -i and -m command line switches as reported on python-dev by PJE - runpy.run_module now leaves any changes it makes to the sys module intact after the function terminates

This commit is contained in:
Nick Coghlan 2007-07-24 13:58:28 +00:00
parent 4f82a03714
commit 13c25c08ca
3 changed files with 43 additions and 48 deletions

View File

@ -56,9 +56,11 @@ the top level namespace of the \module{__builtin__} module.
If the argument \var{alter_sys} is supplied and evaluates to
\code{True}, then \code{sys.argv[0]} is updated with the value of
\code{__file__} and \code{sys.modules[__name__]} is updated with a
temporary module object for the module being executed. Both
\code{sys.argv[0]} and \code{sys.modules[__name__]} are restored to
their original values before the function returns.
new module object for the module being executed. Note that neither
\code{sys.argv[0]} nor \code{sys.modules[__name__]} are restored to
their original values before the function returns - if client code
needs these values preserved, it must either save them explicitly or
else avoid enabling the automatic alterations to \module{sys}.
Note that this manipulation of \module{sys} is not thread-safe. Other
threads may see the partially initialised module, as well as the

View File

@ -33,36 +33,21 @@ def _run_code(code, run_globals, init_globals,
return run_globals
def _run_module_code(code, init_globals=None,
mod_name=None, mod_fname=None,
mod_loader=None, alter_sys=False):
mod_name=None, mod_fname=None,
mod_loader=None, alter_sys=False):
"""Helper for run_module"""
# Set up the top level namespace dictionary
if alter_sys:
# Modify sys.argv[0] and sys.module[mod_name]
temp_module = imp.new_module(mod_name)
mod_globals = temp_module.__dict__
saved_argv0 = sys.argv[0]
restore_module = mod_name in sys.modules
if restore_module:
saved_module = sys.modules[mod_name]
# Modify sys.argv[0] and sys.modules[mod_name]
sys.argv[0] = mod_fname
sys.modules[mod_name] = temp_module
try:
_run_code(code, mod_globals, init_globals,
mod_name, mod_fname, mod_loader)
finally:
sys.argv[0] = saved_argv0
if restore_module:
sys.modules[mod_name] = saved_module
else:
del sys.modules[mod_name]
# Copy the globals of the temporary module, as they
# may be cleared when the temporary module goes away
return mod_globals.copy()
module = imp.new_module(mod_name)
sys.modules[mod_name] = module
mod_globals = module.__dict__
else:
# Leave the sys module alone
return _run_code(code, {}, init_globals,
mod_name, mod_fname, mod_loader)
mod_globals = {}
return _run_code(code, mod_globals, init_globals,
mod_name, mod_fname, mod_loader)
# This helper is needed due to a missing component in the PEP 302

View File

@ -26,8 +26,7 @@ class RunModuleCodeTest(unittest.TestCase):
" module_in_sys_modules = globals() is sys.modules[__name__].__dict__\n"
"# Check nested operation\n"
"import runpy\n"
"nested = runpy._run_module_code('x=1\\n', mod_name='<run>',\n"
" alter_sys=True)\n"
"nested = runpy._run_module_code('x=1\\n', mod_name='<run>')\n"
)
@ -38,35 +37,44 @@ class RunModuleCodeTest(unittest.TestCase):
loader = "Now you're just being silly"
d1 = dict(initial=initial)
saved_argv0 = sys.argv[0]
d2 = _run_module_code(self.test_source,
d1,
name,
file,
loader,
True)
self.failUnless("result" not in d1)
self.failUnless(d2["initial"] is initial)
self.failUnless(d2["result"] == self.expected_result)
self.failUnless(d2["nested"]["x"] == 1)
self.failUnless(d2["__name__"] is name)
self.failUnless(d2["run_name_in_sys_modules"])
self.failUnless(d2["module_in_sys_modules"])
self.failUnless(d2["__file__"] is file)
self.failUnless(d2["run_argv0"] is file)
self.failUnless(d2["__loader__"] is loader)
self.failUnless(sys.argv[0] is saved_argv0)
self.failUnless(name not in sys.modules)
try:
d2 = _run_module_code(self.test_source,
d1,
name,
file,
loader,
alter_sys=True)
self.failUnless("result" not in d1)
self.failUnless(d2["initial"] is initial)
self.failUnless(d2["result"] == self.expected_result)
self.failUnless(d2["nested"]["x"] == 1)
self.failUnless(d2["nested"]["__name__"] == "<run>")
self.failUnless(d2["__name__"] is name)
self.failUnless(d2["__file__"] is file)
self.failUnless(d2["__loader__"] is loader)
self.failUnless(d2["run_argv0"] is file)
self.failUnless(d2["run_name_in_sys_modules"])
self.failUnless(d2["module_in_sys_modules"])
self.failUnless(sys.argv[0] is not saved_argv0)
self.failUnless(name in sys.modules)
finally:
sys.argv[0] = saved_argv0
if name in sys.modules:
del sys.modules[name]
def test_run_module_code_defaults(self):
saved_argv0 = sys.argv[0]
d = _run_module_code(self.test_source)
self.failUnless(d["result"] == self.expected_result)
self.failUnless(d["nested"]["x"] == 1)
self.failUnless(d["nested"]["__name__"] == "<run>")
self.failUnless(d["__name__"] is None)
self.failUnless(d["__file__"] is None)
self.failUnless(d["__loader__"] is None)
self.failUnless(d["run_argv0"] is saved_argv0)
self.failUnless("run_name" not in d)
self.failUnless(not d["run_name_in_sys_modules"])
self.failUnless(sys.argv[0] is saved_argv0)
self.failUnless(None not in sys.modules)
class RunModuleTest(unittest.TestCase):