Skip to content

BUG: Close resources opened by pyxlsb #39134

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
Jan 13, 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
13 changes: 6 additions & 7 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,12 @@ def _get_filepath_or_buffer(

# assuming storage_options is to be interpretted as headers
req_info = urllib.request.Request(filepath_or_buffer, headers=storage_options)
req = urlopen(req_info)
content_encoding = req.headers.get("Content-Encoding", None)
if content_encoding == "gzip":
# Override compression based on Content-Encoding header
compression = {"method": "gzip"}
reader = BytesIO(req.read())
req.close()
with urlopen(req_info) as req:
content_encoding = req.headers.get("Content-Encoding", None)
if content_encoding == "gzip":
# Override compression based on Content-Encoding header
compression = {"method": "gzip"}
reader = BytesIO(req.read())
return IOArgs(
filepath_or_buffer=reader,
encoding=encoding,
Expand Down
6 changes: 6 additions & 0 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ def load_workbook(self, filepath_or_buffer):
pass

def close(self):
if hasattr(self.book, "close"):
# pyxlsb opens a TemporaryFile
self.book.close()
self.handles.close()

@property
Expand Down Expand Up @@ -483,6 +486,9 @@ def parse(
sheet = self.get_sheet_by_index(asheetname)

data = self.get_sheet_data(sheet, convert_float)
if hasattr(sheet, "close"):
# pyxlsb opens two TemporaryFiles
sheet.close()
usecols = maybe_convert_usecols(usecols)

if not data:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ def __init__(self, path):
else:
self.headers = {"Content-Encoding": None}

def __enter__(self):
return self

def __exit__(self, *args):
self.close()

def read(self):
return self.file.read()

Expand Down