Guard against PHP-237 - PYTHON-290

This commit is contained in:
behackett 2011-11-11 15:49:52 -08:00
parent ef0c3fef19
commit a4ea04cb7f
2 changed files with 17 additions and 0 deletions

View File

@ -55,6 +55,9 @@ def _create_property(field_name, docstring,
if closed_only and not self._closed:
raise AttributeError("can only get %r on a closed file" %
field_name)
# Protect against PHP-237
if field_name == 'length':
return self._file.get(field_name, 0)
return self._file.get(field_name, None)
def setter(self, value):

View File

@ -289,6 +289,20 @@ class TestGridfs(unittest.TestCase):
self.assertEqual(u"".encode("iso-8859-1"), self.fs.get(oid).read())
self.assertEqual("iso-8859-1", self.fs.get(oid).encoding)
def test_missing_length_iter(self):
# Test fix that guards against PHP-237
self.fs.put("", filename="empty")
doc = self.db.fs.files.find_one({"filename": "empty"})
doc.pop("length")
self.db.fs.files.save(doc)
f = self.fs.get_last_version(filename="empty")
def iterate_file(grid_file):
for chunk in grid_file:
pass
return True
self.assertTrue(iterate_file(f))
if __name__ == "__main__":
unittest.main()