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)[source]¶
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.
Changed in version 1.5.0: A missing class raises
ProtocolErrorrather than a bareAttributeError(#832).- 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.sentinels.NullType¶
Class name, or
NULLwhen whatever built this descriptor never got one – seeklass.