raise unittest.SkipTest("test module requires subprocess")
-def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, custom=False, **kw):
+def spawn_repl(*args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, custom=False, isolated=True, **kw):
"""Run the Python REPL with the given arguments.
kw is extra keyword args to pass to subprocess.Popen. Returns a Popen
# path may be used by Py_GetPath() to build the default module search
# path.
stdin_fname = os.path.join(os.path.dirname(sys.executable), "<stdin>")
- cmd_line = [stdin_fname, '-I']
+ cmd_line = [stdin_fname]
+ # Isolated mode implies -EPs and ignores PYTHON* variables.
+ if isolated:
+ cmd_line.append('-I')
# Don't re-run the built-in REPL from interactive mode
# if we're testing a custom REPL (such as the asyncio REPL).
if not custom:
with os_helper.temp_dir() as tmpdir:
script = os.path.join(tmpdir, "pythonstartup.py")
with open(script, "w") as f:
- f.write("print('from pythonstartup')" + os.linesep)
+ f.write("print('from pythonstartup')\n")
env = os.environ.copy()
env['PYTHONSTARTUP'] = script
with os_helper.temp_dir() as tmpdir:
script = os.path.join(tmpdir, "pythonstartup.py")
with open(script, "w") as f:
- f.write("print('pythonstartup done!')" + os.linesep)
- f.write("exit(0)" + os.linesep)
+ f.write("print('pythonstartup done!')\n")
+ env = os.environ.copy()
+ env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".asyncio_history")
+ env["PYTHONSTARTUP"] = script
+ p = spawn_asyncio_repl(isolated=False, env=env)
+ output = kill_python(p)
+ self.assertEqual(p.returncode, 0)
+ self.assertIn("pythonstartup done!", output)
+ def test_asyncio_repl_respects_isolated_mode(self):
+ with os_helper.temp_dir() as tmpdir:
+ script = os.path.join(tmpdir, "pythonstartup.py")
+ with open(script, "w") as f:
+ f.write("print('should not print')\n")
env = os.environ.copy()
env["PYTHON_HISTORY"] = os.path.join(tmpdir, ".asyncio_history")
env["PYTHONSTARTUP"] = script
- subprocess.check_call(
- [sys.executable, "-m", "asyncio"],
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- env=env,
- timeout=SHORT_TIMEOUT,
- )
+ p = spawn_asyncio_repl(isolated=True, env=env)
+ output = kill_python(p)
+ self.assertEqual(p.returncode, 0)
+ self.assertNotIn("should not print", output)
@unittest.skipUnless(pty, "requires pty")
def test_asyncio_repl_is_ok(self):