Skip to content

Crash if malformed os.environ #14638

@khk-globus

Description

@khk-globus

Description

Setting os.environ to an invalid data structure crashes IPython. The following is obviously not correct, but while testing something else, I mistyped [] instead of {}:

$ ipython
Python 3.13.1 (main, Dec 12 2024, 16:35:44) [GCC 11.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.31.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import os

In [2]: os.environ = []    # <-------------- stupid human typo!

Traceback (most recent call last):
  File "/tmp/test-py313/bin/ipython", line 8, in <module>
    sys.exit(start_ipython())
             ~~~~~~~~~~~~~^^
  File "/tmp/test-py313/lib/python3.13/site-packages/IPython/__init__.py", line 130, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/tmp/test-py313/lib/python3.13/site-packages/traitlets/config/application.py", line 1075, in launch_instance
    app.start()
    ~~~~~~~~~^^
  File "/tmp/test-py313/lib/python3.13/site-packages/IPython/terminal/ipapp.py", line 317, in start
    self.shell.mainloop()
    ~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/test-py313/lib/python3.13/site-packages/IPython/terminal/interactiveshell.py", line 926, in mainloop
    self.interact()
    ~~~~~~~~~~~~~^^
  File "/tmp/test-py313/lib/python3.13/site-packages/IPython/terminal/interactiveshell.py", line 911, in interact
    code = self.prompt_for_code()
  File "/tmp/test-py313/lib/python3.13/site-packages/IPython/terminal/interactiveshell.py", line 854, in prompt_for_code
    text = self.pt_app.prompt(
        default=default,
        inputhook=self._inputhook,
        **self._extra_prompt_options(),
    )
  File "/tmp/test-py313/lib/python3.13/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1031, in prompt
    if self._output is None and is_dumb_terminal():
                                ~~~~~~~~~~~~~~~~^^
  File "/tmp/test-py313/lib/python3.13/site-packages/prompt_toolkit/utils.py", line 325, in is_dumb_terminal
    return is_dumb_terminal(os.environ.get("TERM", ""))
                            ^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'get'

If you suspect this is an IPython 8.31.0 bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

$ echo $?
1

When doing the same in the standard Python REPL, Python 3.13 crashes for a slightly different reason, but Python 3.10 through Python 3.12 handle it gracefully:

$ python3.12
Python 3.12.8 (main, Dec 12 2024, 16:33:58) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ = []
>>> 

$ 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 $?
1

I am fully aware that "one shouldn't do this at all." For example, use os.environ.clear(), os.environb is linked/a view of os.environ, etc. So, I'd understand if this ticket was just closed — this is clearly a low priority bug. However, a crash is a crash, which is why I reported it at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions