Skip to content

Commit 22fb952

Browse files
committed
refactor(protocols)!: make() takes an enum member or an int, with no keyword groups (#2594)
- _make_index: takes (value, enumeration, bits=, name=), checks the code as _make_uint does and against the enumeration's own range, and returns the member get resolves (a code in range no member holds is written as given); the frame inspection, _make_index_namespace and _make_index_calls go. - Protocol.resolve_index: public helper taking the old name/default/ namespace/reversed group explicitly, for a caller holding a name. - Every make() and _make_* maker: the groups are removed with no shim, each enumeration field is typed Enum | int and resolved by _make_index; a name, a wide bool or a code its field cannot carry raises ProtocolError (#2617, #2676's registry-code bullet). Also SCTP, DCCP, NGAP, IGMPv3, IPv6_Ext and the transport ports, which took codes without a group. - Docstrings: versionchanged/versionremoved on every class that shipped the keywords; pep.rst records the item done and registry-protocol.rst the rule. - Tests: a guard against the groups and frame inspection, refusal tables per family that fail on main, and the name-inference tests rewritten for the new contract. Every tests/ leg matches main (project halves: same environment failures).
1 parent dd30f0f commit 22fb952

82 files changed

Lines changed: 4312 additions & 2570 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/source/contributing/conventions/index.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ House Conventions
1414
questions have mostly arisen;
1515
* :ref:`sentinel-convention` -- :mod:`pcapkit.corekit`;
1616
* :ref:`registry-protocol` -- :mod:`pcapkit.const` again, the registration
17-
wrappers of :mod:`pcapkit.foundation.registry`, and the value a protocol's
17+
wrappers of :mod:`pcapkit.foundation.registry`, how a ``make()`` field holding
18+
an enumeration resolves, and the value a protocol's
1819
:meth:`~pcapkit.protocols.protocol.Protocol.__index__` returns;
1920
* :ref:`extension-header-subclassing` -- a protocol class hierarchy;
2021
* :ref:`protocol-family-grouping` -- how a protocol family's base and members

‎docs/source/contributing/conventions/registry-protocol.rst‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,40 @@ above, and each type's layout is a
878878
:class:`~pcapkit.protocols.schema.link.radiotap.RadiotapField` subclass, whose fields
879879
are the type's subfields and whose ``__align__`` is its alignment.
880880

881+
.. _registry-protocol-make-fields:
882+
883+
Enumeration Fields of ``make()``
884+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
885+
886+
**A** ``make()`` **field that holds an enumeration takes the member or an**
887+
:class:`int` **code, resolved with the enumeration's**
888+
:meth:`~pcapkit.corekit.enum.EnumLookup.get`. Ruled in review of :pr:`2593`
889+
(:issue:`2489`), for every ``make()`` and every ``_make_*`` maker. Nothing inside
890+
``make()`` turns a :obj:`str` into a member: a name is refused, and a caller holding
891+
one resolves it first with :meth:`Protocol.resolve_index
892+
<pcapkit.protocols.protocol.Protocol.resolve_index>`, which takes the name, a
893+
default, the namespace and whether a :obj:`dict` namespace maps names to codes. The
894+
``X_default``, ``X_namespace`` and ``X_reversed`` keywords that went with each such
895+
field were removed with no deprecation and no shim, ruled on :issue:`2594`.
896+
897+
* **Resolve through** :meth:`~pcapkit.protocols.protocol.Protocol._make_index`, giving
898+
the enumeration and the field's width in bits. It checks the code as
899+
``_make_uint()`` checks an integer -- a :obj:`bool` passes only for a one-bit
900+
field, and a negative or too wide code is refused -- and then against the
901+
enumeration's own range, its
902+
:meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`. Each refusal is
903+
:exc:`~pcapkit.utilities.exceptions.ProtocolError`, never a masked or wrapped
904+
code, by :issue:`2617`'s ruling. It returns the member ``get`` resolves, or, for
905+
a code in range that no member holds, the code itself, so that an unassigned
906+
value a parse kept rebuilds as it was captured.
907+
* **The enumeration is named at the call**, never read from the caller's frame or
908+
from its arguments' defaults: that inference is what :issue:`2356` and
909+
:issue:`2394` broke.
910+
911+
:file:`tests/project/test_make_keyword_groups_2594_unit.py` fails on a maker taking
912+
an ``X_default``, ``X_namespace`` or ``X_reversed`` parameter, on a ``_make_index``
913+
call without the field's width, and on the resolver reaching a frame.
914+
881915
.. _registry-protocol-index:
882916

883917
The Registry Index

‎docs/source/contributing/pep.rst‎

Lines changed: 62 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -987,70 +987,64 @@ since ESP has already laid the groundwork.
987987
Deterministic Construction Arguments
988988
------------------------------------
989989

990-
This is meant to open a broader run of proposals for the construction
991-
(``make()``) workflow in general rather than stand alone -- more will be added
992-
under that heading as they get scoped, and the concrete item ready to write
993-
down now is making a ``make()`` call's own argument list deterministic.
994-
995-
:meth:`~pcapkit.protocols.protocol.Protocol._make_index` is where that is not
996-
currently true. A number of ``make()`` methods accept a plain ``str``
997-
enum-member name for a field and resolve it by frame-introspecting the
998-
caller's locals for a conventional four-argument group -- ``X``,
999-
``X_default``, ``X_namespace`` and ``X_reversed`` -- looking for the one
1000-
argument whose own default is an enum member, and taking that member's class
1001-
as the namespace to search. The actual contract of a ``make()`` call is
1002-
therefore not fully visible from its own signature: knowing what a bare
1003-
string resolves against means knowing the convention and trusting that every
1004-
call site followed it exactly, rather than reading it off the call itself.
1005-
That gap is exactly how this round's bug family happened. :issue:`2356` is
1006-
the walk coming up empty because the relevant argument's own default is
1007-
``None``, leaving nothing to anchor a namespace to; :issue:`2394` is the same
1008-
inference failing by a different route -- the name is a loop variable over a
1009-
*list* parameter rather than a scalar argument, so the walk's
1010-
``isinstance(value, str)`` check can never see it. Both were fixed narrowly,
1011-
by naming the correct enum explicitly at each affected call site rather than
1012-
touching the inference, so the mechanism is exactly as fragile as it was
1013-
before and just as liable to the same shape of defect at the next call site
1014-
that does not fit its assumptions precisely.
1015-
1016-
The owner's proposed direction -- recorded here, and explicitly **not**
1017-
scheduled for 1.5.0 -- removes the inference rather than patching it again.
1018-
``make()`` would require an actual enum value, or an ``int`` where one is
1019-
already accepted, for these fields, with no implicit string-to-enum
1020-
resolution performed inside ``make()`` itself. In its place, a small public
1021-
wrapper would take the same four-argument group explicitly -- name, default,
1022-
namespace, reversed -- and *resolve* a string name to its enum member on
1023-
request, so a caller holding a bare string calls that helper first and passes
1024-
the resolved value into ``make()``. That mirrors how a finalised
1025-
:class:`~pcapkit.corekit.infoclass.Info` or
1026-
:class:`~pcapkit.protocols.schema.schema.Schema` already settles its
1027-
constructor's keyword set once, when
1028-
:func:`~pcapkit.corekit.infoclass.info_final` or
1029-
:func:`~pcapkit.protocols.schema.schema.schema_final` is applied at
1030-
class-definition time, rather than inferring it afresh at every call --
990+
This opens a broader run of proposals for the construction (``make()``)
991+
workflow in general -- more go under this heading as they get scoped -- and
992+
its first item, making a ``make()`` call's own argument list deterministic, is
993+
**done for 1.6.0** (:issue:`2594`).
994+
995+
Before 1.6.0, :meth:`~pcapkit.protocols.protocol.Protocol._make_index` resolved
996+
a plain ``str`` enum-member name given to a ``make()`` field by inspecting the
997+
caller's frame for a conventional four-argument group -- ``X``, ``X_default``,
998+
``X_namespace`` and ``X_reversed`` -- looking for the one argument whose own
999+
default is an enum member, and taking that member's class as the namespace to
1000+
search. The contract of a ``make()`` call was therefore not visible from its
1001+
own signature: knowing what a bare string resolved against meant knowing the
1002+
convention and trusting that every call site followed it exactly. That gap is
1003+
how a bug family happened. :issue:`2356` is the walk coming up empty because
1004+
the argument's own default is ``None``, leaving nothing to anchor a namespace
1005+
to; :issue:`2394` is the same inference failing by a different route -- the
1006+
name is a loop variable over a *list* parameter, so the walk's
1007+
``isinstance(value, str)`` check can never see it. Both were fixed narrowly, by
1008+
naming the enum at each affected call site, which left the mechanism as
1009+
fragile as before.
1010+
1011+
The owner removed the inference rather than patching it again, ruled in review
1012+
of :pr:`2593` (:issue:`2489`): a field holding an enumeration takes the member
1013+
or an ``int``, resolved with the enumeration's
1014+
:meth:`~pcapkit.corekit.enum.EnumLookup.get`, and nothing inside ``make()``
1015+
turns a string into a member. That settles a call's contract at the signature,
1016+
the way :func:`~pcapkit.corekit.infoclass.info_final` and
1017+
:func:`~pcapkit.protocols.schema.schema.schema_final` settle a constructor's
1018+
keyword set once at class-definition time, and
10311019
:attr:`~pcapkit.protocols.protocol.Protocol.__own_make_keywords__`
1032-
(:issue:`2286`) settles the same kind of question explicitly rather than
1033-
inferring it, though for a different question: which keywords a ``make()``
1034-
override owns, not what the names passed to it resolve against.
1035-
1036-
The trade-off is real and worth stating rather than glossed over. Accepting a
1037-
bare string for these fields is a pattern used throughout the docs and the
1038-
examples, so dropping the inference is a likely-breaking change at every
1039-
``make()`` call site that currently relies on it -- not a one-line fix.
1040-
Deciding that scope -- which call sites change, whether string acceptance is
1041-
dropped outright or only the silent-inference path behind it, and what the
1042-
wrapper is named and where it lives -- is itself design work, which is
1043-
exactly why this belongs on this page rather than as a quick issue. Done, it
1044-
removes the root cause of the :issue:`2356`/:issue:`2394` family rather than
1045-
leaving its next instance to be found and patched the same way.
1046-
1047-
**Scoped for 1.6.0.** The owner took this direction for every ``make()``, ruled
1048-
in review of :pr:`2593` (:issue:`2489`): a field holding an enum takes the member
1049-
or an ``int``, resolved with the enumeration's ``get`` rather than inferred from
1050-
the caller's frame. The shipped ``X_default``, ``X_namespace`` and ``X_reversed``
1051-
keywords go in 1.6.0 with no deprecation period, ruled on :issue:`2594`, which
1052-
carries the change across :file:`pcapkit/protocols/` in one pass and leaves the
1053-
helper's name and module to it.
1020+
(:issue:`2286`) which keywords a ``make()`` override owns. The pieces:
1021+
1022+
* :meth:`~pcapkit.protocols.protocol.Protocol._make_index` takes the value, the
1023+
enumeration and the field's width, and returns the member ``get`` resolves.
1024+
A name, a :obj:`bool` for a field wider than one bit, and a code the field or
1025+
the enumeration cannot carry raise
1026+
:exc:`~pcapkit.utilities.exceptions.ProtocolError`, so every caller gets the
1027+
range check :issue:`2617` asks for, with no masking.
1028+
* The shipped ``X_default``, ``X_namespace`` and ``X_reversed`` keywords are
1029+
removed in 1.6.0 with no deprecation period and no shim, ruled on
1030+
:issue:`2594`; each class that shipped them records them under
1031+
``versionremoved``.
1032+
* The public wrapper is :meth:`Protocol.resolve_index
1033+
<pcapkit.protocols.protocol.Protocol.resolve_index>`, which takes the old
1034+
group explicitly -- name, default, namespace, reversed -- so a caller holding
1035+
a bare string resolves it first and passes the result to ``make()``. It is a
1036+
static method every protocol inherits, beside
1037+
:meth:`~pcapkit.protocols.protocol.Protocol.decode` and
1038+
:meth:`~pcapkit.protocols.protocol.Protocol.unquote`, rather than a module of
1039+
its own, since it serves ``make()`` alone.
1040+
1041+
The trade-off was real: a bare string for these fields was a pattern used
1042+
throughout the docs and the examples, so the change breaks every ``make()``
1043+
call site that relied on the inference. That is why it landed as one change
1044+
across :file:`pcapkit/protocols/` rather than folded into the next defect fix
1045+
on this family, and it removes the root cause of the
1046+
:issue:`2356`/:issue:`2394` family rather than leaving its next instance to be
1047+
found and patched the same way.
10541048

10551049
Release Plan — 1.5.0 in Two Steps
10561050
---------------------------------
@@ -1383,10 +1377,9 @@ as are IGMP's and GRE's (:issue:`2618`).
13831377
the parsing and the constructing path, rather than a back-reference from a
13841378
parsed protocol to its parent.
13851379

1386-
**Scoped for 1.6.0 — deterministic construction arguments** (:issue:`2594`). See
1380+
**Deterministic construction arguments — done** (:issue:`2594`). See
13871381
`Deterministic Construction Arguments`_ above: every ``make()`` takes an enum
1388-
field as the member or an ``int``, dropping the implicit string-to-enum inference
1389-
and the ``X_default``, ``X_namespace`` and ``X_reversed`` keywords behind it, and
1390-
an explicit helper resolves a bare string. That breaks every call site relying on
1391-
the inference, so it lands as one change rather than folded into the next defect
1392-
fix on this family.
1382+
field as the member or an ``int``, with no string-to-enum inference and no
1383+
``X_default``, ``X_namespace`` and ``X_reversed`` keywords behind it, and
1384+
:meth:`Protocol.resolve_index <pcapkit.protocols.protocol.Protocol.resolve_index>`
1385+
resolves a bare string first.

‎docs/source/pcapkit/protocols/protocol.rst‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Internal Definitions below.
5858

5959
.. automethod:: decode
6060
.. automethod:: unquote
61+
.. automethod:: resolve_index
6162

6263
.. automethod:: expand_comp
6364

@@ -194,6 +195,7 @@ Internal Definitions
194195
pack
195196
decode
196197
unquote
198+
resolve_index
197199
expand_comp
198200
_read_packet
199201
_get_payload
@@ -225,8 +227,9 @@ Internal Definitions
225227
:meth:`~Protocol.from_data`, :meth:`~Protocol.read`,
226228
:meth:`~Protocol.make`, :meth:`~Protocol.unpack`, :meth:`~Protocol.pack`,
227229
:meth:`~Protocol.decode`, :meth:`~Protocol.unquote`,
228-
:meth:`~Protocol.expand_comp`, :meth:`~Protocol._read_packet`,
229-
:meth:`~Protocol._get_payload`, :meth:`~Protocol._make_data`,
230+
:meth:`~Protocol.resolve_index`, :meth:`~Protocol.expand_comp`,
231+
:meth:`~Protocol._read_packet`, :meth:`~Protocol._get_payload`,
232+
:meth:`~Protocol._make_data`,
230233
:meth:`~Protocol._make_index`, :meth:`~Protocol._make_payload`,
231234
:meth:`~Protocol._packing_target`, :meth:`~Protocol._read_mac_addr`,
232235
:meth:`~Protocol._make_mac_addr`,

‎examples/generators/dispatch.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,11 @@ def _linux_sll_payload(arphrd_type: 'Any') -> 'tuple[int, bytes]':
325325
from pcapkit.protocols.link.gre import GRE
326326
return 0x0800, bytes(GRE(protocol_type=0x0800, payload=_ip4(6, _tcp(9999))))
327327
if arphrd_type == ARPHRD.IEEE80211_RADIOTAP:
328+
from pcapkit.const.ieee802_11.control_subtype import ControlSubtype
329+
from pcapkit.const.ieee802_11.type import Type
328330
from pcapkit.protocols.link.ieee802_11 import IEEE802_11
329331
from pcapkit.protocols.link.radiotap import Radiotap
330-
ack = bytes(IEEE802_11(type='Control', subtype='ACK', addr1='02:00:00:00:00:01'))
332+
ack = bytes(IEEE802_11(type=Type.Control, subtype=ControlSubtype.ACK, addr1='02:00:00:00:00:01'))
331333
return 0, bytes(Radiotap(payload=ack))
332334
if arphrd_type == ARPHRD.NETLINK:
333335
from pcapkit.protocols.link.netlink import Netlink

‎examples/generators/dispatch_families/linktype.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ def _payload(code: 'Any') -> 'tuple[bytes, bool]':
6767

6868
def _wlan_ack() -> 'bytes':
6969
"""An 802.11 Ack frame, which carries no body."""
70+
from pcapkit.const.ieee802_11.control_subtype import ControlSubtype
71+
from pcapkit.const.ieee802_11.type import Type
7072
from pcapkit.protocols.link.ieee802_11 import IEEE802_11
71-
return bytes(IEEE802_11(type='Control', subtype='ACK', addr1='02:00:00:00:00:01'))
73+
return bytes(IEEE802_11(type=Type.Control, subtype=ControlSubtype.ACK, addr1='02:00:00:00:00:01'))
7274

7375

7476
def _radiotap(payload: 'bytes') -> 'bytes':

‎examples/generators/dispatch_families/radiotap.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def _build(code: 'Any') -> 'Any':
3030
case.
3131
3232
"""
33+
from pcapkit.const.ieee802_11.control_subtype import ControlSubtype
34+
from pcapkit.const.ieee802_11.type import Type
3335
from pcapkit.const.linux.arphrd import ARPHRD
3436
from pcapkit.const.reg.linktype import LinkType
3537
from pcapkit.protocols.link.ieee802_11 import IEEE802_11
@@ -38,7 +40,7 @@ def _build(code: 'Any') -> 'Any':
3840

3941
if code != LinkType.IEEE802_11:
4042
raise ValueError(f'no radiotap case for link type {code!r}')
41-
ack = bytes(IEEE802_11(type='Control', subtype='ACK', addr1='02:00:00:00:00:01'))
43+
ack = bytes(IEEE802_11(type=Type.Control, subtype=ControlSubtype.ACK, addr1='02:00:00:00:00:01'))
4244
frame = bytes(LinuxSLL1(arphrd_type=ARPHRD.IEEE80211_RADIOTAP, payload=bytes(Radiotap(payload=ack))))
4345
return generator._extract_first(generator._pcap_bytes((frame,), linktype=int(LinkType.LINUX_SLL)),
4446
'.pcap')

‎pcapkit/dumpkit/pcap.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import sys
1515
from typing import TYPE_CHECKING
1616

17+
from pcapkit.const.reg.linktype import LinkType as Enum_LinkType
1718
from pcapkit.dumpkit.common import DumperBase
1819
from pcapkit.protocols.data.misc.pcap.header import Header as Data_Header
1920
from pcapkit.protocols.misc.pcap.header import Header
@@ -26,7 +27,6 @@
2627
from aenum import IntEnum as AenumIntEnum
2728
from typing_extensions import Literal
2829

29-
from pcapkit.const.reg.linktype import LinkType as Enum_LinkType
3030
from pcapkit.corekit.version import VersionInfo
3131
from pcapkit.protocols.data.misc.pcap.frame import Frame as Data_Frame
3232
from pcapkit.protocols.data.misc.pcap.frame import FrameInfo as Data_FrameInfo
@@ -178,7 +178,9 @@ def _dump_header(self, *, protocol: 'Enum_LinkType | StdlibIntEnum | AenumIntEnu
178178
179179
"""
180180
header = Header(
181-
network=protocol,
181+
# NOTE: Header.make takes a member or a code, so a name is
182+
# resolved here, as this method has always taken one (#2594).
183+
network=Header.resolve_index(protocol, namespace=Enum_LinkType),
182184
byteorder=byteorder,
183185
nanosecond=nanosecond,
184186
version=version,

0 commit comments

Comments
 (0)