Skip to content

Commit

Permalink
BUG: Close resources opened by pyxlsb (#39134)
Browse files Browse the repository at this point in the history
  • Loading branch information
twoertwein authored Jan 13, 2021
1 parent e4f03f9 commit d616e3b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
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

0 comments on commit d616e3b

Please sign in to comment.