Improve exception handling in qsavefile_open.
Sometimes exceptions were shadowed with new exceptions because of the file flushing.
This commit is contained in:
parent
e300b2e30d
commit
27e82ce6c8
@ -180,7 +180,7 @@ def deserialize_stream(stream, obj):
|
|||||||
def savefile_open(filename, binary=False, encoding='utf-8'):
|
def savefile_open(filename, binary=False, encoding='utf-8'):
|
||||||
"""Context manager to easily use a QSaveFile."""
|
"""Context manager to easily use a QSaveFile."""
|
||||||
f = QSaveFile(filename)
|
f = QSaveFile(filename)
|
||||||
new_f = None
|
cancelled = False
|
||||||
try:
|
try:
|
||||||
ok = f.open(QIODevice.WriteOnly)
|
ok = f.open(QIODevice.WriteOnly)
|
||||||
if not ok:
|
if not ok:
|
||||||
@ -192,13 +192,15 @@ def savefile_open(filename, binary=False, encoding='utf-8'):
|
|||||||
yield new_f
|
yield new_f
|
||||||
except:
|
except:
|
||||||
f.cancelWriting()
|
f.cancelWriting()
|
||||||
|
cancelled = True
|
||||||
raise
|
raise
|
||||||
finally:
|
else:
|
||||||
if new_f is not None:
|
if new_f is not None:
|
||||||
new_f.flush()
|
new_f.flush()
|
||||||
|
finally:
|
||||||
commit_ok = f.commit()
|
commit_ok = f.commit()
|
||||||
if not commit_ok:
|
if not commit_ok and not cancelled:
|
||||||
raise OSError(f.errorString())
|
raise OSError("Commit failed!")
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
|
Loading…
Reference in New Issue
Block a user