Module Descriptor¶
pcapkit.corekit.module contains the tuple-like class
ModuleDescriptor, which pairs a module name
with a class name.
- class pcapkit.corekit.module.ModuleDescriptor(module, name)¶
Bases:
ModuleDescriptor,Generic[_T]Module descriptor contains module name and class name, the actual class can be imported by
from module import name.- property klass: Type[_T]¶
Import class from module.
- Raises:
pcapkit.utilities.exceptions.ProtocolError – If
nameisNULL– the caller building this descriptor omitted the class name rather than naming one that turned out wrong – or ifmodulehas no attribute namedname.
Important
The module is read from
sys.modulesfirst, andimportlib.import_module()is entered only when it is not loaded yet. That matters because this property is on a per-frame dispatch path:ProtocolBase._lookup_next_layerdeliberately writes no resolved class back, so every next layer lookup resolves its descriptor again – the one registered for the code, or the fallback forRawwhen nobody registered it. An extraction ofmany_interfaces.pcapngperforms 296 resolutions, 48 of them fallbacks (#1559).import_module()keeps real per-call work for an already-imported module (locks,ModuleSpecchecks, thefromlistwalk), so each repeat costs ~436 ns through it against ~117 ns through this property.The class is still read off the module with
getattr()on every access, and nothing is memoised here. That is the point:sys.modulesis the module cache, and it is the only one whose invalidation the interpreter maintains –importlib.reload()rebinds the class in place andsys.modules.pop()drops the entry, both of which this sees immediately. Holding the resolved class instead would serve the pre-reload class forever, and an instance of it failsisinstance()against the live one.
- property name: str | pcapkit.corekit.module.NullType¶
Class name, or
NULLwhen whatever built this descriptor never got one – seeklass.
Auxiliaries¶
- class pcapkit.corekit.module.NullType¶
Bases:
objectType of
NULL, the omitted-class_/modulesentinel.A distinct class rather than a plain
strsuch as'(null)', so thatiscomparisons against it mean what they say: nostra caller passes, including one that happens to spell'(null)'itself, can be this sentinel. See GitHub issue #833.Genuinely a singleton, not merely a class this module happens to instantiate once:
__new__()always hands back the one instance that already exists, rather than building a new one, so no caller – direct, orcopy/picklereconstructing an instance behind the scenes – can end up holding a second object that fails anis NULLcheck downstream.ModuleDescriptor.klassmakes exactly that check; a second instance would slip past it togetattr()and downgrade a cleanProtocolErrorinto a bareTypeError(attribute name must be string, not 'NullType'). A stricter guard that raises on a second call would be truer to “singleton” in the abstract, but it would also mean the module’s ownNULL = NullType()below is the only call that is ever allowed to succeed – fragile for no real benefit, since nothing here needs rejecting a second construction, only preventing it from producing a distinct object.copy.copy(),copy.deepcopy()andpickleprotocols 2 and up reconstruct throughcls.__new__(cls), which the guard above already keeps to one instance. Pickle protocols 0 and 1 do not: they reconstruct throughcopyreg._reconstructor(), which callsobject.__new__()directly, bypassing__new__()entirely.__reduce__()is defined so that every protocol is routed through the same module-level getter instead of through reconstruction at all, and__copy__()/__deepcopy__()returnselfwithout reducing anything.A caveat rather than a defect:
importlib.reload()on this module re-executesNULL = NullType()below, producing a second singleton that the reloaded code compares against correctly but that every module which already imported the pre-reloadNULLstill holds – so a comparison spanning the reload sees two “singletons” that are not each other.ModuleDescriptor.klassfaces exactly this class of problem for the class it resolves, which is why it re-readssys.moduleson every call rather than memoising; nothing equivalent is possible here, because unlike a resolved class there is no live registry this sentinel could be re-read from. It is a property of sharing one module-level binding across a reload, not something this class’s singleton guarantees claim to solve, and nothing in this package reloadspcapkit.corekit.sentinelsafter import.Reloading
pcapkit.corekit.moduleinstead has no effect on the singleton at all. That module only re-importsNULLandNullTypefrom here, and re-running an already-satisfiedfrom ... importreads the current binding insys.modulesrather than re-executing anything, so it neither mints a new instance nor loses the old one.
- pcapkit.corekit.module.NULL = <NULL>¶
Type of
NULL, the omitted-class_/modulesentinel.A distinct class rather than a plain
strsuch as'(null)', so thatiscomparisons against it mean what they say: nostra caller passes, including one that happens to spell'(null)'itself, can be this sentinel. See GitHub issue #833.Genuinely a singleton, not merely a class this module happens to instantiate once:
__new__()always hands back the one instance that already exists, rather than building a new one, so no caller – direct, orcopy/picklereconstructing an instance behind the scenes – can end up holding a second object that fails anis NULLcheck downstream.ModuleDescriptor.klassmakes exactly that check; a second instance would slip past it togetattr()and downgrade a cleanProtocolErrorinto a bareTypeError(attribute name must be string, not 'NullType'). A stricter guard that raises on a second call would be truer to “singleton” in the abstract, but it would also mean the module’s ownNULL = NullType()below is the only call that is ever allowed to succeed – fragile for no real benefit, since nothing here needs rejecting a second construction, only preventing it from producing a distinct object.copy.copy(),copy.deepcopy()andpickleprotocols 2 and up reconstruct throughcls.__new__(cls), which the guard above already keeps to one instance. Pickle protocols 0 and 1 do not: they reconstruct throughcopyreg._reconstructor(), which callsobject.__new__()directly, bypassing__new__()entirely.__reduce__()is defined so that every protocol is routed through the same module-level getter instead of through reconstruction at all, and__copy__()/__deepcopy__()returnselfwithout reducing anything.A caveat rather than a defect:
importlib.reload()on this module re-executesNULL = NullType()below, producing a second singleton that the reloaded code compares against correctly but that every module which already imported the pre-reloadNULLstill holds – so a comparison spanning the reload sees two “singletons” that are not each other.ModuleDescriptor.klassfaces exactly this class of problem for the class it resolves, which is why it re-readssys.moduleson every call rather than memoising; nothing equivalent is possible here, because unlike a resolved class there is no live registry this sentinel could be re-read from. It is a property of sharing one module-level binding across a reload, not something this class’s singleton guarantees claim to solve, and nothing in this package reloadspcapkit.corekit.sentinelsafter import.Reloading
pcapkit.corekit.moduleinstead has no effect on the singleton at all. That module only re-importsNULLandNullTypefrom here, and re-running an already-satisfiedfrom ... importreads the current binding insys.modulesrather than re-executing anything, so it neither mints a new instance nor loses the old one.