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

GoogleCloudFile text mode read fix #865

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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: 8 additions & 5 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.core.files.base import File
from django.utils import timezone
from django.utils.deconstruct import deconstructible
from django.utils.encoding import force_bytes, smart_str, force_str

from storages.base import BaseStorage
from storages.compress import CompressedFileMixin
Expand Down Expand Up @@ -41,6 +42,7 @@ def __init__(self, name, mode, storage):
self.name = name
self.mime_type, self.mime_encoding = mimetypes.guess_type(name)
self._mode = mode
self._force_mode = (lambda b: b) if 'b' in mode else force_str
self._storage = storage
self.blob = storage.bucket.get_blob(name, chunk_size=storage.blob_chunk_size)
if not self.blob and "w" in mode:
Expand Down Expand Up @@ -74,14 +76,15 @@ def _set_file(self, value):

file = property(_get_file, _set_file)

def read(self, num_bytes=None):
def read(self, *args, **kwargs):
if "r" not in self._mode:
raise AttributeError("File was not opened in read mode.")
return self._force_mode(super(GoogleCloudFile, self).read(*args, **kwargs))

if num_bytes is None:
num_bytes = -1

return super().read(num_bytes)
def readline(self, *args, **kwargs):
if "r" not in self._mode:
raise AttributeError("File was not opened in read mode.")
return self._force_mode(super(GoogleCloudFile, self).readline(*args, **kwargs))

def write(self, content):
if "w" not in self._mode:
Expand Down