From 750f2be0e505ab1e9c7e9ff54fe2cdaee1eb9081 Mon Sep 17 00:00:00 2001 From: Chris Patti Date: Tue, 20 May 2025 11:19:34 -0400 Subject: [PATCH 1/5] fix: Catch OSError but don't emit a warning on failure to write history. --- Lib/site.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/site.py b/Lib/site.py index 5c38b1b17d5abd..ec97a7b926360e 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -578,7 +578,7 @@ def register_readline(): def write_history(): try: readline_module.write_history_file(history) - except (FileNotFoundError, PermissionError): + except (FileNotFoundError, PermissionError, OSError): # home directory does not exist or is not writable # https://bugs.python.org/issue19891 pass From 01225ff4a74c2c055128479ce0a65b1559dd1586 Mon Sep 17 00:00:00 2001 From: Chris Patti Date: Tue, 20 May 2025 13:46:34 -0400 Subject: [PATCH 2/5] fix: Properly handle REPL exit for read only FS. --- Lib/site.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/site.py b/Lib/site.py index ec97a7b926360e..6a85e0d293cd76 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -75,6 +75,7 @@ import _sitebuiltins import _io as io import stat +import errno # Prefixes for site-packages; add additional prefixes like /usr/local here PREFIXES = [sys.prefix, sys.exec_prefix] @@ -578,10 +579,16 @@ def register_readline(): def write_history(): try: readline_module.write_history_file(history) - except (FileNotFoundError, PermissionError, OSError): + except (FileNotFoundError, PermissionError): # home directory does not exist or is not writable # https://bugs.python.org/issue19891 pass + except (OSError): + if errno.EROFS: + # Ignore this error - read only fs. + pass + else: + raise atexit.register(write_history) From 498a430ebe1a62e8cf23893be69f4d8d13a5eaf0 Mon Sep 17 00:00:00 2001 From: Chris Patti Date: Tue, 20 May 2025 14:22:08 -0400 Subject: [PATCH 3/5] fix: Handle append case that's run after every cmd --- Lib/_pyrepl/simple_interact.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index b3848833e14208..a022ef836e5334 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -31,6 +31,7 @@ import sys import code import warnings +import errno from .readline import _get_reader, multiline_input, append_history_file @@ -147,8 +148,14 @@ def maybe_run_command(statement: str) -> bool: assert not more try: append_history_file() - except (FileNotFoundError, PermissionError, OSError) as e: + except (FileNotFoundError, PermissionError) as e: warnings.warn(f"failed to open the history file for writing: {e}") + except (OSError): + if errno.EROFS: + pass + else: + raise + input_n += 1 except KeyboardInterrupt: r = _get_reader() From 00fa8545babebaae766cda92189b089a1df204d1 Mon Sep 17 00:00:00 2001 From: Chris Patti Date: Tue, 20 May 2025 14:42:02 -0400 Subject: [PATCH 4/5] fix: Add NEWS file blurb with.. blurb :) --- .../2025-05-20-14-41-50.gh-issue-128066.qzzGfv.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-05-20-14-41-50.gh-issue-128066.qzzGfv.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-05-20-14-41-50.gh-issue-128066.qzzGfv.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-20-14-41-50.gh-issue-128066.qzzGfv.rst new file mode 100644 index 00000000000000..f78190276851b4 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-05-20-14-41-50.gh-issue-128066.qzzGfv.rst @@ -0,0 +1,3 @@ +Fixes an edge case where PyREPL improperly threw an error when Python is +invoked on a read only filesystem while trying to write history file +entries. From 58fd8f440f101f0be1830bb21d92926bd901a816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Tue, 20 May 2025 21:17:17 +0200 Subject: [PATCH 5/5] Review nits --- Lib/_pyrepl/simple_interact.py | 7 +------ Lib/site.py | 7 +++---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index d6579292325173..965b853c34b392 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -152,13 +152,8 @@ def maybe_run_command(statement: str) -> bool: assert not more try: append_history_file() - except (FileNotFoundError, PermissionError) as e: + except (FileNotFoundError, PermissionError, OSError) as e: warnings.warn(f"failed to open the history file for writing: {e}") - except (OSError): - if errno.EROFS: - pass - else: - raise input_n += 1 except KeyboardInterrupt: diff --git a/Lib/site.py b/Lib/site.py index 6a85e0d293cd76..f93271971594d8 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -579,14 +579,13 @@ def register_readline(): def write_history(): try: readline_module.write_history_file(history) - except (FileNotFoundError, PermissionError): + except FileNotFoundError, PermissionError: # home directory does not exist or is not writable # https://bugs.python.org/issue19891 pass - except (OSError): + except OSError: if errno.EROFS: - # Ignore this error - read only fs. - pass + pass # gh-128066: read-only file system else: raise