Skip to content

Commit

Permalink
[s3] fix tracking S3File.closed (#1311)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier authored Sep 29, 2023
1 parent cf33d69 commit 7d6ceeb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 4 additions & 1 deletion storages/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def __init__(self, name, mode, storage, buffer_size=None):
self.obj.load(**params)
self._is_dirty = False
self._raw_bytes_written = 0
self._closed = False
self._file = None
self._multipart = None
self._parts = None
Expand All @@ -148,7 +149,7 @@ def size(self):

@property
def closed(self):
return not self._file or self._file.closed
return self._closed

def _get_file(self):
if self._file is None:
Expand All @@ -168,6 +169,7 @@ def _get_file(self):
self._file.seek(0)
if self._storage.gzip and self.obj.content_encoding == "gzip":
self._file = self._decompress_file(mode=self._mode, file=self._file)
self._closed = False
return self._file

def _set_file(self, value):
Expand Down Expand Up @@ -262,6 +264,7 @@ def close(self):
if self._file is not None:
self._file.close()
self._file = None
self._closed = True


@deconstructible
Expand Down
12 changes: 8 additions & 4 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,18 +904,22 @@ def test_loading_ssec(self):
def test_closed(self):
f = s3.S3File("test", "wb", self.storage)

with self.subTest("is True after init"):
self.assertTrue(f.closed)
with self.subTest("after init"):
self.assertFalse(f.closed)

with self.subTest("is False after file access"):
with self.subTest("after file access"):
# Ensure _get_file has been called
f.file
self.assertFalse(f.closed)

with self.subTest("is True after close"):
with self.subTest("after close"):
f.close()
self.assertTrue(f.closed)

with self.subTest("reopening"):
f.file
self.assertFalse(f.closed)


@mock_s3
class S3StorageTestsWithMoto(TestCase):
Expand Down

0 comments on commit 7d6ceeb

Please sign in to comment.