From c2d39ddb19acaa8ae4e0374e92982e845656270a Mon Sep 17 00:00:00 2001 From: vladimir-poghosyan <38275127+vladimir-poghosyan@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:45:00 +0400 Subject: [PATCH 1/5] Fixed pyrepl history saving on read-only file systems --- Lib/_pyrepl/readline.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 888185eb03be66..bdcef73dbcf7fe 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -450,8 +450,14 @@ def read_history_file(self, filename: str = gethistoryfile()) -> None: def write_history_file(self, filename: str = gethistoryfile()) -> None: maxlength = self.saved_history_length history = self.get_reader().get_trimmed_history(maxlength) - f = open(os.path.expanduser(filename), "w", - encoding="utf-8", newline="\n") + + try: + f = open(os.path.expanduser(filename), "w", + encoding="utf-8", newline="\n") + except OSError as e: + warnings.warn(f"failed to open the history file: {e}") + return + with f: for entry in history: entry = entry.replace("\n", "\r\n") # multiline history support From 0da307d26d8125eb3ac34fe82e22efc0e1b2a5e0 Mon Sep 17 00:00:00 2001 From: vladimir-poghosyan <38275127+vladimir-poghosyan@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:46:04 +0400 Subject: [PATCH 2/5] Added NEWS.d entry --- .../next/Library/2024-12-19-07-27-52.gh-issue-128066.85NlMB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-12-19-07-27-52.gh-issue-128066.85NlMB.rst diff --git a/Misc/NEWS.d/next/Library/2024-12-19-07-27-52.gh-issue-128066.85NlMB.rst b/Misc/NEWS.d/next/Library/2024-12-19-07-27-52.gh-issue-128066.85NlMB.rst new file mode 100644 index 00000000000000..eeae8f1fc666c8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-19-07-27-52.gh-issue-128066.85NlMB.rst @@ -0,0 +1 @@ +Fixed pyrepl history saving on read-only file systems. From 9bc8ef596ff8d2e3c1ee790193421a0b7bd8c783 Mon Sep 17 00:00:00 2001 From: vladimir-poghosyan <38275127+vladimir-poghosyan@users.noreply.github.com> Date: Fri, 11 Apr 2025 09:15:40 +0400 Subject: [PATCH 3/5] Improved warning message during pyrepl history saving Co-authored-by: Sergey B Kirpichev --- Lib/_pyrepl/readline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index bdcef73dbcf7fe..66e1e846524bd9 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -455,7 +455,7 @@ def write_history_file(self, filename: str = gethistoryfile()) -> None: f = open(os.path.expanduser(filename), "w", encoding="utf-8", newline="\n") except OSError as e: - warnings.warn(f"failed to open the history file: {e}") + warnings.warn(f"failed to open the history file for writing: {e}") return with f: From e1a74c52c62822196766d5e2ffc20b479ea566e4 Mon Sep 17 00:00:00 2001 From: vladimir-poghosyan <38275127+vladimir-poghosyan@users.noreply.github.com> Date: Fri, 11 Apr 2025 10:50:15 +0400 Subject: [PATCH 4/5] Reverted changes for writing pyrepl history file --- Lib/_pyrepl/readline.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index cacc361da05e47..be229488e54e37 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -450,14 +450,8 @@ def read_history_file(self, filename: str = gethistoryfile()) -> None: def write_history_file(self, filename: str = gethistoryfile()) -> None: maxlength = self.saved_history_length history = self.get_reader().get_trimmed_history(maxlength) - - try: - f = open(os.path.expanduser(filename), "w", - encoding="utf-8", newline="\n") - except OSError as e: - warnings.warn(f"failed to open the history file for writing: {e}") - return - + f = open(os.path.expanduser(filename), "w", + encoding="utf-8", newline="\n") with f: for entry in history: entry = entry.replace("\n", "\r\n") # multiline history support From 090ca575786ca879aad627ead9f97b40846f7957 Mon Sep 17 00:00:00 2001 From: vladimir-poghosyan <38275127+vladimir-poghosyan@users.noreply.github.com> Date: Fri, 11 Apr 2025 10:53:33 +0400 Subject: [PATCH 5/5] Added warning on failing to write REPL history file at exit --- Lib/site.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/site.py b/Lib/site.py index 9da8b6724e1cec..79930268e25097 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -578,10 +578,10 @@ def register_readline(): def write_history(): try: readline_module.write_history_file(history) - except (FileNotFoundError, PermissionError): + except (FileNotFoundError, PermissionError, OSError) as e: # home directory does not exist or is not writable # https://bugs.python.org/issue19891 - pass + _warn(f'failed to open the history file for writing: {e}', RuntimeWarning) atexit.register(write_history)