Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion gridfs/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def __iter__(self):

def close(self):
"""Make GridOut more generically file-like."""
pass
self.seek(0)

def __enter__(self):
"""Makes it possible to use :class:`GridOut` files
Expand All @@ -491,6 +491,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
"""Makes it possible to use :class:`GridOut` files
with the context manager protocol.
"""
self.close()
return False


Expand Down
18 changes: 18 additions & 0 deletions test/test_grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,24 @@ def write_me(s, chunk_size):
# Custom
write_me(s, 262300)

def test_context_manager_twice(self):
if sys.version_info < (2,6):
raise SkipTest()
contents = b("Imagine this is some important data...")
gfsin = GridIn(self.db.fs, filename="important_twice")
with gfsin as infile:
infile.write(contents)
infile.close()

gfsout = GridOut(self.db.fs, gfsin._id)
# first context read
with gfsout as outfile:
self.assertEqual(contents, outfile.read())
# second context read
with gfsout as outfile:
self.assertEqual(contents, outfile.read())



if __name__ == "__main__":
unittest.main()