Skip to content

Commit dbc28bc

Browse files
authored
fix(const): make every registry's register reject what its get rejects (#1816) (#1823)
EnumRegistry.register routes a new value through _validate_value, but only AppType overrode that hook, so Hardware.register(70000, 'x') minted a member that Hardware.get(70000) then returned, although get refuses the value. - Every vendor template emits a _validate_value override with the check it already writes at the top of _missing_, raising EnumValueError with the same message: the crawler's FLAG, or a bespoke template's own range (0-999 for StatusCode and ReturnCode, 0-9 for ResponseKind and GroupingInformation, str for the string-valued ones). TLSKeyLabel and ExtensionHeader have no _missing_, so they check the member type and the 8-bit Next Header field. - Const files re-rendered offline through each crawler's own template with member data untouched; Method.get and OptionType.register docstrings follow. - registry-protocol.rst and EnumLookup._validate_value say every registry overrides the hook; line citations the change shifts are re-pointed. - A new test walks every registry at runtime and fails on main, and pins the bounds no FLAG gives; the no-mint sweep now mints inside each range. Closes #1816
1 parent 24eb561 commit dbc28bc

143 files changed

Lines changed: 4194 additions & 22 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/registry-protocol.rst‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ generated registries spell by hand in ``_missing_``:
7474
if not (isinstance(value, int) and 0 <= value <= 0xFF):
7575
raise EnumValueError(f'{value!r} is not a valid {cls.__name__}')
7676

77+
**Every registry overrides it**, so that ``register`` refuses what ``get`` refuses
78+
(:issue:`1816`). Each vendor generator emits the override from the check it already
79+
writes at the top of ``_missing_``, with the same message. That is the ``FLAG`` of most
80+
crawlers, but a bespoke template's own range where its ``_missing_`` checks something
81+
else: ``0``-``999`` for :class:`~pcapkit.const.http.status_code.StatusCode` and
82+
:class:`~pcapkit.const.ftp.return_code.ReturnCode`, and :class:`str` for the
83+
string-valued registries.
84+
:class:`~pcapkit.const.pcapng.tls_key_label.TLSKeyLabel` and
85+
:class:`~pcapkit.const.ipv6.extension_header.ExtensionHeader` have no ``_missing_`` to
86+
copy from, because their constructor refuses every value no member carries, so the
87+
first checks its member type and the second the 8-bit Next Header field the type is
88+
sent in. The ``AppType`` transports inherit ``AppType``'s own.
89+
90+
The check is the *range*, not everything ``_missing_`` decides. A value inside the range
91+
that matches neither a member nor an unassigned range -- ``CauseCode(0)``, say -- still
92+
fails to resolve through ``get``, and ``register`` may mint it.
93+
7794
Three things about it are easy to get wrong:
7895

7996
* **It guards, it does not convert.** The return type is :obj:`None` deliberately, so

‎pcapkit/const/arp/hardware.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010

1111
"""
1212

13+
from typing import TYPE_CHECKING
14+
1315
from aenum import IntEnum
1416

1517
from pcapkit.corekit.enum import EnumRegistry
18+
from pcapkit.utilities.exceptions import EnumValueError
19+
20+
if TYPE_CHECKING:
21+
from typing import Any
1622

1723
__all__ = ['Hardware']
1824

@@ -148,6 +154,28 @@ class Hardware(EnumRegistry, IntEnum):
148154
#: Reserved [:rfc:`5494`]
149155
Reserved_65535 = 65535
150156

157+
@classmethod
158+
def _validate_value(cls, value: 'Any') -> 'None':
159+
"""Reject ``value`` unless it passes the range check :meth:`_missing_` makes.
160+
161+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
162+
with that check and the message it raises, so
163+
:meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the
164+
values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the
165+
same words (:issue:`1816`).
166+
167+
Args:
168+
value: Value about to be looked up or minted.
169+
170+
Raises:
171+
EnumValueError: If ``value`` fails that check. A
172+
:exc:`ValueError`, so an ``except ValueError`` caller is
173+
unaffected.
174+
175+
"""
176+
if not (isinstance(value, int) and 0 <= value <= 65535):
177+
raise EnumValueError('%r is not a valid %s' % (value, cls.__name__))
178+
151179
@classmethod
152180
def _missing_(cls, value: 'int') -> 'Hardware':
153181
"""Lookup function used when value is not found.

‎pcapkit/const/arp/operation.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010

1111
"""
1212

13+
from typing import TYPE_CHECKING
14+
1315
from aenum import IntEnum
1416

1517
from pcapkit.corekit.enum import EnumRegistry
18+
from pcapkit.utilities.exceptions import EnumValueError
19+
20+
if TYPE_CHECKING:
21+
from typing import Any
1622

1723
__all__ = ['Operation']
1824

@@ -101,6 +107,28 @@ class Operation(EnumRegistry, IntEnum):
101107
#: Reserved [:rfc:`5494`]
102108
Reserved_65535 = 65535
103109

110+
@classmethod
111+
def _validate_value(cls, value: 'Any') -> 'None':
112+
"""Reject ``value`` unless it passes the range check :meth:`_missing_` makes.
113+
114+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
115+
with that check and the message it raises, so
116+
:meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the
117+
values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the
118+
same words (:issue:`1816`).
119+
120+
Args:
121+
value: Value about to be looked up or minted.
122+
123+
Raises:
124+
EnumValueError: If ``value`` fails that check. A
125+
:exc:`ValueError`, so an ``except ValueError`` caller is
126+
unaffected.
127+
128+
"""
129+
if not (isinstance(value, int) and 0 <= value <= 65535):
130+
raise EnumValueError('%r is not a valid %s' % (value, cls.__name__))
131+
104132
@classmethod
105133
def _missing_(cls, value: 'int') -> 'Operation':
106134
"""Lookup function used when value is not found.

‎pcapkit/const/esp/cipher.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010

1111
"""
1212

13+
from typing import TYPE_CHECKING
14+
1315
from aenum import IntEnum
1416

1517
from pcapkit.corekit.enum import EnumRegistry
18+
from pcapkit.utilities.exceptions import EnumValueError
19+
20+
if TYPE_CHECKING:
21+
from typing import Any
1622

1723
__all__ = ['Cipher']
1824

@@ -218,6 +224,28 @@ class Cipher(EnumRegistry, IntEnum):
218224
#: Alias of :attr:`Cipher.ENCR_MAGMA_MGM_MAC_KTREE`.
219225
MAGMA_MGM_MAC_KTREE = 35
220226

227+
@classmethod
228+
def _validate_value(cls, value: 'Any') -> 'None':
229+
"""Reject ``value`` unless it passes the range check :meth:`_missing_` makes.
230+
231+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
232+
with that check and the message it raises, so
233+
:meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the
234+
values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the
235+
same words (:issue:`1816`).
236+
237+
Args:
238+
value: Value about to be looked up or minted.
239+
240+
Raises:
241+
EnumValueError: If ``value`` fails that check. A
242+
:exc:`ValueError`, so an ``except ValueError`` caller is
243+
unaffected.
244+
245+
"""
246+
if not (isinstance(value, int) and 0 <= value <= 65535):
247+
raise EnumValueError('%r is not a valid %s' % (value, cls.__name__))
248+
221249
@classmethod
222250
def _missing_(cls, value: 'int') -> 'Cipher':
223251
"""Lookup function used when value is not found.

‎pcapkit/const/esp/integrity.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010

1111
"""
1212

13+
from typing import TYPE_CHECKING
14+
1315
from aenum import IntEnum
1416

1517
from pcapkit.corekit.enum import EnumRegistry
18+
from pcapkit.utilities.exceptions import EnumValueError
19+
20+
if TYPE_CHECKING:
21+
from typing import Any
1622

1723
__all__ = ['Integrity']
1824

@@ -107,6 +113,28 @@ class Integrity(EnumRegistry, IntEnum):
107113
#: Alias of :attr:`Integrity.AUTH_HMAC_SHA2_512_256`.
108114
HMAC_SHA2_512_256 = 14
109115

116+
@classmethod
117+
def _validate_value(cls, value: 'Any') -> 'None':
118+
"""Reject ``value`` unless it passes the range check :meth:`_missing_` makes.
119+
120+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
121+
with that check and the message it raises, so
122+
:meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the
123+
values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the
124+
same words (:issue:`1816`).
125+
126+
Args:
127+
value: Value about to be looked up or minted.
128+
129+
Raises:
130+
EnumValueError: If ``value`` fails that check. A
131+
:exc:`ValueError`, so an ``except ValueError`` caller is
132+
unaffected.
133+
134+
"""
135+
if not (isinstance(value, int) and 0 <= value <= 65535):
136+
raise EnumValueError('%r is not a valid %s' % (value, cls.__name__))
137+
110138
@classmethod
111139
def _missing_(cls, value: 'int') -> 'Integrity':
112140
"""Lookup function used when value is not found.

‎pcapkit/const/ftp/command.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from pcapkit.corekit.enum import EnumLookup, EnumRegistry
1919
from pcapkit.corekit.sentinels import NO_DEFAULT
20+
from pcapkit.utilities.exceptions import EnumValueError
2021

2122
if TYPE_CHECKING:
2223
from typing import Any, Optional, Type
@@ -175,6 +176,28 @@ def get(cls, key: 'Any', default: 'Any' = NO_DEFAULT) -> 'FEATCode':
175176
return member
176177
return super().get(key, default)
177178

179+
@classmethod
180+
def _validate_value(cls, value: 'Any') -> 'None':
181+
"""Reject ``value`` unless it is a :class:`str`, as :meth:`_missing_` does.
182+
183+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
184+
with the check :meth:`_missing_` makes, and the message it raises, so
185+
:meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the
186+
values :meth:`get` refuses, in the same words (:issue:`1816`). Any
187+
:class:`str` passes, since :meth:`_missing_` resolves every one.
188+
189+
Args:
190+
value: Value about to be looked up or minted.
191+
192+
Raises:
193+
EnumValueError: If ``value`` is not a :class:`str`. A
194+
:exc:`ValueError`, so an ``except ValueError`` caller is
195+
unaffected.
196+
197+
"""
198+
if not isinstance(value, str):
199+
raise EnumValueError(f'{value!r} is not a valid {cls.__name__}')
200+
178201
@classmethod
179202
def _missing_(cls, value: 'str') -> 'FEATCode':
180203
"""Lookup function used when value is not found.
@@ -575,6 +598,28 @@ def get(key: 'str', default: 'Optional[str]' = None) -> 'Command':
575598
return Command._unregistered_member(default if default is not None else key, name)
576599
return Command[name] # type: ignore[misc]
577600

601+
@classmethod
602+
def _validate_value(cls, value: 'Any') -> 'None':
603+
"""Reject ``value`` unless it is a :class:`str`, as :meth:`_missing_` does.
604+
605+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
606+
with the check :meth:`_missing_` makes, and the message it raises, so
607+
:meth:`register` refuses a value the constructor refuses, in the same
608+
words (:issue:`1816`). Any :class:`str` passes, since
609+
:meth:`_missing_` resolves every one.
610+
611+
Args:
612+
value: Value about to be minted.
613+
614+
Raises:
615+
EnumValueError: If ``value`` is not a :class:`str`. A
616+
:exc:`ValueError`, so an ``except ValueError`` caller is
617+
unaffected.
618+
619+
"""
620+
if not isinstance(value, str):
621+
raise EnumValueError(f'{value!r} is not a valid {cls.__name__}')
622+
578623
@classmethod
579624
def _missing_(cls, value: 'str') -> 'Command':
580625
"""Lookup function used when value is not found.

‎pcapkit/const/ftp/return_code.py‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
from aenum import IntEnum
1717

1818
from pcapkit.corekit.enum import EnumRegistry
19+
from pcapkit.utilities.exceptions import EnumValueError
1920

2021
if TYPE_CHECKING:
21-
from typing import Optional, Type
22+
from typing import Any, Optional, Type
2223

2324
__all__ = ['ReturnCode']
2425

@@ -43,6 +44,29 @@ class ResponseKind(EnumRegistry, IntEnum):
4344
PermanentNegativeCompletion = 5
4445
Protected = 6
4546

47+
@classmethod
48+
def _validate_value(cls, value: 'Any') -> 'None':
49+
"""Reject ``value`` unless it is a digit, which :meth:`_missing_` accepts.
50+
51+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
52+
with the range :meth:`_missing_` resolves, and the message the
53+
constructor raises outside it, so
54+
:meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the
55+
values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the
56+
same words (:issue:`1816`).
57+
58+
Args:
59+
value: Value about to be looked up or minted.
60+
61+
Raises:
62+
EnumValueError: If ``value`` is not an :class:`int` in ``0``-``9``.
63+
A :exc:`ValueError`, so an ``except ValueError`` caller is
64+
unaffected.
65+
66+
"""
67+
if not (isinstance(value, int) and 0 <= value <= 9):
68+
raise EnumValueError('%r is not a valid %s' % (value, cls.__name__))
69+
4670
@classmethod
4771
def _missing_(cls, value: 'int') -> 'ResponseKind':
4872
"""Lookup function used when value is not found.
@@ -67,6 +91,29 @@ class GroupingInformation(EnumRegistry, IntEnum):
6791
Unspecified = 4
6892
FileSystem = 5
6993

94+
@classmethod
95+
def _validate_value(cls, value: 'Any') -> 'None':
96+
"""Reject ``value`` unless it is a digit, which :meth:`_missing_` accepts.
97+
98+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
99+
with the range :meth:`_missing_` resolves, and the message the
100+
constructor raises outside it, so
101+
:meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses exactly the
102+
values :meth:`~pcapkit.corekit.enum.EnumLookup.get` refuses, in the
103+
same words (:issue:`1816`).
104+
105+
Args:
106+
value: Value about to be looked up or minted.
107+
108+
Raises:
109+
EnumValueError: If ``value`` is not an :class:`int` in ``0``-``9``.
110+
A :exc:`ValueError`, so an ``except ValueError`` caller is
111+
unaffected.
112+
113+
"""
114+
if not (isinstance(value, int) and 0 <= value <= 9):
115+
raise EnumValueError('%r is not a valid %s' % (value, cls.__name__))
116+
70117
@classmethod
71118
def _missing_(cls, value: 'int') -> 'GroupingInformation':
72119
"""Lookup function used when value is not found.
@@ -308,6 +355,30 @@ def _unregistered_member(cls, value: 'int', name: 'str') -> 'ReturnCode':
308355
obj.group = GroupingInformation(int(code[1]))
309356
return obj
310357

358+
@classmethod
359+
def _validate_value(cls, value: 'Any') -> 'None':
360+
"""Reject ``value`` unless it is a three-digit reply code.
361+
362+
Overrides :meth:`~pcapkit.corekit.enum.EnumLookup._validate_value`
363+
with the range check :meth:`_missing_` makes, and the message it
364+
raises, so :meth:`~pcapkit.corekit.enum.EnumRegistry.register` refuses
365+
exactly the values :meth:`~pcapkit.corekit.enum.EnumLookup.get`
366+
refuses, in the same words (:issue:`1816`). That range is ``0``-``999``
367+
rather than the registry's own, since :meth:`_missing_` resolves a code
368+
outside the registry to ``Unknown`` rather than refusing it.
369+
370+
Args:
371+
value: Value about to be looked up or minted.
372+
373+
Raises:
374+
EnumValueError: If ``value`` is not an :class:`int` in
375+
``0``-``999``. A :exc:`ValueError`, so an ``except ValueError``
376+
caller is unaffected.
377+
378+
"""
379+
if not (isinstance(value, int) and 0 <= value <= 999):
380+
raise EnumValueError('%r is not a valid %s' % (value, cls.__name__))
381+
311382
@classmethod
312383
def _missing_(cls, value: 'int') -> 'ReturnCode':
313384
"""Lookup function used when value is not found.

0 commit comments

Comments
 (0)