❯ ./python -i -m code
Python 3.15.0a0 (heads/main:5f91d5d9a41, Oct 12 2025, 22:21:59) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> exit()
now exiting InteractiveConsole...
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/home/bswck/Python/cpython/Lib/code.py", line 397, in <module>
interact(banner)
~~~~~~~~^^^^^^^^
File "/home/bswck/Python/cpython/Lib/code.py", line 383, in interact
console.interact(banner, exitmsg)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/home/bswck/Python/cpython/Lib/code.py", line 287, in interact
raise e
File "/home/bswck/Python/cpython/Lib/code.py", line 277, in interact
more = self.push(line)
File "/home/bswck/Python/cpython/Lib/code.py", line 325, in push
more = self.runsource(source, filename, symbol=_symbol)
File "/home/bswck/Python/cpython/Lib/code.py", line 75, in runsource
self.runcode(code)
~~~~~~~~~~~~^^^^^^
File "/home/bswck/Python/cpython/Lib/code.py", line 91, in runcode
exec(code, self.locals)
~~~~^^^^^^^^^^^^^^^^^^^
File "<console>", line 1, in <module>
File "<frozen _sitebuiltins>", line 26, in __call__
SystemExit: None
warning: can't use pyrepl: I/O operation on closed file
>>> exit()
Bug report
Bug description:
Combining a custom REPL with
-iis currently ramshackle, becausepymain_run_pythondoesn't know that something (either a file, or a module, or stdin) acted as a REPL and therefore a new one right after executing the original code isn't desired:cpython/Modules/main.c
Lines 681 to 697 in 5f91d5d
This leads to broken expectations around
python -i -m pdborpython -i -m asyncio:Broken output of
python -i -m asyncioBroken output of
python -i -m pdbBroken output of
python -i -m codeThis is an easy fix. Well, we could avoid the problem by just documenting here that combining this flag isn't supported and perhaps guard against
sys.flags.interactivein custom REPLs, but I dont think it's reasonable at all in this case.We can simply introduce a flag (like,
sys._running_custom_repl) or a function (like,sys._enter_custom_repl, that could raise an error if re-entered) that would inform thepymain_run_pythonfunction back that something already acted as the REPL for this runtime. The scope of that feature would be tightly coupled topymain_run_pythononly! The feature could be then used ininteract()or wherever it fits the most.I don't have a clear proposal regarding the constraints that
sys._enter_custom_replwould check; that's a work in progress and I'm open to suggestions.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux