Skip to content

Interpolate compression reader error messages in the CFFI backend - #368

Open
CLCK0622 wants to merge 1 commit into
indygreg:mainfrom
CLCK0622:fix-cffi-compression-reader-error-formatting
Open

CLCK0622 wants to merge 1 commit into
indygreg:mainfrom
CLCK0622:fix-cffi-compression-reader-error-formatting

Conversation

@CLCK0622

Copy link
Copy Markdown

Interpolate the three error messages in the CFFI compression reader before passing them to ZstdError, matching the C extension and the existing read1()/readinto1() end-of-stream paths. Passing the format string and error text as separate exception arguments leaves %s unexpanded and renders a tuple instead of the intended message. Thanks to @devdanzin for reporting this in the message-formatting part of #301. This change addresses that part of the report; the lifecycle and parameter-validation topics remain outside its scope.

Reproducer (run once with PYTHON_ZSTANDARD_IMPORT_POLICY=cffi and once with PYTHON_ZSTANDARD_IMPORT_POLICY=cext):

import io
import zstandard

print("backend:", zstandard.backend)
for size in (2, 42):
    with zstandard.ZstdCompressor().stream_reader(
        io.BytesIO(b"foobar"), size=size
    ) as reader:
        try:
            reader.read(10)
        except zstandard.ZstdError as exc:
            print(size, type(exc), repr(exc.args), str(exc))

Observed output on Python 3.12.13 with Zstandard 1.5.7 (exc.args; str(exc)):

Backend Declared size Before After
cffi 2 ('zstd compress error: %s', 'Src size is incorrect'); ('zstd compress error: %s', 'Src size is incorrect') ('zstd compress error: Src size is incorrect',); zstd compress error: Src size is incorrect
cext 2 ('zstd compress error: Src size is incorrect',); zstd compress error: Src size is incorrect Same
cffi 42 ('error ending compression stream: %s', 'Src size is incorrect'); ('error ending compression stream: %s', 'Src size is incorrect') ('error ending compression stream: Src size is incorrect',); error ending compression stream: Src size is incorrect
cext 42 ('error ending compression stream: Src size is incorrect',); error ending compression stream: Src size is incorrect Same

The regression test uses a fresh reader for each of read, readinto, read1, and readinto1, with both under-declared and over-declared source sizes. It checks a single exception argument, interpolation, and exact messages shared by the two backends. Size 2 reaches _compress_into_buffer() through all four methods; size 42 reaches each method's end-of-stream path. All three changed raises are exercised. On the original CFFI code, six of the eight subtests fail; the two already formatted end-of-stream methods pass. All eight pass with the fix and with the C extension.

Validation on macOS arm64 / Python 3.12.13, with each backend explicitly selected:

Default full suite Original Patched
CFFI 224 passed, 68 skipped 225 passed, 68 skipped, 8 subtests passed
C extension 248 passed, 44 skipped 249 passed, 44 skipped, 8 subtests passed

Dependencies were installed from uv.lock; both native backends were built with ZSTD_WARNINGS_AS_ERRORS=1. These are the complete default suites; optional slow/fuzz tests were not enabled. Ruff 0.17.2 passes for the repository, and both changed files pass its format check. The full CI platform matrix has not been run locally.

AI disclosure: Codex assisted with part of the code and this description.

Copilot AI balanced review requested due to automatic review settings October 24, 2026 15:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

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.

2 participants