Skip to content

Commit

Permalink
Fixed typo in S3Boto3StorageFile.__init__() that prevented files op…
Browse files Browse the repository at this point in the history
…ened in "r" mode from being read. (jschneier#896)

lambda has a higher precedence than if-else, so `self._force_mode` was set to a function that returned
another lambda when `'b'` is not in `mode`.
Also added regression test.
  • Loading branch information
LincolnPuzey committed Jun 14, 2020
1 parent 8321ff5 commit 1c6004c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion storages/backends/s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(self, name, mode, storage, buffer_size=None):
self._storage = storage
self.name = name[len(self._storage.location):].lstrip('/')
self._mode = mode
self._force_mode = lambda b: b if 'b' in mode else lambda b: b.decode()
self._force_mode = (lambda b: b) if 'b' in mode else (lambda b: b.decode())
self.obj = storage.bucket.Object(name)
if 'w' not in mode:
# Force early RAII-style exception if object does not exist
Expand Down
8 changes: 8 additions & 0 deletions tests/test_s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ def test_compress_content_len(self):
content = self.storage._compress_content(content)
self.assertTrue(len(content.read()) > 0)

def test_storage_open_read_string(self):
"""
Test opening a file in "r" mode (ie reading as string, not bytes)
"""
name = 'test_open_read_string.txt'
content_str = self.storage.open(name, "r").read()
self.assertEqual(content_str, "")

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

0 comments on commit 1c6004c

Please sign in to comment.