Repository navigation
Py313 REPL crash on malformed os.environ #128636
Description
Bug description
While researching a personal project bug, I made a typo in the REPL. Obviously, one shouldn't do like the following distilled MRE, but a crash is a crash so reporting it:
$ python3.13
Python 3.13.1 (main, Dec 12 2024, 16:35:44) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ = []
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File ".../.local/Python3.13.1/lib/python3.13/_pyrepl/__main__.py", line 6, in <module>
__pyrepl_interactive_console()
File ".../.local/Python3.13.1/lib/python3.13/_pyrepl/main.py", line 59, in interactive_console
run_multiline_interactive_console(console)
File ".../local/Python3.13.1/lib/python3.13/_pyrepl/simple_interact.py", line 151, in run_multiline_interactive_console
statement = multiline_input(more_lines, ps1, ps2)
File ".../local/Python3.13.1/lib/python3.13/_pyrepl/readline.py", line 389, in multiline_input
return reader.readline()
File ".../local/Python3.13.1/lib/python3.13/_pyrepl/reader.py", line 795, in readline
self.prepare()
File ".../local/Python3.13.1/lib/python3.13/_pyrepl/historical_reader.py", line 302, in prepare
super().prepare()
File ".../local/Python3.13.1/lib/python3.13/_pyrepl/reader.py", line 635, in prepare
self.console.prepare()
File ".../local/Python3.13.1/lib/python3.13/_pyrepl/unix_console.py", line 349, in prepare
self.height, self.width = self.getheightwidth()
File ".../local/Python3.13.1/lib/python3.13/_pyrepl/unix_console.py", line 451, in getheightwidth
return int(os.environ["LINES"]), int(os.environ["COLUMNS"])
TypeError: list indices must be integers or slices, not str
$ echo $?
1This is clearly a PEBCAK interaction; the proper way to have cleared the environment would have been .clear() (and as an education point and/or reminder for search engine retrieval or others reading this later, os.environ and os.environb are "special" in that they're assumed to exist and are linked — a change to one affects the other).
But, my late-night typo did produce an interpreter crash, so reporting here.
Note that this does not appear to fail in Python up to v3.12. Python 3.13 is the first to crash this way.
Background
I initially thought it was an IPython bug, and so reported it there. @Carreau rightly course-corrected me, with a suggestion for a possible upstream fix in CPython.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
- gh-128636: Fix crash in PyREPL when
os.environis overwritten with an invalid value #128653 - [3.13] gh-128636: Fix crash in PyREPL when
os.environis overwritten with an invalid value (GH-128653) #129186 - gh-128636: Fix crash in PyREPL when os.environ is overwritten with an invalid value for mac #138089
- [3.14] gh-128636: Fix crash in PyREPL when os.environ is overwritten with an invalid value for macOS (GH-138089) #138938
- [3.13] gh-128636: Fix crash in PyREPL when os.environ is overwritten with an invalid value for macOS (GH-138089) #138942
Activity
hugovk commentedon Jan 8, 2025
Thanks for the report.
It's a problem in the new PyREPL added in 3.13, the old one is okay:
https://docs.python.org/3/whatsnew/3.13.html#a-better-interactive-interpreter
tomasr8 commentedon Jan 8, 2025
Similar thing happens when you do e.g.:
khk-globus commentedon Jan 8, 2025
So, that specific bug is within the new PyREPL. I see that. But Carreau hinted that a number of projects assume
os.environis just there and has a dict-like structure; would there be value in considering some protection aroundos.environ? Something like (hypothetical):That's more controversial of course, but perhaps not without some merit. A thought.
With that, I'll watch from the sidelines unless otherwise poked. Thanks!
tomasr8 commentedon Jan 8, 2025
I don't think we need to make
os.environprotected (that would be a pretty large breaking change), but the new REPL probably shouldn't crash because of it. Perhaps just catching the extraTypeErrorandValueErrormight be sufficient?MarcRene71 commentedon Jan 22, 2025
This kills wfastcgi.py, too. When you configue a Flaskhandler in an Microsoft IIS, pointing to "E:\Python313\python.exe|D:\www_test\wfastcgi.py" it immediately crashes with the self.getheightwidth() Error.
Using 3.12, the same thing works fine.
gh-128636: Fix crash in PyREPL when `os.environ` is overwritten with …
pythongh-128636: Fix crash in PyREPL when `os.environ` is overwritten…
[3.13] gh-128636: Fix crash in PyREPL when `os.environ` is overwritte…
ambv commentedon Jul 18, 2025
This is still not solved on macOS where the same PEBKAC causes a loop of:
gh-128636: Fix crash in PyREPL when os.environ is overwritten with an…
pythongh-128636: Fix crash in PyREPL when os.environ is overwritten w…
pythongh-128636: Fix crash in PyREPL when os.environ is overwritten w…
[3.13] pythongh-128636: Fix crash in PyREPL when os.environ is overwr…
[3.13] gh-128636: Fix crash in PyREPL when os.environ is overwritten …
[3.14] gh-128636: Fix crash in PyREPL when os.environ is overwritten …
yihong0618 commentedon Sep 17, 2025
This issue can close I think
hugovk commentedon Feb 13, 2026
Reported @standaell1234-maker to GH (also for #138942 (comment))