Skip to content

fix(corekit): refuse a non-bytes value for the c format on every engine (#2015) - #2018

Merged
JarryShaw merged 2 commits into
mainfrom
fix/2015-graalpy-bytearray-c-format
Oct 21, 2026
Merged

JarryShaw merged 2 commits into
mainfrom
fix/2015-graalpy-bytearray-c-format

Conversation

@JarryShaw

@JarryShaw JarryShaw commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

What is the purpose of your pull request?

  • fix — corrects a defect

Description of your pull request and other information

GraalPy's struct packs a bytearray for the c format, but CPython's raises struct.error. Field.pack already converts a bytearray to bytes for s and p (#2004); it now does the same for c, so a bytearray packs identically on every engine. A memoryview is not converted, because CPython refuses one for c, s and p alike; it is passed straight through to struct. No library field uses c.

What the tests check:

  • test_bytearray_for_the_c_format_packs_as_bytes: a bytearray, or a subclass of one, packs the same as b'a', both under the real struct and under a stand-in that accepts buffers the way GraalPy's does. With main's field.py, it fails 4 subtests.
  • test_memoryview_for_the_c_format_is_left_to_struct: Field.pack hands the memoryview to struct unchanged.

GraalPy is not installed locally, so this was not run there. Only narrow selections were run, not make test: test_fields_bytearray_pack_2004_unit.py and test_fields_field.py pass.

Closes #2015

…ne (#2015)

GraalPy's struct packs a bytearray for the c format, where CPython's
raises struct.error, so Field.pack now refuses anything but bytes for c
itself with StructError (a struct.error subclass). The test pins that
error and adds a lenient struct stand-in that reproduces GraalPy's
behaviour on CPython.
@JarryShaw JarryShaw moved this to In review in PyPCAPKit Oct 21, 2026
@JarryShaw JarryShaw added fix Pull requests that fix a defect (fix: subject prefix) review: pending No verdict for the current head - never reviewed, or the head moved since the last one labels Oct 21, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

GOOD TO GO at 4c8ce4fb3.

A Sonnet cross-review checked every claim it set out to:

  • No library field uses a c template, and template[-1:] handles a prefixed >c.
  • StructError subclasses struct.error, so existing except struct.error handlers still catch it.
  • With main's field.py, the test file fails 6 subtests; with the fix, it gives 47 passed.
  • bytes and its subclasses still pack.

The behaviour has not been checked on GraalPy itself: the test stands in for GraalPy's struct, so the graalpy-25.4 leg will be the first real check.

@JarryShaw JarryShaw added review: good-to-go Cross-review at the current head says ready; CI state is separate and removed review: pending No verdict for the current head - never reviewed, or the head moved since the last one labels Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Coverage: 91.55% (unit tier, Python 3.15, f9fe66a76, Unit Tests run success)

Package Statements Missed Branches Partial Cover
pcapkit (top level) 104 4 20 2 95.16%
pcapkit/const 19597 881 2618 867 92.11%
pcapkit/corekit 2542 71 810 37 96.54%
pcapkit/dumpkit 276 3 100 3 98.40%
pcapkit/foundation 2930 23 1022 25 98.79%
pcapkit/interface 138 4 52 3 96.32%
pcapkit/protocols 17501 222 4612 187 98.13%
pcapkit/toolkit 872 36 298 10 95.21%
pcapkit/utilities 523 2 158 4 99.12%
pcapkit/vendor 4431 1995 1018 167 51.73%

Per-file detail: the coverage-html artifact of this run.

@JarryShaw

Copy link
Copy Markdown
Owner Author

cant we just convert like the line before?

@JarryShaw

Copy link
Copy Markdown
Owner Author

Yes. I'll change the fix so that a bytearray for the c format is converted to bytes, the same as for s and p, instead of being refused. It then packs the same on every engine. The test will pin that a bytearray packs to the same bytes as the equivalent bytes value, both under a strict struct and under one that accepts buffers, the way GraalPy's does. I'll check memoryview the same way and report what I find. Until the new head is reviewed, this PR is back at review: needs-changes.

@JarryShaw JarryShaw added review: needs-changes Cross-review at the current head says changes are required; see the verdict comment and removed review: good-to-go Cross-review at the current head says ready; CI state is separate labels Oct 21, 2026
…g it (#2015)

Field.pack now hands a bytearray for c over as bytes, as it already does
for s and p, so it packs identically under CPython's struct (which
refuses one) and GraalPy's (which takes one). The StructError refusal is
dropped. A memoryview is still passed through unconverted. Tests pin the
c packing under the real struct and a lenient stand-in.
@JarryShaw

Copy link
Copy Markdown
Owner Author

GOOD TO GO at f9fe66a76.

A Sonnet re-review confirmed:

  • The StructError refusal, its import and its Raises: entry are all gone.
  • A bytearray, or a subclass of one, for c packs as b'a'. A length that isn't 1 still raises struct.error.
  • With main's field.py, the new test fails 4 subtests. On the head, 5 passed and test_fields_field.py gave 42 passed.
  • The memoryview test checks only that the value is passed through, so it can't fail on GraalPy for an engine reason.

GraalPy itself was not run.

@JarryShaw JarryShaw added review: good-to-go Cross-review at the current head says ready; CI state is separate and removed review: needs-changes Cross-review at the current head says changes are required; see the verdict comment labels Oct 21, 2026
@JarryShaw
JarryShaw merged commit 2ab2260 into main Oct 21, 2026
57 checks passed
@JarryShaw
JarryShaw deleted the fix/2015-graalpy-bytearray-c-format branch October 21, 2026 16:04
@github-project-automation github-project-automation Bot moved this from In review to Done in PyPCAPKit Oct 21, 2026
@JarryShaw JarryShaw removed the review: good-to-go Cross-review at the current head says ready; CI state is separate label Oct 21, 2026
@JarryShaw JarryShaw added this to the PyPCAPKit v1.5 milestone Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Pull requests that fix a defect (fix: subject prefix)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

GraalPy: bytearray for the c format is not refused (test_fields_bytearray_pack_2004)

1 participant