fix(toolshed): honour the find() -1 sentinel when rewriting generated stub headers - #2549
Open
LeSingh1 wants to merge 1 commit into
Open
fix(toolshed): honour the find() -1 sentinel when rewriting generated stub headers#2549LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
`_normalize_stub_headers` rewrites the OS path separator in a generated
stub's first-line comment. It splits the file on the first newline:
newline = data.find(b"\n")
first_line = data[:newline] if newline != -1 else data
...
stub.write_bytes(first_line.replace(b"\\", b"/") + data[newline:])
The first slice special-cases the -1 that `bytes.find` returns when there is
no newline; the second does not. `data[-1:]` is the file's last byte, so for
a stub that is a single line with no trailing newline the rewritten header
gets a duplicate of its own final character appended:
in : b'...stubgen-pyx from cuda_core\\cuda\\core\\x.pyx'
out: b'...stubgen-pyx from cuda_core/cuda/core/x.pyxx'
A `.pyi` with no trailing newline is a shape this repo tolerates on purpose:
`.pyi` is excluded from the end-of-file-fixer hook in
.pre-commit-config.yaml, so nothing adds one. And because this wrapper runs
as a pre-commit *fixer* (the stubgen-pyx-cuda-core hook), the corrupted byte
is written back into the working tree and committed.
Slice the remainder with the same sentinel check.
Adds tests under toolshed/tests/ covering the rewrite with and without a
trailing newline, header-only stubs, and the cases that must be left
byte-identical (already-POSIX headers, hand-written stubs containing
backslashes, and empty files).
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
_normalize_stub_headersrewrites the OS path separator in a generated stub's first-line comment, so committed.pyifiles are identical across platforms. It splits the file on the first newline:The first slice special-cases the
-1thatbytes.findreturns when there is no newline. The second does not — anddata[-1:]is the file's last byte. For a stub that is a single line with no trailing newline, the rewritten header gets a duplicate of its own final character appended:Two things make this worth fixing rather than shrugging at:
.pyiwith no trailing newline is a shape this repo tolerates on purpose..pyiis in theend-of-file-fixerexclude list in.pre-commit-config.yaml, so nothing adds one.stubgen-pyx-cuda-corepre-commit hook — a fixer. The corrupted byte is written straight back into the working tree, and the nextgit committakes it.The
-1handling one line above shows the sentinel was already known about; it just was not applied to both slices.Fix
Slice the remainder with the same sentinel check:
No behavior change for any stub that has a newline, which is every stub in the tree today.
Tests
toolshed/run_stubgen_pyx.pyhad no tests. Addedtoolshed/tests/test_run_stubgen_pyx.py, which calls_normalize_stub_headers(tmp_path)directly (nostubgen-pyxbinary, no subprocess, no CUDA):\→/changed;Verification
Executed in full (pure Python, no GPU):
ruff check/ruff format --checkclean on both changed Python files. Restore done withcpaside +git show upstream/main:<path> >; index verified clean before committing.