Skip to content

Fix PYTHONSTARTUP exception handling in the REPL #143023

@johnslavik

Description

@johnslavik

Bug report

Bug description:

EDIT: Some exceptions currently do not prevent the REPL from starting but should -- that's the bug.
Below is a report about SystemExit and request for clarity if it was the intended behavior for it to be able to prevent the REPL from starting. It seems that SystemExit should prevent the REPL from starting, but there are other exceptions such as KeyboardInterrupt that do stop the Python startup script execution, but still proceed to a new REPL session, and that likely isn't desired.


To reproduce:

  1. The new REPL
❯ PYTHONSTARTUP=<(echo 'raise SystemExit') ./python
Python 3.15.0a3+ (heads/main:5b5263648f2, Dec 20 2025, 07:19:41) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
  1. The basic REPL
❯ PYTHON_BASIC_REPL=1 PYTHONSTARTUP=<(echo 'raise SystemExit') ./python
Python 3.15.0a3+ (heads/main:5b5263648f2, Dec 20 2025, 07:19:41) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.

(both actually hit the same code path -- Py_RunMain -> pymain_run_python -> pymain_run_stdin -> pymain_run_startup -> _PyRun_SimpleFileObject -> PyErr_Print -> PyErr_PrintEx -> _PyErr_PrintEx -> handle_system_exit)

While this is largely an edge case of the regular REPLs, copying this semantics just feels wrong in Python code.
I've run into this while adding PYTHONSTARTUP exception handling to the asyncio REPL in GH-140288.

Should I be keeping asyncio REPL in parity with Python-side REPLs behavior in this regard?
Do we want to fix this in the C code?

And lastly, as part of this issue we would document the exception handling of PYTHONSTARTUP.

@ZeroIntensity stdlib interpreter-core docs topic-repl 3.13 3.14 3.15

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtopic-replRelated to the interactive shelltype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions