Skip to content

Commit

Permalink
fix polars.read_csv inadvertently closing BytesIO input buffers (#2704)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Feb 20, 2022
1 parent a40dd98 commit 3230bfb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def managed_file(file: Any) -> Iterator[Any]:
if isinstance(file, StringIO):
return BytesIO(file.read().encode("utf8"))
if isinstance(file, BytesIO):
return file
return managed_file(file)
if isinstance(file, Path):
return managed_file(str(file))
if isinstance(file, str):
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ def test_read_csv_columns_argument(
assert df.columns == col_out


def test_read_csv_buffer_ownership() -> None:
buf = io.BytesIO(b"\xf0\x9f\x98\x80,5.55,333\n\xf0\x9f\x98\x86,-5.0,666")
df = pl.read_csv(
buf,
has_header=False,
new_columns=["emoji", "flt", "int"],
)
# confirm that read_csv succeeded, and didn't close the input buffer (#2696)
assert df.shape == (2, 3)
assert not buf.closed


def test_column_rename_and_dtype_overwrite() -> None:
csv = """
a,b,c
Expand Down

0 comments on commit 3230bfb

Please sign in to comment.