Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomic replace (second attempt) #482

Closed
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(storages): add support for atomic writes on Windows
- Cross-platform atomic replace with os.replace
- Close handles before attempting to replace
- Close db_ in db fixture before tmpdir teardown
richardsheridan committed Jul 27, 2022
commit c64e5499f157729ae2fe68e77b87a5c04775b5b2
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,8 @@ def db(request):
db_.drop_tables()
db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')

yield db_
with db_:
yield db_


@pytest.fixture
4 changes: 2 additions & 2 deletions tinydb/storages.py
Original file line number Diff line number Diff line change
@@ -146,10 +146,10 @@ def write(self, data: Dict[str, Dict[str, Any]]):

# Replace the current file with the temporary file
temp_file.close()
os.rename(temp_file.name, file_name)
self._handle.close()
os.replace(temp_file.name, file_name)

# Reopen the file
self._handle.close()
self._handle = open(file_name, mode=self._mode, encoding=self._handle.encoding)