Commit 0728cf6
committed
fix(toolshed): honour the find() -1 sentinel when rewriting stub headers
`_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).1 parent 3bd069a commit 0728cf6
3 files changed
Lines changed: 68 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
33 | 37 | | |
| 38 | + | |
34 | 39 | | |
35 | 40 | | |
36 | | - | |
| 41 | + | |
37 | 42 | | |
38 | 43 | | |
39 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
0 commit comments