Skip to content

fix: read lines as bytes in ReadableFile.readline and readlines - #832

Open
jayhemnani9910 wants to merge 2 commits into
developmentseed:mainfrom
jayhemnani9910:fix/readline-bytes
Open

jayhemnani9910 wants to merge 2 commits into
developmentseed:mainfrom
jayhemnani9910:fix/readline-bytes

Conversation

@jayhemnani9910

Copy link
Copy Markdown

readline() and readlines() read into a String, so any line that is not valid UTF-8 raises:

obstore.put(store, "l1.csv", "name;city\nJosé;Málaga\n".encode("latin-1"))
obstore.open_reader(store, "l1.csv").readline()
# OSError: stream did not contain valid UTF-8

Both methods return Bytes, as the type stubs say, so they should work on any bytes. Latin-1 and cp1252 CSVs or logs, and binary files with newlines, all fail today. read() on the same file works. The fsspec layer uses this reader, so iterating over a file opened with FsspecStore fails the same way.

readlines() also pushes the line before checking for EOF, so it always ends with an extra b'', and a JSONL loop over the result breaks on the last item.

Both now use read_until(b'\n', ...) into a Vec<u8>, and readlines() stops at EOF before adding a line. The same applies to the hint branch.

Testing: added test_readline_non_utf8 and test_readlines_no_trailing_empty_line to tests/test_buffered.py. Both fail on main and pass with the change. pytest tests passes (474 passed, 2 xfailed), and cargo fmt --check and clippy show nothing new in buffered.rs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant