Skip to content
Open
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
13 changes: 13 additions & 0 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ def _get_file(self):
# This automatically decompresses the file
self.blob.download_to_file(self._file, checksum="crc32c")
self._file.seek(0)
if "b" not in self._mode:
if hasattr(self._file, "readable"):
# For versions > Python 3.10 compatibility
# See SpooledTemporaryFile changes in 3.11 (https://docs.python.org/3/library/tempfile.html) # noqa: E501
# Now fully implements the io.BufferedIOBase and io.TextIOBase abstract base classes allowing the file # noqa: E501
# to be readable in the mode that it was specified (without accessing the underlying _file object). # noqa: E501
# In this case, we need to wrap the file in a TextIOWrapper to ensure that the file is read as a text file. # noqa: E501
self._file = io.TextIOWrapper(self._file, encoding="utf-8")
else:
# For versions <= Python 3.10 compatibility
self._file = io.TextIOWrapper(
self._file._file, encoding="utf-8"
)
return self._file

def _set_file(self, value):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,26 @@ def test_iam_sign_blob_with_sa_email_setting(self):
access_token=storage.credentials.token,
)

def test_open_read_text_mode(self):
"""
Test opening a file in text mode ('rt') returns strings, not bytes
"""
data = b"This is some test text data."
expected_text = "This is some test text data."

with self.storage.open(self.filename, "rt") as f:
self.storage._client.bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob.assert_called_with(
self.filename, chunk_size=None
)

f.blob.download_to_file = lambda tmpfile, **kwargs: tmpfile.write(data)
content = f.read()

# Should return string, not bytes
self.assertIsInstance(content, str)
self.assertEqual(content, expected_text)


class GoogleCloudGzipClientTests(GCloudTestCase):
def setUp(self):
Expand Down