Skip to content

FIX # 45957 issue read_csv with empty list #46033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ def __next__(self):

def _make_engine(
self,
f: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str] | list | IO,
f: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str] | IO,
engine: CSVEngine = "c",
):
mapping: dict[str, type[ParserBase]] = {
Expand All @@ -1202,28 +1202,30 @@ def _make_engine(
raise ValueError(
f"Unknown engine: {engine} (valid options are {mapping.keys()})"
)
if not isinstance(f, list):
# open file here
is_text = True
mode = "r"
if engine == "pyarrow":
is_text = False
mode = "rb"
# error: No overload variant of "get_handle" matches argument types
# "Union[str, PathLike[str], ReadCsvBuffer[bytes], ReadCsvBuffer[str]]"
# , "str", "bool", "Any", "Any", "Any", "Any", "Any"
self.handles = get_handle( # type: ignore[call-overload]
f,
mode,
encoding=self.options.get("encoding", None),
compression=self.options.get("compression", None),
memory_map=self.options.get("memory_map", False),
is_text=is_text,
errors=self.options.get("encoding_errors", "strict"),
storage_options=self.options.get("storage_options", None),
)
assert self.handles is not None
f = self.handles.handle
# fixing #45957 issue
#Passing an empty list to read_csv causes segmentation fault
#if not isinstance(f, list):
# open file here
is_text = True
mode = "r"
if engine == "pyarrow":
is_text = False
mode = "rb"
# error: No overload variant of "get_handle" matches argument types
# "Union[str, PathLike[str], ReadCsvBuffer[bytes], ReadCsvBuffer[str]]"
# , "str", "bool", "Any", "Any", "Any", "Any", "Any"
self.handles = get_handle( # type: ignore[call-overload]
f,
mode,
encoding=self.options.get("encoding", None),
compression=self.options.get("compression", None),
memory_map=self.options.get("memory_map", False),
is_text=is_text,
errors=self.options.get("encoding_errors", "strict"),
storage_options=self.options.get("storage_options", None),
)
assert self.handles is not None
f = self.handles.handle

try:
return mapping[engine](f, **self.options)
Expand Down