revert custom exception throwing in savefile_open

instead adapt test_failing_flush to catch correct exception (ValueError).
This commit is contained in:
Patric Schmitz 2016-06-28 15:03:23 +02:00
parent a33978ee89
commit 217b912f3c
2 changed files with 5 additions and 13 deletions

View File

@ -197,8 +197,6 @@ def deserialize_stream(stream, obj):
def savefile_open(filename, binary=False, encoding='utf-8'):
"""Context manager to easily use a QSaveFile."""
f = QSaveFile(filename)
open_ok = False
caller_finished = False
cancelled = False
try:
open_ok = f.open(QIODevice.WriteOnly)
@ -209,17 +207,11 @@ def savefile_open(filename, binary=False, encoding='utf-8'):
else:
new_f = io.TextIOWrapper(PyQIODevice(f), encoding=encoding)
yield new_f
caller_finished = True
new_f.flush()
except:
f.cancelWriting()
cancelled = True
if not open_ok:
raise QtOSError(f, msg="Open failed!")
elif not caller_finished:
raise
else:
raise QtOSError(f, msg="Flush failed!")
raise
finally:
commit_ok = f.commit()
if not commit_ok and not cancelled:

View File

@ -388,6 +388,7 @@ class TestSavefileOpen:
def test_mock_open_error(self, qsavefile_mock):
"""Test with a mock and a failing open()."""
qsavefile_mock.open.return_value = False
qsavefile_mock.errorString.return_value = "Hello World"
with pytest.raises(OSError) as excinfo:
with qtutils.savefile_open('filename'):
@ -395,7 +396,7 @@ class TestSavefileOpen:
qsavefile_mock.open.assert_called_once_with(QIODevice.WriteOnly)
qsavefile_mock.cancelWriting.assert_called_once_with()
assert str(excinfo.value) == "Open failed!"
assert str(excinfo.value) == "Hello World"
def test_mock_exception(self, qsavefile_mock):
"""Test with a mock and an exception in the block."""
@ -476,7 +477,6 @@ class TestSavefileOpen:
with qtutils.savefile_open(str(filename)):
pass
errors = ["Filename refers to a directory", # Qt >= 5.4
"Open failed!",
"Commit failed!"] # older Qt versions
assert str(excinfo.value) in errors
assert tmpdir.listdir() == [filename]
@ -484,12 +484,12 @@ class TestSavefileOpen:
def test_failing_flush(self, tmpdir):
"""Test with the file being closed before flushing."""
filename = tmpdir / 'foo'
with pytest.raises(OSError) as excinfo:
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) == "Flush failed!"
assert str(excinfo.value) == "IO operation on closed device!"
assert tmpdir.listdir() == [filename]
def test_failing_commit(self, tmpdir):