Skip to content

Commit

Permalink
Fix nonstandard encoding handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Sep 18, 2024
1 parent dfb7544 commit be6f61f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/multicsv/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def end_section() -> None:
if not line:
break

current_position = previous_position + len(line)
current_position = self._file.tell()

if line.endswith("\n"):
line = line[:-1]
Expand Down
17 changes: 17 additions & 0 deletions tests/test_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import io
import pytest
import csv
from pathlib import Path
from typing import TextIO
from multicsv.file import MultiCSVFile
from multicsv.exceptions import SectionNotFound, CSVFileBaseIOClosed, OpOnClosedCSVFileError
Expand Down Expand Up @@ -230,3 +232,18 @@ def test_op_on_closed_via_context(simple_csv):

with pytest.raises(OpOnClosedCSVFileError):
csv_file["section1"]


def test_open_nonpython_encoding(tmp_path: Path):

csv_content: bytes \
= b"[section1]\r\na,b,c\r\n1,2,3\r\n[section2]\r\nd,e,f\r\n4,5,6\r\n"

temp_file = tmp_path / "file1.csv"
with open(temp_file, "wb") as fd:
fd.write(csv_content)

with MultiCSVFile(temp_file.open()) as csv_file:
datasection = csv_file["section2"]
csvdatasection = csv.DictReader(datasection)
assert csvdatasection.fieldnames == ['d', 'e', 'f']

0 comments on commit be6f61f

Please sign in to comment.