https://github.com/python/cpython/commit/e7c542de5f069a4b83e8eded3067613e4d59a529
commit: e7c542de5f069a4b83e8eded3067613e4d59a529
branch: main
author: Bartosz Sławecki <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-01-03T13:33:04+05:30
summary:
gh-140648: Make asyncio REPL respect the `-I` flag (isolated mode) (#143045)
files:
A Misc/NEWS.d/next/Library/2025-12-21-17-24-29.gh-issue-140648.i8dca6.rst
M Lib/asyncio/__main__.py
M Lib/test/test_repl.py
diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py
index afbb70bbcab930..44667efc522556 100644
--- a/Lib/asyncio/__main__.py
+++ b/Lib/asyncio/__main__.py
@@ -96,7 +96,7 @@ def run(self):
console.write(banner)
- if startup_path := os.getenv("PYTHONSTARTUP"):
+ if not sys.flags.isolated and (startup_path :=
os.getenv("PYTHONSTARTUP")):
sys.audit("cpython.run_startup", startup_path)
import tokenize
diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py
index 0fa1df40e44c5f..6cdb1ca65c6aed 100644
--- a/Lib/test/test_repl.py
+++ b/Lib/test/test_repl.py
@@ -28,7 +28,7 @@
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
@@ -42,7 +42,10 @@ def spawn_repl(*args, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, custom=F
# path may be used by PyConfig_Get("module_search_paths") 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:
@@ -215,7 +218,7 @@ def make_repl(env):
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
@@ -296,19 +299,27 @@ def test_asyncio_repl_reaches_python_startup_script(self):
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):
diff --git
a/Misc/NEWS.d/next/Library/2025-12-21-17-24-29.gh-issue-140648.i8dca6.rst
b/Misc/NEWS.d/next/Library/2025-12-21-17-24-29.gh-issue-140648.i8dca6.rst
new file mode 100644
index 00000000000000..9e56f096b938f1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-12-21-17-24-29.gh-issue-140648.i8dca6.rst
@@ -0,0 +1,3 @@
+The :mod:`asyncio` REPL now respects the :option:`-I` flag (isolated mode).
+Previously, it would load and execute :envvar:`PYTHONSTARTUP` even if the
+flag was set. Contributed by Bartosz Sławecki.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]