Skip to content

Commit 96ba36d

Browse files
authored
BUG: do not suppress errors when closing file handles (#47165)
1 parent 7e23a37 commit 96ba36d

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

doc/source/whatsnew/v1.4.3.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Fixed regressions
3030

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

3636
.. ---------------------------------------------------------------------------

pandas/io/common.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,8 @@ def close(self) -> None:
115115
self.handle.flush()
116116
self.handle.detach()
117117
self.created_handles.remove(self.handle)
118-
try:
119-
for handle in self.created_handles:
120-
handle.close()
121-
except (OSError, ValueError):
122-
pass
118+
for handle in self.created_handles:
119+
handle.close()
123120
self.created_handles = []
124121
self.is_wrapped = False
125122

pandas/tests/io/test_common.py

+12
Original file line numberDiff line numberDiff line change
@@ -592,3 +592,15 @@ def test_fail_mmap():
592592
with pytest.raises(UnsupportedOperation, match="fileno"):
593593
with BytesIO() as buffer:
594594
icom.get_handle(buffer, "rb", memory_map=True)
595+
596+
597+
def test_close_on_error():
598+
# GH 47136
599+
class TestError:
600+
def close(self):
601+
raise OSError("test")
602+
603+
with pytest.raises(OSError, match="test"):
604+
with BytesIO() as buffer:
605+
with icom.get_handle(buffer, "rb") as handles:
606+
handles.created_handles.append(TestError())

0 commit comments

Comments
 (0)