2.2. Internal Classes

class namedstruct.namedstruct.NamedStruct(parser)

Store a binary struct message, which is serializable.

There are external interfaces for general usage as well as internal interfaces reserved for parser. See doc for each interface.

All the interface names start with _ to preserve normal names for fields.

__copy__()

Create a copy of the struct. It is always a deepcopy, in fact it packs the struct and unpack it again.

__deepcopy__(memo)

Create a copy of the struct.

__getstate__()

Try to pickle the struct. Register the typedef of the struct to make sure it can be pickled and transfered with the type information. Actually it transfers the packed bytes and the type name.

__init__(parser)

Constructor. Usually a NamedStruct is constructed automatically by a parser, you should not call the initializer yourself.

Parameters:
  • parser – parser to pack/unpack the struct
  • inlineparent – if not None, this struct is embedded into another struct. An embedded struct acts like an agent to inlineparent - attributes are read from and stored into inlineparent.
__len__()

Get the struct size with padding. Usually this aligns a struct into 4-bytes or 8-bytes boundary.

Returns:padded size of struct in bytes
__repr__(*args, **kwargs)

Return the representation of the struct.

__setstate__(state)

Restore from pickled value.

__weakref__

list of weak references to the object (if defined)

_autosubclass()

Subclass a struct. When you modified some fields of a base class, you can create sub-classes with this method.

_create_embedded_indices()

Create indices for all the embedded structs. For parser internal use.

_extend(newsub)

Append a subclass (extension) after the base class. For parser internal use.

_get_embedded(name)

Return an embedded struct object to calculate the size or use _tobytes(True) to convert just the embedded parts.

Parameters:name – either the original type, or the name of the original type. It is always the type used in type definitions, even if it is already replaced once or more.
Returns:an embedded struct
_getextra()

Get the extra data of this struct.

_gettype()

Return current type of this struct

Returns:a typedef object (e.g. nstruct)
_pack()

Pack current struct into bytes. For parser internal use.

Returns:packed bytes
_prepack()

Prepack stage. For parser internal use.

_realsize()

Get the struct size without padding (or the “real size”)

Returns:the “real size” in bytes
static _registerPickleType(name, typedef)

Register a type with the specified name. After registration, NamedStruct with this type (and any sub-types) can be successfully pickled and transfered.

_replace_embedded_type(name, newtype)

Replace the embedded struct to a newly-created struct of another type (usually based on the original type). The attributes of the old struct is NOT preserved.

Parameters:
  • name – either the original type, or the name of the original type. It is always the type used in type definitions, even if it is already replaced once or more.
  • newtype – the new type to replace
_setextra(extradata)

Set the _extra field in the struct, which stands for the additional (“extra”) data after the defined fields.

_subclass(parser)

Create sub-classed struct from extra data, with specified parser. For parser internal use.

Parameters:parser – parser of subclass
_tobytes(skipprepack=False)

Convert the struct to bytes. This is the standard way to convert a NamedStruct to bytes.

Parameters:skipprepack – if True, the prepack stage is skipped. For parser internal use.
Returns:converted bytes
_unpack(data)

Unpack a struct from bytes. For parser internal use.

_validate(recursive=True)

DEPRECATED structs are always unpacked now. _validate do nothing.

Force a unpack on this struct to check if there are any format errors. Sometimes a struct is not unpacked until attributes are read from it, if there are format errors in the original data, a BadFormatError is raised. Call _validate to ensure that the struct is fully well-formatted.

Parameters:recursive – if True (default), also validate all sub-fields.
class namedstruct.namedstruct.ParseError

Base class for error in parsing

class namedstruct.namedstruct.BadLenError

Data size does not match struct length

class namedstruct.namedstruct.BadFormatError

Error in data format