User Defined Warnings¶
pcapkit.utilities.warnings holds refined built-in warnings.
Removed in version 1.5.0: pcapkit.utilities.warnings.DEVMODE, which 1.4.x imported from
pcapkit.utilities.logging; read
pcapkit.utilities.logging.DEVMODE instead.
Reporting a Warning¶
Every warning pcapkit reports goes through
warn(), which reports it exactly once on each
channel:
the
pcapkitlogger (logger), atlogging.WARNINGlevel, unconditionally – so a consumer watching thepcapkitlogger sees every complaint whatever the warning filters say;the standard
warningsmachinery, viawarnings.warn(), subject to the filters – sowarnings.filterwarnings(),warnings.catch_warnings,pytest’sfilterwarnings,-WandPYTHONWARNINGSgovernpcapkitwarnings exactly as they govern any other library’s (with one wrinkle in naming a category on the command line, noted below).
Neither flag changes how many records there are: PCAPKIT_DEVMODE
changes where the log records go, and PCAPKIT_VERBOSE how much detail
they carry. Constructing a warning class reports nothing and has no side
effects.
A next layer is parsed on trial, and replaced with
Raw when its header was not captured or its
parse raises. The trial’s warnings are held back by
hold_warnings: a replaced layer’s are dropped
on each channel, since they describe a layer the result does not have, and a kept
layer’s are reported as above when the trial ends (#1580, #2308,
#2516).
- pcapkit.utilities.warnings.warn(message, category, stacklevel=None)[source]¶
Wrapper function of
warnings.warn().The warning is reported once on the
loggerlogger, then once throughwarnings.warn(). The logger call does not consultwarnings.filters, so a log-based consumer sees the complaint even when the application has filtered the category out; thewarnings.warn()call is filtered normally, so the application keeps full control of that channel – including turning the warning into an error with-W error.- Parameters:
stacklevel (
int|None) – Warning stack level, relative to the caller of this function –1blames the line that calledwarn(),2its caller, and so on, exactly as the argument of the same name reads onwarnings.warn()itself. Defaults tostacklevel(), i.e. the innermost frame outsidepcapkit.
- Return type:
See also
pcapkit.utilities.warningsfor the emission model in full, and for how to silence either channel.Note
Inside a
hold_warningsblock the warning is held back instead, with the frame it is attributed to, and reported from there only if the block keeps it.
- class pcapkit.utilities.warnings.hold_warnings(hold=True)[source]¶
Bases:
objectHold back the warnings a parse reports until it is known to be kept.
- Parameters:
hold (
bool) – Hold the warnings reported inside the block. When false, they are reported at once instead, even inside an enclosing block that holds.
Every
warn()call inside the block is held, with the frame it is attributed to. On leaving the block they are reported in order, with the same message and category and attributed to the same frame on both channels, unlessdiscardwas set, in which case they are dropped, whether the block ends or is left by anException. A block left by any otherBaseException–KeyboardInterrupt,SystemExit,GeneratorExit– drops them whateverdiscardsays, so that a filter turning them into errors cannot replace it.The line named is the frame’s line when the block is left. For the frame
stacklevel()names – the caller ofpcapkit, still inside that call – it is the line named when held.Blocks nest, and each reports what it keeps when it is left, never handing it to an enclosing block. Under a filter turning warnings into errors, the error is then raised where the block that kept them is left – the end of the trial parse, inside the guard it was raised in when nothing was held.
Protocol._parse_next_layeruses this for the trial parse it replaces withRawwhen the header was not captured, or thatbeholder()replaces when it raises: the warnings describe a layer the result does not have (#1580, #2308).Note
Nothing here touches
warnings.filtersor a module’s__warningregistry__;warnings.catch_warningswould, which is what made unrelated warnings re-fire up to v1.4.1 (seeBaseWarning).A block left while one opened inside it is still open – a generator suspended inside its own block – stays in place for that one, which steps over it when it is left. While it is suspended, though, the generator’s block holds its caller’s warnings too: a context variable follows the thread, not the frame.
- discard¶
Drop the held warnings on leaving the block, rather than report them.
- holding¶
Whether warnings reported inside the block are held.
Silencing pcapkit Warnings¶
Filter them like any other library’s, by
BaseWarning for all of them, or by a single
category:
import warnings
from pcapkit.utilities.warnings import BaseWarning, SchemaWarning
warnings.filterwarnings('ignore', category=BaseWarning) # all of them
warnings.filterwarnings('error', category=SchemaWarning) # or turn one into an error
From the command line, name one of the standard categories these are mixed with.
UserWarning covers all of them, and RuntimeWarning,
ImportWarning, ResourceWarning and DeprecationWarning each
select a family:
python -W ignore::UserWarning ... # every pcapkit warning, and everyone else's
python -W error::RuntimeWarning ... # the RuntimeWarning family, as errors
Note
-W and PYTHONWARNINGS cannot name a pcapkit
category directly: -W ignore::pcapkit.utilities.warnings.BaseWarning is
rejected with Invalid -W option ignored: invalid module name. CPython
imports the category while parsing the option, before site has added
site-packages to sys.path, so no installed package’s category can
be named there. Use a standard category on the command line, or a filter in
code as above.
That governs the warnings channel. The pcapkit logger is configured
through logging:
import logging
logging.getLogger('pcapkit').setLevel(logging.ERROR) # drop WARNING records
Changed in version 1.5.0: pcapkit warnings reach the warnings channel by default, and
the first snippet above restores the 1.4.x quiet. In 1.4.x, constructing a
BaseWarning installed an ignore
filter for its own class, so a caller could not re-enable them. The filter
went at the front of the process-global warnings.filters, so it
also overrode the host application’s own configuration for those categories
and made unrelated warnings re-fire (#364).
- exception pcapkit.utilities.warnings.BaseWarning[source]¶
Bases:
UserWarningBase warning class of all kinds.
Constructing one of these is deliberately free of side effects: it emits nothing and mutates no process-global state. Reporting a warning is the job of
warn(), which is called once per complaint; a warning object may be built without ever being reported, and the two must not be confused.Changed in version 1.5.0: The constructor has no side effects (#364). In 1.4.x it called
warnings.simplefilter('ignore', type(self))outside development mode, which inserted an entry at the front of the process-globalwarnings.filters– overriding whatever the host application,-Wor the test runner had configured, and invalidating every module’s__warningregistry__so that unrelated warnings re-fired. It also logged the warning a second time underDEVMODE. Filtering is the application’s business, and is done through the standardwarningsmachinery.- Parameters:
*args – Arbitrary positional arguments.
ImportWarning Category¶
- exception pcapkit.utilities.warnings.FormatWarning[source]¶
Bases:
BaseWarning,ImportWarningWarning on unknown format(s).
- exception pcapkit.utilities.warnings.EngineWarning[source]¶
Bases:
BaseWarning,ImportWarningUnsupported extraction engine.
- exception pcapkit.utilities.warnings.InvalidVendorWarning[source]¶
Bases:
BaseWarning,ImportWarningInvalid updater given to the vendor CLI.
RuntimeWarning Category¶
- exception pcapkit.utilities.warnings.FileWarning[source]¶
Bases:
BaseWarning,RuntimeWarningWarning on file(s).
- exception pcapkit.utilities.warnings.LayerWarning[source]¶
Bases:
BaseWarning,RuntimeWarningUnrecognised layer.
- exception pcapkit.utilities.warnings.ProtocolWarning[source]¶
Bases:
BaseWarning,RuntimeWarningUnrecognised protocol.
- exception pcapkit.utilities.warnings.ChecksumWarning[source]¶
Bases:
ProtocolWarningChecksum mismatch.
Reported when parsing verifies a checksum and finds it wrong, so that a filter can silence it without silencing other protocol warnings.
Added in version 1.6.0.
- exception pcapkit.utilities.warnings.AttributeWarning[source]¶
Bases:
BaseWarning,RuntimeWarningUnsupported attribute.
- exception pcapkit.utilities.warnings.DevModeWarning[source]¶
Bases:
BaseWarning,RuntimeWarningRun in development mode.
- exception pcapkit.utilities.warnings.VendorRequestWarning[source]¶
Bases:
BaseWarning,RuntimeWarningVendor request connection failed.
- exception pcapkit.utilities.warnings.VendorRuntimeWarning[source]¶
Bases:
BaseWarning,RuntimeWarningVendor failed during runtime.
- exception pcapkit.utilities.warnings.UnknownFieldWarning[source]¶
Bases:
BaseWarning,RuntimeWarningUnknown field.
- exception pcapkit.utilities.warnings.RegistryWarning[source]¶
Bases:
BaseWarning,RuntimeWarningRegistry warning.
- exception pcapkit.utilities.warnings.SchemaWarning[source]¶
Bases:
BaseWarning,RuntimeWarningSchema warning.
- exception pcapkit.utilities.warnings.InfoWarning[source]¶
Bases:
BaseWarning,RuntimeWarningInfo class warning.
- exception pcapkit.utilities.warnings.SeekWarning[source]¶
Bases:
BaseWarning,RuntimeWarningSeek operation warning.
- exception pcapkit.utilities.warnings.ExtractionWarning[source]¶
Bases:
BaseWarning,RuntimeWarningExtraction warning.
- exception pcapkit.utilities.warnings.ConfigWarning[source]¶
Bases:
BaseWarning,RuntimeWarningConfiguration warning.
Reported for an environment variable that is invalid, and so falls back to its default, or that lifts a limit past its default: by the readers of
pcapkit.utilities.config, and by this module for the flagspcapkit.utilities.loggingreads.Added in version 1.5.0.
ResourceWarning Category¶
- exception pcapkit.utilities.warnings.DPKTWarning[source]¶
Bases:
BaseWarning,ResourceWarningWarnings on DPKT usage.
- exception pcapkit.utilities.warnings.ScapyWarning[source]¶
Bases:
BaseWarning,ResourceWarningWarnings on Scapy usage.
- exception pcapkit.utilities.warnings.PySharkWarning[source]¶
Bases:
BaseWarning,ResourceWarningWarnings on PyShark usage.
- exception pcapkit.utilities.warnings.EmojiWarning[source]¶
Bases:
BaseWarning,ResourceWarningWarnings on Emoji usage.
- exception pcapkit.utilities.warnings.VendorWarning[source]¶
Bases:
BaseWarning,ResourceWarningWarnings on vendor usage.
DeprecationWarning Category¶
- exception pcapkit.utilities.warnings.DeprecatedFormatWarning[source]¶
Bases:
BaseWarning,DeprecationWarningWarning on deprecated formats.