From 6b8ff8daed202f4cd0aa5636a92084d1f19f61c8 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 30 May 2021 12:53:19 +0200 Subject: Handle un-encodable initial text for editor (cherry picked from commit c74d1075620f54d8904b9ae822299ba1221450f4) --- qutebrowser/misc/editor.py | 2 +- tests/unit/misc/test_editor.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/qutebrowser/misc/editor.py b/qutebrowser/misc/editor.py index 5741c6b47..d561a7b96 100644 --- a/qutebrowser/misc/editor.py +++ b/qutebrowser/misc/editor.py @@ -131,7 +131,7 @@ class ExternalEditor(QObject): raise ValueError("Already editing a file!") try: self._filename = self._create_tempfile(text, 'qutebrowser-editor-') - except OSError as e: + except (OSError, UnicodeEncodeError) as e: message.error("Failed to create initial file: {}".format(e)) return diff --git a/tests/unit/misc/test_editor.py b/tests/unit/misc/test_editor.py index cd21308f8..8e5597a0e 100644 --- a/tests/unit/misc/test_editor.py +++ b/tests/unit/misc/test_editor.py @@ -148,6 +148,17 @@ class TestFileHandling: assert msg.text.startswith("Failed to create initial file: ") assert editor._proc is None + def test_encode_error(self, message_mock, editor, caplog, config_stub): + """Test file handling when the initial text can't be encoded.""" + config_stub.val.editor.encoding = 'ascii' + + with caplog.at_level(logging.ERROR): + editor.edit("fooƤbar") + + msg = message_mock.getmsg(usertypes.MessageLevel.error) + assert msg.text.startswith("Failed to create initial file: ") + assert editor._proc is None + def test_double_edit(self, editor): editor.edit("") with pytest.raises(ValueError): -- cgit v1.2.3-54-g00ecf