-
Type:
Bug
-
Resolution: Done
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
When using GridFS.new_file with a manually-specified, pre-existing _id, and safe=True on the database connection, FileExists is not raised. This happens when trying to create the same file with non-empty contents.
db = Connection('localhost', safe=True).some_db
f = GridFS(db).new_file(_id='some id')
f.write('some content')
f.close() # file is created correctly
f = GridFS(db).new_file(_id='some id') # does not raise FileExists
f.write('some content')
f.close() # raises DuplicateKeyError instead of FileExists
Reason:
GridIn.close() calls GridIn.__flush(). This in turn eventually calls self._chunks.insert(chunk). With safe=True on the connection, the chunk insert raises DuplicateKeyError, which is not caught.
This will also happen on the f.write(content) call if the content causes GridIn's buffer to exceed the chunk size (because it will flush and raise the same DuplicateKeyError).