Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index d3f07c7..9fcd219 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -3,18 +3,25 @@
import sys
import subprocess
+def _spawn_python(*args):
+ cmd_line = [sys.executable]
+ cmd_line.extend(args)
+ return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+
+def _kill_python(p):
+ p.stdin.close()
+ data = p.stdout.read()
+ p.stdout.close()
+ # try to cleanup the child so we don't appear to leak when running
+ # with regrtest -R. This should be a no-op on Windows.
+ subprocess._cleanup()
+ return data
+
class CmdLineTest(unittest.TestCase):
- def start_python(self, cmd_line):
- cmd = '"%s" %s' % (sys.executable, cmd_line)
- p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
- stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
- p.stdin.close()
- data = p.stdout.read()
- p.stdout.close()
- # try to cleanup the child so we don't appear to leak when running
- # with regrtest -R. This should be a no-op on Windows.
- subprocess._cleanup()
- return data
+ def start_python(self, *args):
+ p = _spawn_python(*args)
+ return _kill_python(p)
def exit_code(self, *args):
cmd_line = [sys.executable]
@@ -72,6 +79,17 @@
self.exit_code('-m', 'timeit', '-n', '1'),
0)
+ def test_run_module_bug1764407(self):
+ # -m and -i need to play well together
+ # Runs the timeit module and checks the __main__
+ # namespace has been populated appropriately
+ p = _spawn_python('-i', '-m', 'timeit', '-n', '1')
+ p.stdin.write('Timer\n')
+ p.stdin.write('exit()\n')
+ data = _kill_python(p)
+ self.assertTrue(data.startswith('1 loop'))
+ self.assertTrue('__main__.Timer' in data)
+
def test_run_code(self):
# Test expected operation of the '-c' switch
# Switch needs an argument
diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py
index 9de9811..dfefee9 100644
--- a/Lib/test/test_runpy.py
+++ b/Lib/test/test_runpy.py
@@ -5,7 +5,7 @@
import sys
import tempfile
from test.test_support import verbose, run_unittest, forget
-from runpy import _run_module_code, run_module
+from runpy import _run_code, _run_module_code, _run_module_as_main, run_module
# Set up the test code and expected results
@@ -26,10 +26,19 @@
" 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"
)
+ def test_run_code(self):
+ saved_argv0 = sys.argv[0]
+ d = _run_code(self.test_source, {})
+ self.failUnless(d["result"] == self.expected_result)
+ 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(sys.argv[0] is saved_argv0)
def test_run_module_code(self):
initial = object()
@@ -42,8 +51,7 @@
d1,
name,
file,
- loader,
- True)
+ loader)
self.failUnless("result" not in d1)
self.failUnless(d2["initial"] is initial)
self.failUnless(d2["result"] == self.expected_result)
@@ -57,16 +65,6 @@
self.failUnless(sys.argv[0] is saved_argv0)
self.failUnless(name not in sys.modules)
- 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["__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(sys.argv[0] is saved_argv0)
class RunModuleTest(unittest.TestCase):