Skip to content

Commit 23fea24

Browse files
Backport PR pandas-dev#47165 on branch 1.4.x (BUG: do not suppress errors when closing file handles) (pandas-dev#47178)
1 parent d84073c commit 23fea24

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
@@ -112,11 +112,8 @@ def close(self) -> None:
112112
self.handle.flush()
113113
self.handle.detach()
114114
self.created_handles.remove(self.handle)
115-
try:
116-
for handle in self.created_handles:
117-
handle.close()
118-
except (OSError, ValueError):
119-
pass
115+
for handle in self.created_handles:
116+
handle.close()
120117
self.created_handles = []
121118
self.is_wrapped = False
122119

pandas/tests/io/test_common.py

+12
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,15 @@ def test_fail_mmap():
600600
with pytest.raises(UnsupportedOperation, match="fileno"):
601601
with BytesIO() as buffer:
602602
icom.get_handle(buffer, "rb", memory_map=True)
603+
604+
605+
def test_close_on_error():
606+
# GH 47136
607+
class TestError:
608+
def close(self):
609+
raise OSError("test")
610+
611+
with pytest.raises(OSError, match="test"):
612+
with BytesIO() as buffer:
613+
with icom.get_handle(buffer, "rb") as handles:
614+
handles.created_handles.append(TestError())

0 commit comments

Comments
 (0)