diff --git a/README.asciidoc b/README.asciidoc index 8a5c7448d..e1b2a8cbc 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -152,8 +152,8 @@ Contributors, sorted by the number of commits in descending order: * Raphael Pierzina * Joel Torstensson * Jan Verbeek -* Tarcisio Fedrizzi * Patric Schmitz +* Tarcisio Fedrizzi * Claude * Corentin Julé * meles5 diff --git a/qutebrowser/utils/qtutils.py b/qutebrowser/utils/qtutils.py index 1fea5e6c6..841fc714a 100644 --- a/qutebrowser/utils/qtutils.py +++ b/qutebrowser/utils/qtutils.py @@ -209,20 +209,19 @@ def savefile_open(filename, binary=False, encoding='utf-8'): f = QSaveFile(filename) cancelled = False try: - ok = f.open(QIODevice.WriteOnly) - if not ok: + open_ok = f.open(QIODevice.WriteOnly) + if not open_ok: raise QtOSError(f) if binary: new_f = PyQIODevice(f) else: new_f = io.TextIOWrapper(PyQIODevice(f), encoding=encoding) yield new_f + new_f.flush() except: f.cancelWriting() cancelled = True raise - else: - new_f.flush() finally: commit_ok = f.commit() if not commit_ok and not cancelled: diff --git a/tests/unit/utils/test_qtutils.py b/tests/unit/utils/test_qtutils.py index 78c2cffdb..a3d895842 100644 --- a/tests/unit/utils/test_qtutils.py +++ b/tests/unit/utils/test_qtutils.py @@ -481,16 +481,27 @@ class TestSavefileOpen: assert str(excinfo.value) in errors assert tmpdir.listdir() == [filename] + def test_failing_flush(self, tmpdir): + """Test with the file being closed before flushing.""" + filename = tmpdir / 'foo' + with pytest.raises(ValueError) as excinfo: + with qtutils.savefile_open(str(filename), binary=True) as f: + f.write(b'Hello') + f.dev.commit() # provoke failing flush + + assert str(excinfo.value) == "IO operation on closed device!" + assert tmpdir.listdir() == [filename] + def test_failing_commit(self, tmpdir): """Test with the file being closed before committing.""" filename = tmpdir / 'foo' with pytest.raises(OSError) as excinfo: with qtutils.savefile_open(str(filename), binary=True) as f: f.write(b'Hello') - f.dev.commit() # provoke failing "real" commit + f.dev.cancelWriting() # provoke failing commit assert str(excinfo.value) == "Commit failed!" - assert tmpdir.listdir() == [filename] + assert tmpdir.listdir() == [] def test_line_endings(self, tmpdir): """Make sure line endings are translated correctly.