From 9d1107ea8082202b0238f60d975e721102893bc5 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 24 Dec 2024 21:44:28 +0100 Subject: [PATCH] yes: deal with UnicodeDecodeError in input(), fixes #6984 if retry is True, it will just retry to get a valid answer. if retry is False, it will return the default. the code can be tested by entering "error" (without the quotes). --- src/borg/helpers/yes_no.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/borg/helpers/yes_no.py b/src/borg/helpers/yes_no.py index 745b06a51a..305b8c0c8f 100644 --- a/src/borg/helpers/yes_no.py +++ b/src/borg/helpers/yes_no.py @@ -8,6 +8,9 @@ TRUISH = ("Yes", "YES", "yes", "Y", "y", "1") DEFAULTISH = ("Default", "DEFAULT", "default", "D", "d", "") +ERROR = "error" +assert ERROR not in TRUISH + FALSISH + DEFAULTISH + def yes( msg=None, @@ -88,10 +91,14 @@ def output(msg, msg_type, is_prompt=False, **kwargs): if not prompt: return default try: - answer = input() + answer = input() # this may raise UnicodeDecodeError, #6984 + if answer == ERROR: # for testing purposes + raise UnicodeDecodeError("?", b"?", 0, 1, "?") # args don't matter except EOFError: # avoid defaultish[0], defaultish could be empty answer = truish[0] if default else falsish[0] + except UnicodeDecodeError: + answer = ERROR if answer in defaultish: if default_msg: output(default_msg, "accepted_default")