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

S3Boto3StorageFile respects mode on readlines #1000

Merged
merged 1 commit into from
Sep 2, 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
3 changes: 3 additions & 0 deletions storages/backends/s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ def readline(self, *args, **kwargs):
raise AttributeError("File was not opened in read mode.")
return self._force_mode(super().readline(*args, **kwargs))

def readlines(self):
return list(self)

def write(self, content):
if 'w' not in self._mode:
raise AttributeError("File was not opened in write mode.")
Expand Down
20 changes: 20 additions & 0 deletions tests/test_s3boto3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import gzip
import io
import pickle
import threading
from textwrap import dedent
Expand Down Expand Up @@ -300,6 +301,25 @@ def test_storage_open_read_string(self):
self.assertEqual(content_str, "")
file.close()

def test_storage_open_readlines(self):
"""
Test readlines with file opened in "r" and "rb" modes
"""
name = 'test_open_readlines_string.txt'
with io.BytesIO() as temp_file:
temp_file.write(b"line1\nline2")
file = self.storage.open(name, "r")
file._file = temp_file

content_lines = file.readlines()
self.assertEqual(content_lines, ["line1\n", "line2"])

temp_file.seek(0)
file = self.storage.open(name, "rb")
file._file = temp_file
content_lines = file.readlines()
self.assertEqual(content_lines, [b"line1\n", b"line2"])

def test_storage_open_write(self):
"""
Test opening a file in write mode
Expand Down
Loading