Pickle Reductions¶
pcapkit.utilities.pickle holds the pickle reductions several
pcapkit classes share, so each is written once (#2714).
ReduceAsProtocol2 lets a typing.Generic subclass or a class with
__slots__ pickle under protocol 0 and 1, on GraalPy as on CPython;
ReduceToName reduces a singleton to the module-level name holding it;
ReduceToItems reduces a multi-valued mapping to a call of its class on its
item pairs; and reduce_to() makes one instance reduce to a call of a given
function. A pickle a class wrote before it took one of these still loads, and one
it writes with it loads where the class still reduces by its own.
- class pcapkit.utilities.pickle.ReduceAsProtocol2[source]¶
Bases:
objectMixin reducing an instance under every
pickleprotocol as protocol 2 does.Below protocol 2,
object.__reduce_ex__()hands the instance tocopyreg._reduce_ex, which walkstype(self).__mro__for the first base that is not a heap type and, unless that base isobject, calls it on the instance.Genericis a heap type on CPython, so the walk passes it. GraalPy buildsGenericas a built-in, so the walk stops there and raisesTypeError: typing.Generic() takes no argumentsfor any generic class with no reduction of its own (#2714). The same walk refuses a class with__slots__and no__getstate__of its own on every interpreter.Protocol 2’s reduction reads the class and the instance state without that walk, slots included, and protocol 0 and 1 write it as an ordinary call of
copyreg.__newobj__, restoring the instance through__new__as protocol 2 does. The next__reduce_ex__in the MRO builds it, so a base with a reduction of its own keeps it, asOrderedMultiInfokeepsOrderedMultiDict’s, and a subclass overriding__reduce__keeps its own.It goes before
Genericamong the bases, as onInfo,Schema,ReassemblyBaseandDeferred.Added in version 1.6.0.
- class pcapkit.utilities.pickle.ReduceToName[source]¶
Bases:
objectMixin reducing a singleton to the module-level name that holds it.
__reduce__()returns_name, asEllipsis.__reduce__()returns'Ellipsis', sopicklestores the instance as a reference to that name in the class’s module and loads it back as the same object, at every protocol. A class using it sets_nameto that name.It is shared by every
Sentineland by the missing-value marker ofpcapkit.corekit.multidict.Added in version 1.6.0.
- class pcapkit.utilities.pickle.ReduceToItems[source]¶
Bases:
objectMixin reducing a multi-valued mapping to a call of its class on its item pairs.
__reduce_ex__()returns(type(self), (pairs,)), the pairs beinglist(self.items(multi=True)), under every protocol. Rebuilding through the class keeps the order of interleaved keys and builds whatever the class keeps beside the pairs, and needs no state to be restored afterwards, which an immutable class refuses.It is shared by
OrderedMultiDictandMultiInfo.Added in version 1.6.0.
- pcapkit.utilities.pickle.reduce_to(obj, rebuild, *args)[source]¶
Make
objreduce torebuild(*args)underpickleandcopy.The reduction goes on the instance, as
obj.__reduce_ex__, which bothpickleandcopyhonour, since each fetches it withgetattr()on the object. That suits an object whose type reduces it wrongly, asEnum.__reduce_ex__reduces an unregistered member to the one value lookup it is absent from.functools.partial()rather than a closure keepsobjout of its own reducer, so being reducible does not put it in a reference cycle.It is shared by
EnumRegistry._make_reducibleandEnumField._unregistered_member.- Parameters:
- Return type:
_T- Returns:
obj, for use as areturnexpression.
Added in version 1.6.0.
- pcapkit.utilities.pickle._reduce_to(rebuild, args, protocol)[source]¶
The
__reduce_ex__reduce_to()installs, bound to its first two arguments.