Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ def in_place(

try:
yield readable, writable
except Exception:
except BaseException:
# move backup back
readable.close()
writable.close()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,17 @@ def test_exception_in_context(self, tmpdir):
assert 'Lorem' not in data
assert 'lazy dog' in data

@pytest.mark.parametrize('error', [KeyboardInterrupt, SystemExit, GeneratorExit])
def test_base_exception_restores_original(self, tmpdir, error):
doc = self.create_reference(tmpdir)
with pytest.raises(error), doc.in_place(encoding='utf-8') as (reader, writer):
writer.write(self.alternate_content)
raise error()
assert doc.read_text(encoding='utf-8') == self.reference_content
assert reader.closed
assert writer.closed
assert not (doc + '.bak').exists()

def test_write_mode_invalid(self, tmpdir):
with pytest.raises(ValueError):
with (Path(tmpdir) / 'document').in_place(mode='w'):
Expand Down