Skip to content

Commit 1545899

Browse files
authored
fix(const): AppType.get takes EnumLookup.get's default and raises EnumValueError (#2545) (#2568)
1 parent fce0021 commit 1545899

5 files changed

Lines changed: 298 additions & 97 deletions

File tree

‎docs/source/pcapkit/const/reg.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ registry is one enumeration per transport protocol sharing a memberless base.
105105
:class:`int` to its port number, and compares, orders and hashes as that
106106
port.
107107

108+
.. versionchanged:: 1.6.0
109+
110+
:meth:`AppType.get() <pcapkit.const.reg.apptype.apptype.AppType.get>` takes
111+
the ``default`` of :meth:`~pcapkit.corekit.enum.EnumLookup.get`, and a miss
112+
raises :exc:`~pcapkit.utilities.exceptions.EnumValueError`, still a
113+
:exc:`ValueError`, rather than a builtin one (:issue:`2545`).
114+
108115
.. autoclass:: pcapkit.const.reg.apptype.apptype.AppType
109116
:members:
110117
:undoc-members:

‎pcapkit/const/reg/apptype/apptype.py‎

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ def get(cls, key: 'int | str', default: 'Any' = NO_DEFAULT) -> 'TransportProtoco
9999
name miss reaches the caller as
100100
:exc:`~pcapkit.utilities.exceptions.EnumKeyError` from the base.
101101
:class:`AppType` and its per-transport registries raise
102-
:exc:`ValueError` from their own ``get()`` because it takes an
103-
:class:`int` port and never accepts a name; that is not a name-miss
104-
policy, so it sets none for this class to follow. The
102+
:exc:`~pcapkit.utilities.exceptions.EnumValueError` from their own
103+
``get()`` because it takes an :class:`int` port and never accepts a
104+
name; that is not a name-miss policy, so it sets none for this class
105+
to follow. The
105106
maintainer's ruling given in review of the work for :issue:`808`, that
106107
:class:`TransportProtocol` is not to be extended at all, holds either
107108
way: the refusal is a refusal and mints nothing, whichever exception
@@ -2596,12 +2597,27 @@ def _dispatch(cls, key: 'int', proto: 'TransportProtocol | str | int') -> 'Type[
25962597
f'{cls.__name__}')
25972598

25982599
@classmethod
2599-
def get(cls, key: 'int', *, # type: ignore[override] # pylint: disable=arguments-differ
2600+
def get(cls, key: 'int', default: 'Any' = NO_DEFAULT, *,
26002601
proto: 'TransportProtocol | str | int' = TransportProtocol.undefined) -> 'AppType':
26012602
"""Resolve port ``key`` to the member for its canonical service.
26022603
2604+
Takes the ``default`` :meth:`~pcapkit.corekit.enum.EnumLookup.get`
2605+
advertises, since an override honours the base's signature rather than
2606+
suppressing the mismatch (:issue:`935`), and answers a miss as the base
2607+
does, from :mod:`pcapkit.utilities.exceptions` (:issue:`923`).
2608+
26032609
Args:
26042610
key: Port number to look up.
2611+
default: An already-registered value to fall back to when ``key``
2612+
resolves to nothing: a member's ``value``, such as
2613+
``'http [80 - tcp]'``, and not a port or a member, which hashes
2614+
as its port. Resolved as
2615+
:meth:`~pcapkit.corekit.enum.EnumLookup.get` resolves its own,
2616+
through ``_value2member_map_`` and so never minting, in the
2617+
registry ``key`` was dispatched to, or else in the class called
2618+
on, which on :class:`AppType` itself holds no members.
2619+
:data:`~pcapkit.corekit.sentinels.NO_DEFAULT`, the default, means
2620+
*no default*.
26052621
proto: Transport protocol carrying ``key``. Selects the registry to
26062622
search when called on :class:`AppType` itself, which holds no
26072623
members; ignored when called on one of those registries, each of
@@ -2628,53 +2644,60 @@ def get(cls, key: 'int', *, # type: ignore[override] # pylint: disable=argument
26282644
registry's own transport (:issue:`2048`).
26292645
26302646
Raises:
2631-
ValueError: If called on a class that holds no members, i.e. on
2632-
:class:`AppType` itself, with a ``proto`` naming no registry to
2633-
delegate to. Also for a ``key`` that is not a port number, since
2634-
this registry resolves ports and not service names -- including
2635-
one outside ``0..65535``, whose rejection by :meth:`_missing_` this
2636-
method propagates rather than minting over, so that ``get`` is
2637-
never more permissive than ``AppType(...)``. Also covers a
2638-
``proto`` int that matches no registry at all, composite or
2639-
not -- looked up as the whole value it is rather than
2640-
decoded into the registries its bits would each name, so it
2641-
is refused only when that whole value itself names none of
2642-
them, not simply for being composite -- see
2643-
:meth:`_dispatch`.
2647+
EnumValueError: If ``key`` resolves to nothing and there is no
2648+
usable ``default``: a ``key`` that is not a port number, since
2649+
this registry resolves ports and not service names, including
2650+
one outside ``0..65535``, which :meth:`_missing_` rejects rather
2651+
than minting over, so that ``get`` is never more permissive than
2652+
``AppType(...)``; or, on :class:`AppType` itself, a ``proto``
2653+
naming no registry to delegate to, composite or not (see
2654+
:meth:`_dispatch`). A :exc:`ValueError`, and quiet, as from the
2655+
base.
2656+
EnumKeyError: If, on :class:`AppType` itself, ``proto`` is a name no
2657+
:class:`TransportProtocol` member carries, from
2658+
:meth:`TransportProtocol.get`.
26442659
26452660
"""
2646-
owner = cls._dispatch(key, proto)
2661+
owner = cls
2662+
try:
2663+
owner = cls._dispatch(key, proto)
26472664

2648-
matched = owner.__registry__.getlist(key) # type: ignore[union-attr]
2649-
if matched:
2650-
canonical = owner.__canonical__.get(key)
2651-
if canonical is not None:
2652-
for member in matched:
2653-
if member.svc == canonical:
2654-
return member
2655-
# NOTE: IANA's oldest row, for a port whose collision postdates
2656-
# :data:`__canonical__` -- including one ``aenum.extend_enum``
2657-
# created, which must not displace what the registry already
2658-
# answered with.
2659-
return matched[0]
2665+
matched = owner.__registry__.getlist(key) # type: ignore[union-attr]
2666+
if matched:
2667+
canonical = owner.__canonical__.get(key)
2668+
if canonical is not None:
2669+
for member in matched:
2670+
if member.svc == canonical:
2671+
return member
2672+
# NOTE: IANA's oldest row, for a port whose collision postdates
2673+
# :data:`__canonical__` -- including one ``aenum.extend_enum``
2674+
# created, which must not displace what the registry already
2675+
# answered with.
2676+
return matched[0]
26602677

2661-
# NOTE: :meth:`_missing_` answers :obj:`None` for a port it holds no row
2662-
# for, and that answer does not mint (GitHub issue #860), following the
2663-
# owner's ruling there that ``get`` should not mint because it lacks the
2664-
# information to create a new member: only IANA-registered values are
2665-
# legitimate, and ``register`` is the proper way to add one. That ruling
2666-
# was given for the FEATCode, Command and Method branches, and covers
2667-
# this family under the same issue. A port outside ``0..65535`` *raises*
2668-
# rather than reaching here at all. Minting on the ``None`` answer would
2669-
# name a member ``PORT_{key}_{transport}`` -- GitHub issue #758's
2670-
# defect, which also minted ``PORT_999999_tcp`` and ``PORT_-1_tcp``, the
2671-
# latter a name no attribute access could reach -- so, as inside
2672-
# :meth:`_missing_` itself, the port gets an unregistered member
2673-
# instead: neither is a value anyone asked to be named. A port IANA
2674-
# lists on other transports only is in no span on this one: 8011,
2675-
# reserved on TCP and ``bier-echo`` on UDP, answers ``unknown`` on DCCP
2676-
# and SCTP (#2048).
2677-
ret = owner._missing_(key)
2678+
# NOTE: :meth:`_missing_` answers :obj:`None` for a port it holds no row
2679+
# for, and that answer does not mint (GitHub issue #860), following the
2680+
# owner's ruling there that ``get`` should not mint because it lacks the
2681+
# information to create a new member: only IANA-registered values are
2682+
# legitimate, and ``register`` is the proper way to add one. That ruling
2683+
# was given for the FEATCode, Command and Method branches, and covers
2684+
# this family under the same issue. A port outside ``0..65535`` *raises*
2685+
# rather than reaching here at all. Minting on the ``None`` answer would
2686+
# name a member ``PORT_{key}_{transport}`` -- GitHub issue #758's
2687+
# defect, which also minted ``PORT_999999_tcp`` and ``PORT_-1_tcp``, the
2688+
# latter a name no attribute access could reach -- so, as inside
2689+
# :meth:`_missing_` itself, the port gets an unregistered member
2690+
# instead: neither is a value anyone asked to be named. A port IANA
2691+
# lists on other transports only is in no span on this one: 8011,
2692+
# reserved on TCP and ``bier-echo`` on UDP, answers ``unknown`` on DCCP
2693+
# and SCTP (#2048).
2694+
ret = owner._missing_(key)
2695+
except ValueError as error:
2696+
# NOTE: answered as EnumLookup.get answers a value miss (#923, #2545),
2697+
# with ``default`` from the registry dispatched to, else from ``cls``.
2698+
if default is NO_DEFAULT or default not in owner._value2member_map_:
2699+
raise EnumValueError(str(error), quiet=True) from error
2700+
return owner._value2member_map_[default]
26782701
if ret is None:
26792702
ret = owner._unregistered_member(key, 'unknown', owner.__transport__)
26802703
return ret

‎pcapkit/vendor/reg/apptype/apptype.py‎

Lines changed: 72 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ def get(cls, key: 'int | str', default: 'Any' = NO_DEFAULT) -> 'TransportProtoco
191191
name miss reaches the caller as
192192
:exc:`~pcapkit.utilities.exceptions.EnumKeyError` from the base.
193193
:class:`AppType` and its per-transport registries raise
194-
:exc:`ValueError` from their own ``get()`` because it takes an
195-
:class:`int` port and never accepts a name; that is not a name-miss
196-
policy, so it sets none for this class to follow. The
194+
:exc:`~pcapkit.utilities.exceptions.EnumValueError` from their own
195+
``get()`` because it takes an :class:`int` port and never accepts a
196+
name; that is not a name-miss policy, so it sets none for this class
197+
to follow. The
197198
maintainer's ruling given in review of the work for :issue:`808`, that
198199
:class:`TransportProtocol` is not to be extended at all, holds either
199200
way: the refusal is a refusal and mints nothing, whichever exception
@@ -550,12 +551,27 @@ def _dispatch(cls, key: 'int', proto: 'TransportProtocol | str | int') -> 'Type[
550551
f'{{cls.__name__}}')
551552
552553
@classmethod
553-
def get(cls, key: 'int', *, # type: ignore[override] # pylint: disable=arguments-differ
554+
def get(cls, key: 'int', default: 'Any' = NO_DEFAULT, *,
554555
proto: 'TransportProtocol | str | int' = TransportProtocol.undefined) -> '{NAME}':
555556
"""Resolve port ``key`` to the member for its canonical service.
556557
558+
Takes the ``default`` :meth:`~pcapkit.corekit.enum.EnumLookup.get`
559+
advertises, since an override honours the base's signature rather than
560+
suppressing the mismatch (:issue:`935`), and answers a miss as the base
561+
does, from :mod:`pcapkit.utilities.exceptions` (:issue:`923`).
562+
557563
Args:
558564
key: Port number to look up.
565+
default: An already-registered value to fall back to when ``key``
566+
resolves to nothing: a member's ``value``, such as
567+
``'http [80 - tcp]'``, and not a port or a member, which hashes
568+
as its port. Resolved as
569+
:meth:`~pcapkit.corekit.enum.EnumLookup.get` resolves its own,
570+
through ``_value2member_map_`` and so never minting, in the
571+
registry ``key`` was dispatched to, or else in the class called
572+
on, which on :class:`{NAME}` itself holds no members.
573+
:data:`~pcapkit.corekit.sentinels.NO_DEFAULT`, the default, means
574+
*no default*.
559575
proto: Transport protocol carrying ``key``. Selects the registry to
560576
search when called on :class:`{NAME}` itself, which holds no
561577
members; ignored when called on one of those registries, each of
@@ -582,53 +598,60 @@ def get(cls, key: 'int', *, # type: ignore[override] # pylint: disable=argument
582598
registry's own transport (:issue:`2048`).
583599
584600
Raises:
585-
ValueError: If called on a class that holds no members, i.e. on
586-
:class:`{NAME}` itself, with a ``proto`` naming no registry to
587-
delegate to. Also for a ``key`` that is not a port number, since
588-
this registry resolves ports and not service names -- including
589-
one outside ``0..65535``, whose rejection by :meth:`_missing_` this
590-
method propagates rather than minting over, so that ``get`` is
591-
never more permissive than ``{NAME}(...)``. Also covers a
592-
``proto`` int that matches no registry at all, composite or
593-
not -- looked up as the whole value it is rather than
594-
decoded into the registries its bits would each name, so it
595-
is refused only when that whole value itself names none of
596-
them, not simply for being composite -- see
597-
:meth:`_dispatch`.
601+
EnumValueError: If ``key`` resolves to nothing and there is no
602+
usable ``default``: a ``key`` that is not a port number, since
603+
this registry resolves ports and not service names, including
604+
one outside ``0..65535``, which :meth:`_missing_` rejects rather
605+
than minting over, so that ``get`` is never more permissive than
606+
``{NAME}(...)``; or, on :class:`{NAME}` itself, a ``proto``
607+
naming no registry to delegate to, composite or not (see
608+
:meth:`_dispatch`). A :exc:`ValueError`, and quiet, as from the
609+
base.
610+
EnumKeyError: If, on :class:`{NAME}` itself, ``proto`` is a name no
611+
:class:`TransportProtocol` member carries, from
612+
:meth:`TransportProtocol.get`.
598613
599614
"""
600-
owner = cls._dispatch(key, proto)
601-
602-
matched = owner.__registry__.getlist(key) # type: ignore[union-attr]
603-
if matched:
604-
canonical = owner.__canonical__.get(key)
605-
if canonical is not None:
606-
for member in matched:
607-
if member.svc == canonical:
608-
return member
609-
# NOTE: IANA's oldest row, for a port whose collision postdates
610-
# :data:`__canonical__` -- including one ``aenum.extend_enum``
611-
# created, which must not displace what the registry already
612-
# answered with.
613-
return matched[0]
614-
615-
# NOTE: :meth:`_missing_` answers :obj:`None` for a port it holds no row
616-
# for, and that answer does not mint (GitHub issue #860), following the
617-
# owner's ruling there that ``get`` should not mint because it lacks the
618-
# information to create a new member: only IANA-registered values are
619-
# legitimate, and ``register`` is the proper way to add one. That ruling
620-
# was given for the FEATCode, Command and Method branches, and covers
621-
# this family under the same issue. A port outside ``0..65535`` *raises*
622-
# rather than reaching here at all. Minting on the ``None`` answer would
623-
# name a member ``PORT_{{key}}_{{transport}}`` -- GitHub issue #758's
624-
# defect, which also minted ``PORT_999999_tcp`` and ``PORT_-1_tcp``, the
625-
# latter a name no attribute access could reach -- so, as inside
626-
# :meth:`_missing_` itself, the port gets an unregistered member
627-
# instead: neither is a value anyone asked to be named. A port IANA
628-
# lists on other transports only is in no span on this one: 8011,
629-
# reserved on TCP and ``bier-echo`` on UDP, answers ``unknown`` on DCCP
630-
# and SCTP (#2048).
631-
ret = owner._missing_(key)
615+
owner = cls
616+
try:
617+
owner = cls._dispatch(key, proto)
618+
619+
matched = owner.__registry__.getlist(key) # type: ignore[union-attr]
620+
if matched:
621+
canonical = owner.__canonical__.get(key)
622+
if canonical is not None:
623+
for member in matched:
624+
if member.svc == canonical:
625+
return member
626+
# NOTE: IANA's oldest row, for a port whose collision postdates
627+
# :data:`__canonical__` -- including one ``aenum.extend_enum``
628+
# created, which must not displace what the registry already
629+
# answered with.
630+
return matched[0]
631+
632+
# NOTE: :meth:`_missing_` answers :obj:`None` for a port it holds no row
633+
# for, and that answer does not mint (GitHub issue #860), following the
634+
# owner's ruling there that ``get`` should not mint because it lacks the
635+
# information to create a new member: only IANA-registered values are
636+
# legitimate, and ``register`` is the proper way to add one. That ruling
637+
# was given for the FEATCode, Command and Method branches, and covers
638+
# this family under the same issue. A port outside ``0..65535`` *raises*
639+
# rather than reaching here at all. Minting on the ``None`` answer would
640+
# name a member ``PORT_{{key}}_{{transport}}`` -- GitHub issue #758's
641+
# defect, which also minted ``PORT_999999_tcp`` and ``PORT_-1_tcp``, the
642+
# latter a name no attribute access could reach -- so, as inside
643+
# :meth:`_missing_` itself, the port gets an unregistered member
644+
# instead: neither is a value anyone asked to be named. A port IANA
645+
# lists on other transports only is in no span on this one: 8011,
646+
# reserved on TCP and ``bier-echo`` on UDP, answers ``unknown`` on DCCP
647+
# and SCTP (#2048).
648+
ret = owner._missing_(key)
649+
except ValueError as error:
650+
# NOTE: answered as EnumLookup.get answers a value miss (#923, #2545),
651+
# with ``default`` from the registry dispatched to, else from ``cls``.
652+
if default is NO_DEFAULT or default not in owner._value2member_map_:
653+
raise EnumValueError(str(error), quiet=True) from error
654+
return owner._value2member_map_[default]
632655
if ret is None:
633656
ret = owner._unregistered_member(key, 'unknown', owner.__transport__)
634657
return ret

0 commit comments

Comments
 (0)