Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[s3] fix tracking S3File.closed #1311

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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
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