misc/editor: Fix tempfile deleted on error / editor crash

This patch attempts to fix an issue where an error occuring in
misc/guiprocess or the editor process crashing would delete the
temporary file thus making it impossible to recover changes not commited
to the form field from the editor.
This commit is contained in:
Tomasz Kramkowski 2015-10-01 13:15:50 +01:00
parent 0de1e40f20
commit c575435782
3 changed files with 16 additions and 3 deletions

View File

@ -58,6 +58,7 @@ class ExternalEditor(QObject):
return return
try: try:
os.close(self._oshandle) os.close(self._oshandle)
if self._proc.exit_status() != QProcess.CrashExit:
os.remove(self._filename) os.remove(self._filename)
except OSError as e: except OSError as e:
# NOTE: Do not replace this with "raise CommandError" as it's # NOTE: Do not replace this with "raise CommandError" as it's

View File

@ -151,3 +151,6 @@ class GUIProcess(QObject):
else: else:
message.error(self._win_id, "Error while spawning {}: {}.".format( message.error(self._win_id, "Error while spawning {}: {}.".format(
self._what, self._proc.error()), immediately=True) self._what, self._proc.error()), immediately=True)
def exit_status(self):
return self._proc.exitStatus()

View File

@ -91,19 +91,28 @@ class TestFileHandling:
filename = editor._filename filename = editor._filename
assert os.path.exists(filename) assert os.path.exists(filename)
editor._proc._proc.exitStatus = mock.Mock(
return_value=QProcess.CrashExit)
editor._proc.finished.emit(1, QProcess.NormalExit) editor._proc.finished.emit(1, QProcess.NormalExit)
assert not os.path.exists(filename) assert os.path.exists(filename)
os.remove(filename)
def test_crash(self, editor): def test_crash(self, editor):
"""Test file handling when closing with a crash.""" """Test file handling when closing with a crash."""
editor.edit("") editor.edit("")
filename = editor._filename filename = editor._filename
assert os.path.exists(filename) assert os.path.exists(filename)
editor._proc._proc.exitStatus = mock.Mock(
return_value=QProcess.CrashExit)
editor._proc.error.emit(QProcess.Crashed) editor._proc.error.emit(QProcess.Crashed)
editor._proc.finished.emit(0, QProcess.CrashExit) editor._proc.finished.emit(0, QProcess.CrashExit)
assert not os.path.exists(filename) assert os.path.exists(filename)
os.remove(filename)
@pytest.mark.posix @pytest.mark.posix
def test_unreadable(self, message_mock, editor): def test_unreadable(self, message_mock, editor):