Skip to content

BUG 1.4: do not suppress errors when closing file handles #47160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -112,11 +112,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 @@ -600,3 +600,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())