Skip to content

Commit

Permalink
BUG: do not suppress errors when closing file handles (pandas-dev#47165)
Browse files Browse the repository at this point in the history
  • Loading branch information
twoertwein authored and yehoshuadimarsky committed Jul 13, 2022
1 parent 27ec3e4 commit b568848
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Most I/O methods do no longer suppress ``OSError`` and ``ValueError`` when closing file handles (:issue:`47136`)
-

.. ---------------------------------------------------------------------------
Expand Down
7 changes: 2 additions & 5 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,8 @@ def close(self) -> None:
self.handle.flush()
self.handle.detach()
self.created_handles.remove(self.handle)
try:
for handle in self.created_handles:
handle.close()
except (OSError, ValueError):
pass
for handle in self.created_handles:
handle.close()
self.created_handles = []
self.is_wrapped = False

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,15 @@ def test_fail_mmap():
with pytest.raises(UnsupportedOperation, match="fileno"):
with BytesIO() as buffer:
icom.get_handle(buffer, "rb", memory_map=True)


def test_close_on_error():
# GH 47136
class TestError:
def close(self):
raise OSError("test")

with pytest.raises(OSError, match="test"):
with BytesIO() as buffer:
with icom.get_handle(buffer, "rb") as handles:
handles.created_handles.append(TestError())

0 comments on commit b568848

Please sign in to comment.