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 ProtocolError rather than a bare AttributeError (#832).

Raises:

pcapkit.utilities.exceptions.ProtocolError – If name is NULL – the caller building this descriptor omitted the class name rather than naming one that turned out wrong – or if module has no attribute named name.

Important

The module is read from sys.modules first, and importlib.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_layer deliberately writes no resolved class back, so every next layer lookup resolves its descriptor again – the one registered for the code, or the fallback for Raw when nobody registered it. An extraction of many_interfaces.pcapng performs 296 resolutions, 48 of them fallbacks (#1559). import_module() keeps real per-call work for an already-imported module (locks, ModuleSpec checks, the fromlist walk), 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.modules is the module cache, and it is the only one whose invalidation the interpreter maintains – importlib.reload() rebinds the class in place and sys.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 fails isinstance() against the live one.

property module: str

Module name.

property name: str | pcapkit.corekit.sentinels.NullType

Class name, or NULL when whatever built this descriptor never got one – see klass.

Type Variables

pcapkit.corekit.module._T: Any