diff --git a/pandas/io/common.py b/pandas/io/common.py index 12a886d057199..1fee840c5ed4a 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -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, diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 11797823cddc2..962ba2c7f9ef7 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -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 @@ -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: diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py index 34b36e2549b62..e37acb2ee6bea 100644 --- a/pandas/tests/io/test_pickle.py +++ b/pandas/tests/io/test_pickle.py @@ -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()