Skip to content

Commit 8808db8

Browse files
authored
Close FastParquet file even on error (#46556)
1 parent efb262f commit 8808db8

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

doc/source/whatsnew/v1.5.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ I/O
561561
- Bug in :func:`read_parquet` when ``engine="pyarrow"`` which caused partial write to disk when column of unsupported datatype was passed (:issue:`44914`)
562562
- Bug in :func:`DataFrame.to_excel` and :class:`ExcelWriter` would raise when writing an empty DataFrame to a ``.ods`` file (:issue:`45793`)
563563
- Bug in Parquet roundtrip for Interval dtype with ``datetime64[ns]`` subtype (:issue:`45881`)
564-
- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements(:issue:`45598`)
564+
- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements (:issue:`45598`)
565+
- Bug in :func:`read_parquet` when ``engine="fastparquet"`` where the file was not closed on error (:issue:`46555`)
565566

566567
Period
567568
^^^^^^

pandas/io/parquet.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,12 @@ def read(
349349
)
350350
path = handles.handle
351351

352-
parquet_file = self.api.ParquetFile(path, **parquet_kwargs)
353-
354-
result = parquet_file.to_pandas(columns=columns, **kwargs)
355-
356-
if handles is not None:
357-
handles.close()
358-
return result
352+
try:
353+
parquet_file = self.api.ParquetFile(path, **parquet_kwargs)
354+
return parquet_file.to_pandas(columns=columns, **kwargs)
355+
finally:
356+
if handles is not None:
357+
handles.close()
359358

360359

361360
@doc(storage_options=_shared_docs["storage_options"])

pandas/tests/io/test_parquet.py

+8
Original file line numberDiff line numberDiff line change
@@ -1155,3 +1155,11 @@ def test_use_nullable_dtypes_not_supported(self, fp):
11551155
df.to_parquet(path)
11561156
with pytest.raises(ValueError, match="not supported for the fastparquet"):
11571157
read_parquet(path, engine="fastparquet", use_nullable_dtypes=True)
1158+
1159+
def test_close_file_handle_on_read_error(self):
1160+
with tm.ensure_clean("test.parquet") as path:
1161+
pathlib.Path(path).write_bytes(b"breakit")
1162+
with pytest.raises(Exception, match=""): # Not important which exception
1163+
read_parquet(path, engine="fastparquet")
1164+
# The next line raises an error on Windows if the file is still open
1165+
pathlib.Path(path).unlink(missing_ok=False)

0 commit comments

Comments
 (0)