From 156c83d2e5b00544d675d78335b249305fac44e0 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sat, 12 Mar 2022 00:44:11 +0100 Subject: [PATCH] Backport PR #46325: Regression in read csv causing segfault for invalid file input --- doc/source/whatsnew/v1.4.2.rst | 1 + pandas/io/parsers/readers.py | 4 ++++ pandas/tests/io/parser/test_unsupported.py | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/doc/source/whatsnew/v1.4.2.rst b/doc/source/whatsnew/v1.4.2.rst index badda6a73d1c8..239f02b0b6bcf 100644 --- a/doc/source/whatsnew/v1.4.2.rst +++ b/doc/source/whatsnew/v1.4.2.rst @@ -15,6 +15,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed regression in :meth:`DataFrame.drop` and :meth:`Series.drop` when :class:`Index` had extension dtype and duplicates (:issue:`45860`) +- Fixed regression in :func:`read_csv` killing python process when invalid file input was given for ``engine="c"`` (:issue:`45957`) - Fixed memory performance regression in :meth:`Series.fillna` when called on a :class:`DataFrame` column with ``inplace=True`` (:issue:`46149`) - Provided an alternative solution for passing custom Excel formats in :meth:`.Styler.to_excel`, which was a regression based on stricter CSS validation. Examples available in the documentation for :meth:`.Styler.format` (:issue:`46152`) - diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py index 577c38deb4ed4..7d0e78ce43b71 100644 --- a/pandas/io/parsers/readers.py +++ b/pandas/io/parsers/readers.py @@ -1227,6 +1227,10 @@ def _make_engine( assert self.handles is not None f = self.handles.handle + elif engine != "python": + msg = f"Invalid file path or buffer object type: {type(f)}" + raise ValueError(msg) + try: return mapping[engine](f, **self.options) except Exception: diff --git a/pandas/tests/io/parser/test_unsupported.py b/pandas/tests/io/parser/test_unsupported.py index fad5c3d785e97..e360b9a49474d 100644 --- a/pandas/tests/io/parser/test_unsupported.py +++ b/pandas/tests/io/parser/test_unsupported.py @@ -189,3 +189,13 @@ def test_close_file_handle_on_invalide_usecols(all_parsers): parser.read_csv(fname, usecols=["col1", "col2", "col3"]) # unlink fails on windows if file handles still point to it os.unlink(fname) + + +def test_invalid_file_inputs(all_parsers): + # GH#45957 + parser = all_parsers + if parser.engine == "python": + pytest.skip("Python engine supports lists.") + + with pytest.raises(ValueError, match="Invalid"): + parser.read_csv([])