|
1 | 1 | # -*- coding: utf-8 -*- |
2 | | -"""numerical field class""" |
| 2 | +"""numerical field class |
| 3 | +
|
| 4 | +.. versionremoved:: 1.6.0 |
| 5 | +
|
| 6 | + ``pcapkit.corekit.fields.numbers._reduce_unregistered_member``, with no |
| 7 | + alias; :func:`pcapkit.utilities.pickle.reduce_to` installs the reduction it |
| 8 | + built (:issue:`2714`). |
| 9 | +
|
| 10 | +""" |
3 | 11 |
|
4 | 12 | import enum |
5 | | -import functools |
6 | 13 | import math |
7 | 14 | from typing import TYPE_CHECKING, Generic, TypeVar, Union, cast |
8 | 15 |
|
|
11 | 18 | from pcapkit.corekit.fields.field import Field |
12 | 19 | from pcapkit.corekit.sentinels import NO_VALUE |
13 | 20 | from pcapkit.utilities.exceptions import BaseError, FieldValueError, IntError, ProtocolError |
| 21 | +from pcapkit.utilities.pickle import reduce_to |
14 | 22 |
|
15 | 23 | __all__ = [ |
16 | 24 | 'NumberField', |
@@ -958,14 +966,9 @@ def _unregistered_member(namespace: 'Type[StdlibEnum] | Type[AenumEnum]', |
958 | 966 | # NOTE: ``Enum.__reduce_ex__`` reduces a member to ``(cls, (value,))``, |
959 | 967 | # i.e. to the one lookup this member is deliberately absent from, so |
960 | 968 | # pickle and copy would both raise on it without this. Overriding it |
961 | | - # per instance -- which pickle and copy both honour, since both fetch |
962 | | - # it with getattr on the object rather than on its type -- rebuilds an |
963 | | - # equivalent unregistered member instead. functools.partial rather |
964 | | - # than a closure keeps this off obj itself, so the member does not |
965 | | - # become part of a reference cycle merely by being reducible. |
966 | | - obj.__reduce_ex__ = functools.partial( |
967 | | - _reduce_unregistered_member, namespace, value, name, attrs) |
968 | | - return obj |
| 969 | + # per instance, which pickle and copy both honour, rebuilds an |
| 970 | + # equivalent unregistered member instead. |
| 971 | + return reduce_to(obj, _rebuild_unregistered_member, namespace, value, name, attrs) |
969 | 972 |
|
970 | 973 |
|
971 | 974 | def _rebuild_unregistered_member(namespace: 'Type[StdlibEnum] | Type[AenumEnum]', |
@@ -996,31 +999,6 @@ def _rebuild_unregistered_member(namespace: 'Type[StdlibEnum] | Type[AenumEnum]' |
996 | 999 | namespace, value, name, **attrs) |
997 | 1000 |
|
998 | 1001 |
|
999 | | -def _reduce_unregistered_member( # pylint: disable=unused-argument |
1000 | | - namespace: 'Type[StdlibEnum] | Type[AenumEnum]', |
1001 | | - value: 'Any', name: 'str', attrs: 'dict[str, Any]', |
1002 | | - protocol: 'int') -> 'tuple[Callable[..., Any], tuple[Any, ...]]': |
1003 | | - """The ``__reduce_ex__`` :meth:`EnumField._unregistered_member` installs. |
1004 | | -
|
1005 | | - Bound to its first four arguments with :func:`functools.partial`, so that |
1006 | | - the reducer holds the ingredients of the member rather than the member |
1007 | | - itself. |
1008 | | -
|
1009 | | - Args: |
1010 | | - namespace: The registry class the member is an instance of. |
1011 | | - value: The member's ``_value_``. |
1012 | | - name: The member's ``_name_``. |
1013 | | - attrs: The extra attributes the member carries. |
1014 | | - protocol: The :mod:`pickle` protocol version, ignored -- the reduction |
1015 | | - is the same for all of them, and :mod:`copy` passes 4 here. |
1016 | | -
|
1017 | | - Returns: |
1018 | | - A two-tuple of :func:`_rebuild_unregistered_member` and its arguments. |
1019 | | -
|
1020 | | - """ |
1021 | | - return (_rebuild_unregistered_member, (namespace, value, name, attrs)) |
1022 | | - |
1023 | | - |
1024 | 1002 | class PortEnumField(EnumField): |
1025 | 1003 | """Enumerated value for transport-layer port fields. |
1026 | 1004 |
|
|
0 commit comments