Skip to content

fix newline stripping in plain text readers #174

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
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions test/test_datapipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,13 @@ def dict_content_test_helper(iterator):

def test_line_reader_iterdatapipe(self) -> None:
text1 = "Line1\nLine2"
text2 = "Line2,1\nLine2,2\nLine2,3"
text2 = "Line2,1\r\nLine2,2\r\nLine2,3"

# Functional Test: read lines correctly
source_dp = IterableWrapper([("file1", io.StringIO(text1)), ("file2", io.StringIO(text2))])
line_reader_dp = source_dp.readlines()
expected_result = [("file1", line) for line in text1.split("\n")] + [
("file2", line) for line in text2.split("\n")
expected_result = [("file1", line) for line in text1.splitlines()] + [
("file2", line) for line in text2.splitlines()
]
self.assertEqual(expected_result, list(line_reader_dp))

Expand All @@ -396,8 +396,8 @@ def test_line_reader_iterdatapipe(self) -> None:
[("file1", io.BytesIO(text1.encode("utf-8"))), ("file2", io.BytesIO(text2.encode("utf-8")))]
)
line_reader_dp = source_dp.readlines()
expected_result_bytes = [("file1", line.encode("utf-8")) for line in text1.split("\n")] + [
("file2", line.encode("utf-8")) for line in text2.split("\n")
expected_result_bytes = [("file1", line.encode("utf-8")) for line in text1.splitlines()] + [
("file2", line.encode("utf-8")) for line in text2.splitlines()
]
self.assertEqual(expected_result_bytes, list(line_reader_dp))

Expand All @@ -407,8 +407,8 @@ def test_line_reader_iterdatapipe(self) -> None:
expected_result = [
("file1", "Line1\n"),
("file1", "Line2"),
("file2", "Line2,1\n"),
("file2", "Line2,2\n"),
("file2", "Line2,1\r\n"),
("file2", "Line2,2\r\n"),
("file2", "Line2,3"),
]
self.assertEqual(expected_result, list(line_reader_dp))
Expand Down
4 changes: 2 additions & 2 deletions torchdata/datapipes/iter/util/plain_text_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def strip_newline(self, stream: Union[Iterator[bytes], Iterator[str]]) -> Union[

for line in stream:
if isinstance(line, str):
yield line.strip("\n")
yield line.strip("\r\n")
else:
yield line.strip(b"\n")
yield line.strip(b"\r\n")

def decode(self, stream: Union[Iterator[bytes], Iterator[str]]) -> Union[Iterator[bytes], Iterator[str]]:
if not self._decode:
Expand Down