Skip to content

Commit d62526c

Browse files
committed
feat: Throw a more helpful exception when reading a closed part.
fixes #72
1 parent f6ad3a7 commit d62526c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

multipart.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,9 @@ def raw(self):
884884
885885
Warning, this may consume a lot of memory, check :attr:`size` first.
886886
"""
887+
if self.file is None:
888+
raise MultipartError("Cannot read from closed MultipartPart")
889+
887890
pos = self.file.tell()
888891
self.file.seek(0)
889892

@@ -895,6 +898,8 @@ def save_as(self, path):
895898
""" Save a copy of this part to `path` and return the number of bytes
896899
written.
897900
"""
901+
if self.file is None:
902+
raise MultipartError("Cannot read from closed MultipartPart")
898903

899904
with open(path, "wb") as fp:
900905
pos = self.file.tell()

test/test_legacy_parser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,12 @@ def test_segment_close(self):
197197
file1.close()
198198
self.assertIsNone(file1.file)
199199

200+
with self.assertMultipartError("Cannot read from closed MultipartPart"):
201+
file1.raw
202+
with self.assertMultipartError("Cannot read from closed MultipartPart"):
203+
file1.value
204+
with self.assertMultipartError("Cannot read from closed MultipartPart"):
205+
file1.save_as("/tmp/foo")
206+
200207
# Closing again is a NOP
201208
file1.close() # Do nothing

0 commit comments

Comments
 (0)