Skip to content

Commit c341a0a

Browse files
alexa-infradavidism
authored andcommitted
fix os.fspath call on file-like objects
1 parent 32f3b07 commit c341a0a

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Unreleased
1414
:issue:`1739`
1515
- Fix missing local variables in interactive debugger console.
1616
:issue:`1746`
17+
- Fix passing file-like objects like ``io.BytesIO`` to
18+
``FileStorage.save``. :issue:`1733`
1719

1820

1921
Version 1.0.0

src/werkzeug/datastructures.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -3058,7 +3058,9 @@ def save(self, dst, buffer_size=16384):
30583058
from shutil import copyfileobj
30593059

30603060
close_dst = False
3061-
dst = fspath(dst)
3061+
3062+
if hasattr(dst, "__fspath__"):
3063+
dst = fspath(dst)
30623064

30633065
if isinstance(dst, string_types):
30643066
dst = open(dst, "wb")

tests/test_datastructures.py

+14
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,20 @@ def test_save_to_pathlib_dst(self, tmp_path):
12431243
storage.save(dst)
12441244
assert dst.read_text() == "test"
12451245

1246+
def test_save_to_bytes_io(self):
1247+
storage = self.storage_class(io.BytesIO(b"one\ntwo"))
1248+
dst = io.BytesIO()
1249+
storage.save(dst)
1250+
assert dst.getvalue() == b"one\ntwo"
1251+
1252+
def test_save_to_file(self, tmp_path):
1253+
path = tmp_path / "file.data"
1254+
storage = self.storage_class(io.BytesIO(b"one\ntwo"))
1255+
with path.open("wb") as dst:
1256+
storage.save(dst)
1257+
with path.open("rb") as src:
1258+
assert src.read() == b"one\ntwo"
1259+
12461260

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

0 commit comments

Comments
 (0)