Repository navigation
[Python] Read all compressed file data for unbounded reads - #41622
JustinBinber wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR fixes a silent data truncation bug in CompressedFile.read() where calling read() without arguments previously defaulted to returning at most DEFAULT_READ_BUFFER_SIZE (16 MiB) of decompressed data, and calling read(None) or read(-1) raised a TypeError or returned incomplete data. The implementation cleanly updates CompressedFile.read() and _fetch_to_internal_buffer() to follow standard Python io semantics for unbounded reads, clamps negative seek offsets, and includes comprehensive unit and pipeline tests.
🔍 General Feedback
- The fix accurately aligns
CompressedFile.read(num_bytes=None)with standard Python file-like object conventions (handling omitted,None, and negative sizes as read-to-EOF while preserving bounded behavior for non-negative sizes). - Clamping
absolute_offsetto>= 0inseek()is a good defensive measure and aligns with the method's documented out-of-bounds behavior. - Test coverage is thorough across all supported compression formats, edge cases (empty files, offsets, rewinds, concatenated members), and
fileio.ReadableFileintegration.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @claudevdm for label python. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
Synced the branch with current master ( Rechecked the merged tree locally: 138 tests and 55 subtests passed with one existing skip on FnApiRunner, and the separate TestStream test passed on BundleBasedDirectRunner (139 tests passed in total). YAPF 0.43.0, the current Ruff 0.17.6 / Pyrefly 1.5.0 pins, and whitespace checks pass. |
Fixes #41621.
Compressed files currently return at most 16 MiB from a default
read().fileio.ReadableFile.read()andread_utf8()each perform one such read, so larger content can be silently truncated. Explicitread(None)raises andread(-1)can return empty data before reading the source.Change the default to an unlimited read and drain the existing decompression buffer to EOF for omitted,
Noneor negative sizes. Explicit nonnegative sizes keep their bounded behavior. Clamp negative absolute seek offsets to zero before skipping, so seek never interprets a negative distance as an unlimited read. No codec-specific logic or dependencies change.Regression tests cover all five codecs, partial reads, empty streams, EOF, rewind and concatenated members. A real
ReadMatchespipeline checks both publicReadableFilereaders with a 16 MiB gzip member followed by a final row. The old code returns 16,777,216 bytes instead of 16,777,225.Validation on Windows / Python 3.11.7:
filesystem_test.pyandlocalfilesystem_test.py, plusfileio_test.py::ReadTest,WriteFilesTestandtextio_test.py::TextSinkTest: 138 passed, 1 existing skip and 55 subtests passed on FnApiRunner, excluding one TestStream test unsupported by that runner. That test passes separately on BundleBasedDirectRunner (139 passed in total).git diff --checkpass.The source checkout used generated protobuf bindings from the released 2.78.0 wheel; a fresh SDK/protobuf build, cloud execution and the full runner matrix were not run. Exploratory tests also reproduced existing zstd/lzma concatenated-frame chunk-boundary errors on the unchanged base with bounded reads; those are outside this change.
Unlimited reads use memory proportional to the remaining decompressed data. Callers needing bounded reads can continue to pass an explicit size. This does not address the separate CSV restriction-size issue in #41471.