-
Notifications
You must be signed in to change notification settings - Fork 549
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
Inserting non JSON serializable object corrupts database #89
Comments
Thanks for reporting this problem. As far as I can see, the main problem was, that serialization and file access were interleaved (due to using |
@msiemens another solution is atomic file writes- write to another temporary file then rename that file to the original file. This is atomic because the move operation in windows and *nix is guaranteed to be atomic. Example: import os
import json
from tempfile import mkstemp
fd, path = mkstemp()
with os.fdopen(fd, 'w') as fp:
json.dump(data, fp)
os.rename(path, real_database_filename) See: |
@eugene-eeo That's certainly a possible solution. But I think it complicates the matter more than needed (here's an insightful article on atomic writes from MSDN: http://blogs.msdn.com/b/adioltean/archive/2005/12/28/507866.aspx). I'll keep this solution in mind, in case I might need it somewhere else. |
I would consider this a pretty severe issue as it corrupts the database on unexpected user input. I would instead expect this to act as defensive as possible.
Example code:
Results in the following in
some-db.json
:{"_default": {"1": {"foo": "bar"}, "2": {"bar":
This is the traceback:
This was on a linux box running python 2.7.10 and tinydb 3.1.0.
The text was updated successfully, but these errors were encountered: