Skip to content

Commit

Permalink
Merge pull request #1734 from alexa-infra/fix-filestorage-save-bytes-io
Browse files Browse the repository at this point in the history
FileStorage.save with file-like objects
  • Loading branch information
davidism authored Mar 17, 2020
2 parents 32f3b07 + c341a0a commit 244b486
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Unreleased
:issue:`1739`
- Fix missing local variables in interactive debugger console.
:issue:`1746`
- Fix passing file-like objects like ``io.BytesIO`` to
``FileStorage.save``. :issue:`1733`


Version 1.0.0
Expand Down
4 changes: 3 additions & 1 deletion src/werkzeug/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3058,7 +3058,9 @@ def save(self, dst, buffer_size=16384):
from shutil import copyfileobj

close_dst = False
dst = fspath(dst)

if hasattr(dst, "__fspath__"):
dst = fspath(dst)

if isinstance(dst, string_types):
dst = open(dst, "wb")
Expand Down
14 changes: 14 additions & 0 deletions tests/test_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,20 @@ def test_save_to_pathlib_dst(self, tmp_path):
storage.save(dst)
assert dst.read_text() == "test"

def test_save_to_bytes_io(self):
storage = self.storage_class(io.BytesIO(b"one\ntwo"))
dst = io.BytesIO()
storage.save(dst)
assert dst.getvalue() == b"one\ntwo"

def test_save_to_file(self, tmp_path):
path = tmp_path / "file.data"
storage = self.storage_class(io.BytesIO(b"one\ntwo"))
with path.open("wb") as dst:
storage.save(dst)
with path.open("rb") as src:
assert src.read() == b"one\ntwo"


@pytest.mark.parametrize("ranges", ([(0, 1), (-5, None)], [(5, None)]))
def test_range_to_header(ranges):
Expand Down

0 comments on commit 244b486

Please sign in to comment.