-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: pd.ExcelFile closes stream on destruction #32544
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
Changes from all commits
9b018f6
3c81041
22a06fc
9b9fff9
443780d
58e83fb
7609675
509f203
a8ea7bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,6 +366,9 @@ def _workbook_class(self): | |
def load_workbook(self, filepath_or_buffer): | ||
pass | ||
|
||
def close(self): | ||
pass | ||
|
||
@property | ||
@abc.abstractmethod | ||
def sheet_names(self): | ||
|
@@ -895,14 +898,7 @@ def sheet_names(self): | |
|
||
def close(self): | ||
"""close io if necessary""" | ||
if self.engine == "openpyxl": | ||
# https://stackoverflow.com/questions/31416842/ | ||
# openpyxl-does-not-close-excel-workbook-in-read-only-mode | ||
wb = self.book | ||
wb._archive.close() | ||
|
||
if hasattr(self.io, "close"): | ||
self.io.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this intentionally removed? Wondering if there is any engine that needs this... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the bug fix |
||
self._reader.close() | ||
|
||
def __enter__(self): | ||
return self | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -629,6 +629,17 @@ def test_read_from_py_localpath(self, read_ext): | |
|
||
tm.assert_frame_equal(expected, actual) | ||
|
||
@td.check_file_leaks | ||
def test_close_from_py_localpath(self, read_ext): | ||
|
||
# GH31467 | ||
str_path = os.path.join("test1" + read_ext) | ||
with open(str_path, "rb") as f: | ||
x = pd.read_excel(f, "Sheet1", index_col=0) | ||
del x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to construct this test without the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# should not throw an exception because the passed file was closed | ||
f.read() | ||
|
||
def test_reader_seconds(self, read_ext): | ||
if pd.read_excel.keywords["engine"] == "pyxlsb": | ||
pytest.xfail("Sheets containing datetimes not supported by pyxlsb") | ||
|
@@ -1020,10 +1031,10 @@ def test_excel_read_buffer(self, engine, read_ext): | |
tm.assert_frame_equal(expected, actual) | ||
|
||
def test_reader_closes_file(self, engine, read_ext): | ||
f = open("test1" + read_ext, "rb") | ||
with pd.ExcelFile(f) as xlsx: | ||
# parses okay | ||
pd.read_excel(xlsx, "Sheet1", index_col=0, engine=engine) | ||
with open("test1" + read_ext, "rb") as f: | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with pd.ExcelFile(f) as xlsx: | ||
# parses okay | ||
pd.read_excel(xlsx, "Sheet1", index_col=0, engine=engine) | ||
|
||
assert f.closed | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.