User Defined Warnings¶
pcapkit.utilities.warnings 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.
How a Warning Is Reported¶
Every warning pcapkit reports goes through
warn(), which reports it exactly once on each
of two channels:
the
loggerlogger, 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).
The counts are the same in development mode as outside it:
PCAPKIT_DEVMODE and PCAPKIT_VERBOSE change how much detail
the log record carries, never how many records there are. Constructing a warning
class is not an act of reporting one – it emits nothing and has no side effects.
A next layer captured shorter than its length hint is parsed on trial, and
replaced with Raw when its header was not
captured. The trial’s warnings are held back by
hold_warnings: a replaced layer’s are dropped
on both channels, since they describe a layer the result does not have, and a kept
layer’s are reported as above when the trial ends (#1580).
- 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.
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. A block left by anExceptionreports them whateverdiscardsays; one left by any otherBaseException–KeyboardInterrupt,SystemExit,GeneratorExit– drops them, 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.
ProtocolBase._parse_next_layeruses this for the trial parse it replaces withRawwhen the header was not captured: the warnings describe a layer the result does not have (#1580).Note
Nothing here touches
warnings.filtersor a module’s__warningregistry__;warnings.catch_warnings()would, 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, at the level of granularity you want –
BaseWarning for all of them, or 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, and that happens before
site has added site-packages to sys.path, so no installed
package’s own category can be named there – this is not specific to
pcapkit. Use a standard category on the command line, or install a
precise filter in code as above.
All of that governs the warnings channel. The pcapkit logger is
configured separately, through the logging module:
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,ImportWarningVendor CLI invalid updater.
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.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 by
pcapkit.utilities.configfor an environment variable that is invalid, and so falls back to its default, or that lifts a limit past its default.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.