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 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.module.NullType

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

Auxiliaries

class pcapkit.corekit.module.NullType

Bases: object

Type of NULL, the omitted-class_/module sentinel.

A distinct class rather than a plain str such as '(null)', so that is comparisons against it mean what they say: no str a 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, or copy/pickle reconstructing an instance behind the scenes – can end up holding a second object that fails an is NULL check downstream. ModuleDescriptor.klass makes exactly that check; a second instance would slip past it to getattr() and downgrade a clean ProtocolError into a bare TypeError (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 own NULL = 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() and pickle protocols 2 and up reconstruct through cls.__new__(cls), which the guard above already keeps to one instance. Pickle protocols 0 and 1 do not: they reconstruct through copyreg._reconstructor(), which calls object.__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__() return self without reducing anything.

A caveat rather than a defect: importlib.reload() on this module re-executes NULL = NullType() below, producing a second singleton that the reloaded code compares against correctly but that every module which already imported the pre-reload NULL still holds – so a comparison spanning the reload sees two “singletons” that are not each other. ModuleDescriptor.klass faces exactly this class of problem for the class it resolves, which is why it re-reads sys.modules on 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 reloads pcapkit.corekit.sentinels after import.

Reloading pcapkit.corekit.module instead has no effect on the singleton at all. That module only re-imports NULL and NullType from here, and re-running an already-satisfied from ... import reads the current binding in sys.modules rather 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_/module sentinel.

A distinct class rather than a plain str such as '(null)', so that is comparisons against it mean what they say: no str a 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, or copy/pickle reconstructing an instance behind the scenes – can end up holding a second object that fails an is NULL check downstream. ModuleDescriptor.klass makes exactly that check; a second instance would slip past it to getattr() and downgrade a clean ProtocolError into a bare TypeError (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 own NULL = 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() and pickle protocols 2 and up reconstruct through cls.__new__(cls), which the guard above already keeps to one instance. Pickle protocols 0 and 1 do not: they reconstruct through copyreg._reconstructor(), which calls object.__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__() return self without reducing anything.

A caveat rather than a defect: importlib.reload() on this module re-executes NULL = NullType() below, producing a second singleton that the reloaded code compares against correctly but that every module which already imported the pre-reload NULL still holds – so a comparison spanning the reload sees two “singletons” that are not each other. ModuleDescriptor.klass faces exactly this class of problem for the class it resolves, which is why it re-reads sys.modules on 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 reloads pcapkit.corekit.sentinels after import.

Reloading pcapkit.corekit.module instead has no effect on the singleton at all. That module only re-imports NULL and NullType from here, and re-running an already-satisfied from ... import reads the current binding in sys.modules rather than re-executing anything, so it neither mints a new instance nor loses the old one.

Type Variables

pcapkit.corekit.module._T: Any