Skip to content

Commit

Permalink
[s3] S3Boto3StorageFile respects mode on readlines (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
emanlodovice authored Sep 2, 2023
1 parent 0e6f10f commit 53d6675
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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

0 comments on commit 53d6675

Please sign in to comment.