Skip to content

Commit 14154ac

Browse files
author
Mike Dirolf
committed
fix for gridfs with non-ObjectId _ids
1 parent 2657065 commit 14154ac

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

gridfs/grid_file.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class directly.
122122
def __erase(self):
123123
"""Erase all of the data stored in this GridFile.
124124
"""
125-
grid_file = self.__collection.files.find_one(self.__id)
125+
grid_file = self.__collection.files.find_one({"_id": self.__id})
126126
grid_file["next"] = None
127127
grid_file["length"] = 0
128128
self.__collection.files.save(grid_file)
@@ -139,9 +139,9 @@ def mode(self):
139139

140140
def __create_property(field_name, read_only=False):
141141
def getter(self):
142-
return self.__collection.files.find_one(self.__id).get(field_name, None)
142+
return self.__collection.files.find_one({"_id": self.__id}).get(field_name, None)
143143
def setter(self, value):
144-
grid_file = self.__collection.files.find_one(self.__id)
144+
grid_file = self.__collection.files.find_one({"_id": self.__id})
145145
grid_file[field_name] = value
146146
self.__collection.files.save(grid_file)
147147
if not read_only:
@@ -166,7 +166,7 @@ def rename(self, filename):
166166
:Parameters:
167167
- `filename`: the new name for this GridFile
168168
"""
169-
grid_file = self.__collection.files.find_one(self.__id)
169+
grid_file = self.__collection.files.find_one({"_id": self.__id})
170170
grid_file["filename"] = filename
171171
self.__collection.files.save(grid_file)
172172

@@ -211,7 +211,7 @@ def flush(self):
211211
md5 = self.__collection.database()._command(SON([("filemd5", self.__id),
212212
("root", self.__collection.name())]))["md5"]
213213

214-
grid_file = self.__collection.files.find_one(self.__id)
214+
grid_file = self.__collection.files.find_one({"_id": self.__id})
215215
grid_file["md5"] = md5
216216
grid_file["length"] = self.__position + len(self.__buffer)
217217
self.__collection.files.save(grid_file)

test/test_grid_file.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,10 @@ def test_multiple_reads(self):
348348
self.assertEqual(file.read(2), "")
349349
file.close()
350350

351+
def test_spec_with_id(self):
352+
# This was raising a TypeError at one point - make sure it doesn't
353+
file = GridFile({"_id": "foobar", "filename": "foobar"}, self.db, "w")
354+
file.close()
355+
351356
if __name__ == "__main__":
352357
unittest.main()

0 commit comments

Comments
 (0)