Repository navigation
REPL exits with traceback when str(sys.ps1) raises #130698
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Feb 28, 2025 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shellRelated to the interactive shell
on Feb 28, 2025 Easy fix is
git diff diff --git a/Lib/_pyrepl/reader.py b/Lib/_pyrepl/reader.py index 1252847e02b..6d41802bce0 100644 --- a/Lib/_pyrepl/reader.py +++ b/Lib/_pyrepl/reader.py @@ -548,6 +548,11 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str: else: prompt = self.ps1 + try: + prompt = str(prompt) + except: + prompt = ">>> " + if self.can_colorize: prompt = f"{ANSIColors.BOLD_MAGENTA}{prompt}{ANSIColors.RESET}" return promptBut maybe we should handle unrecoverable exceptions like MemoryError, SystemExit and stop REPL?
Reacted by devdanzin and Pablo Galindo Salgado@sergey-miryanov wanna prepare a PR for this @sergey-miryanov ?
@pablogsal yes, will do.
@pablogsal I just have added a PR. Please take a look.
Note that old repl just clear error indicator and return empty prompt:
Lines 228 to 237 in 8a9c6c4
const char *ps1 = ""; if (_PySys_GetOptionalAttr(&_Py_ID(ps1), &attr) < 0) { PyErr_Clear(); } else if (attr != NULL) { ps1_obj = PyObject_Str(attr); Py_DECREF(attr); if (ps1_obj == NULL) { PyErr_Clear(); } But maybe we should handle unrecoverable exceptions like MemoryError, SystemExit and stop REPL?
MemoryError isn't really unrecoverable, unless you have some bug in your C code. The SystemExit is more questionable.
I think it's ok to replicate old-repl behavior, but instead of being just silent - emit a warning.
@skirpichev I don't know what is the best way - replicate old behavior, show default prompt (as I did in PR), or emit warning. Maybe @pablogsal can point to the right direction.
- marked REPL throws when
sys.ps1is not a string #158827 as a duplicate of this issueon Oct 5, 2026
Bug report
Bug description:
Subject says it all. This is on the latest HEAD of main.
Probably @pablogsal should look into this.
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
sys.ps1andsys.ps2to strings before PyREPL use #158828