Choosing Between utilities and corekit¶
Which of the two support packages a module belongs in. Settled by the owner on #1712, over sorting modules by packet knowledge or by who uses them: this rule can be checked from the import graph alone, and needed the fewest moves.
The page’s questions, as one decision:
flowchart TD
NEW["a new support module,<br/>helper or ContextVar"] --> LAYER{"a schema-layer<br/>helper used<br/>outside it?"}
LAYER -->|no| OWNED{"a helper in<br/>one protocol's<br/>schema?"}
LAYER -->|yes| SCHEMA["corekit.schema<br/>one module per group"]
OWNED -->|"used beyond that<br/>protocol, and generic"| SCHEMA
OWNED -->|no| VAR{"a ContextVar?"}
OWNED -->|"used by that<br/>protocol alone"| STAY["stays beside<br/>that schema"]
VAR -->|"set or read by<br/>several modules"| CTX["utilities.contextvars"]
VAR -->|no| UTIL{"usable by any<br/>code, importing<br/>no pcapkit but<br/>utilities?"}
VAR -->|"used by one<br/>module only"| PRIV["private, in<br/>that module"]
UTIL -->|yes| VER{"differs by<br/>interpreter<br/>version?"}
UTIL -->|no| CORE["corekit"]
VER -->|yes| COMPAT["utilities.compat, behind<br/>a sys.version_info gate"]
VER -->|no| OWN["a utilities module,<br/>not compat"]
pcapkit.utilities is for cross-cutting services: exceptions, warnings,
logging, the compatibility layer, configuration, and wrappers around third-party
libraries such as chardet. Ask of a module: could any code
use this service, and does it import nothing from pcapkit outside
utilities? Both halves must hold, and nothing covers top-level, in-function and
TYPE_CHECKING imports alike.
tests/project/test_utilities_import_boundary_1735_unit.py checks every module
under pcapkit/utilities/.
pcapkit.utilities.compat holds compatibility implementations and re-exports
only. Where a name or behaviour differs by interpreter version, compat re-exports
the right one behind a sys.version_info gate rather than a getattr or
hasattr probe. Ruled in review of the threading excepthook and AppType union
fixes for #1691 and #1694, whose threads do not hold it, and applied to
the other probes on #1709.
tests/project/test_stdlib_feature_probes_1709_unit.py fails on a getattr
or hasattr call naming its attribute in a string literal, where the receiver is a
builtin class or a module the file binds with an import statement, or where the
attribute is one the test lists with the interpreter version that added it, unless the
test exempts the call. A helper that must behave the same on every platform is neither
a compatibility implementation nor a re-export, so it gets its own module:
fromtimestamp() is in
pcapkit.utilities.datetime, ruled in review of the timestamp fix for
#2099, whose thread does not hold it.
pcapkit.corekit is for the types the packet model is built from: what
pcapkit.protocols, pcapkit.const and pcapkit.foundation
subclass, instantiate or decorate with. Ask: is this part of what a protocol or its
schema is made of? A support module that fails the utilities question belongs
here. pcapkit.corekit.decorators acts on a protocol’s file and falls back to
protocol classes, so it moved out of utilities on #1735. corekit may
import utilities; the rule forbids the reverse.
pcapkit.corekit.checksum, settled on #2580, fails the utilities
question because ChecksumStatus subclasses
EnumLookup. The protocols compute and verify checksums
through it, so it imports pcapkit only from corekit and utilities, never
from pcapkit.protocols, as
tests/corekit/test_checksum_import_boundary_2580_unit.py checks. What needs IPv4
and IPv6 stays beside
IP_Pseudo. The package has
a module per checksum family, so a new family sits beside
internet.
Context Variables¶
Where a ContextVar lives depends on which modules use it.
Settled by the owner on #2616:
One that more than one module sets or reads lives in
pcapkit.utilities.contextvars, under a public name in its__all__. No other module re-exports it, so every reader imports it from there.One that only its own module uses stays in that module, private, as
_exthdr_rebuiltdoes inpcapkit.protocols.internet.internet.
A shared variable kept in one of its users makes the others import a private name
and ties them to that module’s place in the import graph. A module of bare
declarations importing nothing from pcapkit passes the utilities question,
so any module may import it – pcapkit.corekit.checksum included, which must not
reach pcapkit.protocols. tests/project/test_shared_contextvars_2616_unit.py
fails on a module that takes a context variable from anywhere else, by name or as an
attribute of an imported module, and on a variable there that is private, missing
from __all__, or read by one module alone.