Skip to content

Commit

Permalink
S3Boto3StorageFile respects mode on readlines
Browse files Browse the repository at this point in the history
  • Loading branch information
emanlodovice committed Apr 7, 2021
1 parent 770332b commit 179d707
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
Expand Up @@ -150,6 +150,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
@@ -1,4 +1,5 @@
import gzip
import io
import pickle
import threading
from datetime import datetime
Expand Down Expand Up @@ -252,6 +253,25 @@ def test_storage_open_read_string(self):
content_str = self.storage.open(name, "r").read()
self.assertEqual(content_str, "")

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 179d707

Please sign in to comment.