Base Fields¶
- class pcapkit.corekit.fields.field.Field(length, default=<pcapkit.corekit.fields.field.NoValueType object>, callback=<function Field.<lambda>>)[source]¶
Bases:
FieldBase[_T],Generic[_T]Base class for protocol fields.
- Parameters:
length (
int|Callable[[dict[str,Any]],int]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.default (
TypeVar(_T) |NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
- _length¶
- _length_callback¶
- class pcapkit.corekit.fields.field.FieldBase(*args, **kwargs)[source]¶
-
Internal base class for protocol fields.
Important
A negative value of
lengthindicates that the field is variable-length (i.e., length unspecified) and thuspack()should be considerate of the template format and the actual value provided for packing.- Parameters:
- _default: _T | NoValueType = <pcapkit.corekit.fields.field.NoValueType object>¶
- property default: _T | NoValueType¶
Field default value.
- property length: int¶
Field size.
- Raises:
ProtocolError – If
templateresolves to a negative count (e.g.'-5s', from alengthcallback such aslambda pkt: pkt['__length__']resolving below zero once the buffer ran short of what the schema declared).struct.calcsize()cannot size such a template and raises a barestruct.error, uncatchable as a pcapkit-specific error; this re-raises it as the negative-length message below. See #805.ProtocolError – If
templateis otherwise malformed – anything elsestruct.calcsize()cannot size, such as a typo’d format character – rather than the negative-length message above, which would misreport the actual cause.struct.calcsize()raises the identical barestruct.errorfor both cases (measured:calcsize('-1s')andcalcsize('Xs')both raisebad char in struct format), so the two are told apart by_RE_NEGATIVE_LENGTH_TEMPLATEagainsttemplateitself – which is known already, without needing anythingstruct.calcsize()’s own error says – rather than by the error message. See #825.
- __call__(packet)[source]¶
Update field attributes.
- Parameters:
- Return type:
Self- Returns:
Updated field instance.
This method will return a new instance of
FieldBaseinstead of updating the current instance.
- __copy__()[source]¶
Return a shallow copy of the field.
Every field of every protocol is copied once per packet by
__call__(), which made the genericcopy.copy()path – viaobject.__reduce_ex__()andcopy._reconstruct()– one of the costlier things an extraction did. This does what that path would have done, and only that: a new instance of the same class, its__dict__shallow-updated from this one.Note
Every
__call__override in this module callsself.__copy__()directly rather thancopy.copy(self).copy.copy()still has to find this method before it can call it –getattr(cls, '__copy__', None)– and that lookup alone was profiled at 55,846 calls (~1.7% of anextract()run) onexamples/captures/http.pcap, one per field per packet, all from this exact path. Calling__copy__directly is exactly whatcopy.copy()would have done once it found it, so this changes nothing about when a field is copied or what the copy contains – only the redundant dispatch is removed. See GitHub issue #730.- Return type:
Self- Returns:
A new field instance sharing this one’s attribute values.
- __set_name__(owner, name)[source]¶
Set field name and update field list (if applicable).
This method is to be called by the metaclass during class creation. It is used to set the field name and update the field list, i.e.,
Schema.__fields__mapping dictionary.
- unpack(buffer, packet)[source]¶
Unpack field value from
bytes.- Parameters:
- Return type:
TypeVar(_T)- Returns:
Unpacked field value.
- Raises:
FieldValueError – If
bufferholds fewer octets thanlengthdeclares, and eitherlengthis past_MAX_ZERO_PAD_LENGTH, or the shortfall is past_MAX_ZERO_PAD_SHORTFALLand takes this parse’s total of such shortfalls past what_ZERO_PAD_BUDGET_RATIOallows for the octets it has actually been given. A shortfall within_MAX_ZERO_PAD_SHORTFALLis always padded and never raises.
Auxiliaries¶
- pcapkit.corekit.fields.field.NoValue¶
Default value for
FieldBase.default.- Type:
Internal Definitions¶
- class pcapkit.corekit.fields.field.FieldMeta(name, bases, namespace, /, **kwargs)[source]¶
-
Meta class to add dynamic support to
FieldBase.This meta class is used to generate necessary attributes for the
FieldBaseclass. It can be useful to reduce unnecessary registry calls and simplify the customisation process.