Skip to content

REGR: close corrupt files in ExcelFile #41806

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

Merged
merged 1 commit into from
Jun 4, 2021
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.sum` and :meth:`DataFrame.prod` when ``min_count`` and ``numeric_only`` are both given (:issue:`41074`)
- Regression in :func:`read_csv` when using ``memory_map=True`` with an non-UTF8 encoding (:issue:`40986`)
- Regression in :meth:`DataFrame.replace` and :meth:`Series.replace` when the values to replace is a NumPy float array (:issue:`40371`)
- Regression in :func:`ExcelFile` when a corrupt file is opened but not closed (:issue:`41778`)

.. ---------------------------------------------------------------------------

Expand Down
12 changes: 9 additions & 3 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,11 @@ def __init__(self, filepath_or_buffer, storage_options: StorageOptions = None):
elif hasattr(self.handles.handle, "read"):
# N.B. xlrd.Book has a read attribute too
self.handles.handle.seek(0)
self.book = self.load_workbook(self.handles.handle)
try:
self.book = self.load_workbook(self.handles.handle)
except Exception:
self.close()
raise
elif isinstance(self.handles.handle, bytes):
self.book = self.load_workbook(BytesIO(self.handles.handle))
else:
Expand All @@ -440,8 +444,10 @@ def load_workbook(self, filepath_or_buffer):
pass

def close(self):
if hasattr(self.book, "close"):
# pyxlsb opens a TemporaryFile
if hasattr(self, "book") and hasattr(self.book, "close"):
# pyxlsb: opens a TemporaryFile
# openpyxl: https://stackoverflow.com/questions/31416842/
# openpyxl-does-not-close-excel-workbook-in-read-only-mode
self.book.close()
self.handles.close()

Expand Down
6 changes: 0 additions & 6 deletions pandas/io/excel/_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,6 @@ def load_workbook(self, filepath_or_buffer: FilePathOrBuffer):
filepath_or_buffer, read_only=True, data_only=True, keep_links=False
)

def close(self):
# https://stackoverflow.com/questions/31416842/
# openpyxl-does-not-close-excel-workbook-in-read-only-mode
self.book.close()
super().close()

@property
def sheet_names(self) -> list[str]:
return self.book.sheetnames
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
)
from functools import partial
import os
from pathlib import Path
from urllib.error import URLError
from zipfile import BadZipFile

Expand Down Expand Up @@ -1499,3 +1500,21 @@ def test_engine_invalid_option(self, read_ext):
with pytest.raises(ValueError, match="Value must be one of *"):
with pd.option_context(f"io.excel{read_ext}.reader", "abc"):
pass

def test_corrupt_files_closed(self, request, engine, read_ext):
# GH41778
errors = (BadZipFile,)
if engine is None:
pytest.skip()
elif engine == "xlrd":
import xlrd

errors = (BadZipFile, xlrd.biffh.XLRDError)

with tm.ensure_clean(f"corrupt{read_ext}") as file:
Path(file).write_text("corrupt")
with tm.assert_produces_warning(False):
try:
pd.ExcelFile(file, engine=engine)
except errors:
pass