BUG: read_parquet leaks file handles on error · Issue #46555 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

Run this in development mode:

from pathlib import Path import pandas as pd from pandas.io.parquet import to_parquet, read_parquet

path = Path('/tmp/test.parquet') to_parquet(pd.DataFrame(dict(a=[1])), path) with path.open('r+b') as f: f.seek(16) f.write(b'bogus')

try: read_parquet(path) except Exception: pass

Issue Description

The handles aren’t closed in a except/finally block, so they only get closed when everything works.

When running the above in development mode, you’ll see a ResourceWarning

Expected Behavior

all file handles are closed no matter what

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.9.12.final.0
python-bits : 64
OS : Linux
OS-release : 5.16.16-zen1-1-zen
Version : #1 ZEN SMP PREEMPT Mon, 21 Mar 2022 22:59:42 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : de_DE.UTF-8
LOCALE : de_DE.UTF-8
pandas : 1.4.1
numpy : 1.22.3
pytz : 2021.1
dateutil : 2.8.1
pip : 22.0.4
setuptools : 57.4.0
Cython : None
pytest : 7.1.1
hypothesis : None
sphinx : 4.3.2
blosc : None
feather : None
xlsxwriter : 1.4.0
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : 2.8.6
jinja2 : 3.0.3
IPython : 7.23.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fastparquet : 0.8.0
fsspec : 2022.02.0
gcsfs : None
matplotlib : 3.4.1
numba : 0.53.1
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
sqlalchemy : 1.4.23
tables : 3.6.1
tabulate : 0.8.9
xarray : None
xlrd : 1.2.0
xlwt : None
zstandard : None