Skip to content

fix(corekit): pack a bytearray into an s or p field on PyPy too (#2004) - #2007

Merged
JarryShaw merged 1 commit into
mainfrom
fix/2004-bytearray-pack
Oct 21, 2026
Merged

JarryShaw merged 1 commit into
mainfrom
fix/2004-bytearray-pack

Conversation

@JarryShaw

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

Field.pack now passes a bytearray to struct.pack as bytes when the format ends in s or p. PyPy refuses a bytearray there with TypeError: expected bytes, got bytearray object, and CPython accepts it. This is the only struct.pack call under corekit/fields/, so BytesField, StringField, the address fields and every wrapper go through it.

memoryview, and a bytearray for c, are left alone: CPython already refuses both, so converting them would accept something new.

test_fields_bytearray_pack_2004_unit.py uses a struct stand-in that refuses a bytearray, as PyPy does. It fails without the fix: 8 SUBFAILED on CPython, 12 on PyPy 8.0.1. The tests/corekit/test_fields_* files and the IPv6 CALIPSO make test pass on both interpreters.

Closes #2004

PyPy's struct.pack takes bytes alone for the s and p formats, where
CPython also takes a bytearray, so Field.pack raised TypeError on PyPy for
a bytearray value. Field.pack now hands struct.pack a bytes copy for those
formats. CPython output is unchanged, and a memoryview, or a bytearray for
the c format, stays refused as CPython refuses it.
@JarryShaw JarryShaw added fix Pull requests that fix a defect (fix: subject prefix) review: running A cross-review is in flight against the current head - no verdict yet labels Oct 20, 2026
@JarryShaw JarryShaw moved this to In review in PyPCAPKit Oct 20, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

GOOD TO GO at e1cb15d2c. A Sonnet review checked the conversion:

  • Every template under corekit/fields/ is a single-code string, and no field uses p. A multi-code template would end in another letter and be left as is.
  • Only a bytearray bound for s/p is converted; memoryview, bool, str, list and a bytearray for c still raise.
  • With field.py reverted, the new test fails (8 failed), with the struct stand-in patched where Field.pack reads it.

I ran all 22 tests/corekit/test_fields_* files, which pass. PaddingField.pack, the one override that skips Field.pack, already converts to bytes itself.

@JarryShaw JarryShaw added review: good-to-go Cross-review at the current head says ready; CI state is separate and removed review: running A cross-review is in flight against the current head - no verdict yet labels Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage: 91.54% (unit tier, Python 3.15, e1cb15d2c, 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 2929 23 1020 25 98.78%
pcapkit/interface 138 4 52 3 96.32%
pcapkit/protocols 17468 222 4600 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
JarryShaw merged commit 2998f39 into main Oct 21, 2026
60 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in PyPCAPKit Oct 21, 2026
@JarryShaw
JarryShaw deleted the fix/2004-bytearray-pack branch October 21, 2026 00:56
@JarryShaw JarryShaw removed the review: good-to-go Cross-review at the current head says ready; CI state is separate label Oct 21, 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.

fix(corekit): a bytearray value fails to pack into an s field on PyPy

1 participant