Conversions

The conversions modules provide utilities for converting between various aggregate types, C types, and Python types.

Modules

PyTypes

class bytemaker.conversions.pytypes.ConversionConfig[source]

Bases: object

Class to configure conversions for Python primitives.

classmethod get_conversion_info(pytype: type) ConversionInfo[source]
classmethod has_suitable_conversion(pytype: type) bool[source]
classmethod set_conversion_info(conversion_info: ConversionInfo)[source]
class bytemaker.conversions.pytypes.ConversionInfo(pytype: type, to_bits: Callable[[Any], BitVector], from_bits: Callable[[BitVector], Any], num_bits: Callable[[Any], int])[source]

Bases: object

Class to store information about conversions between Python primitives and BitVectors.

Attributes:

pytypetype

The Python type to convert to/from BitVectors

to_bitsCallable[[Any], BitVector]

Function to convert a Python instance of the type to a BitVector

from_bitsCallable[[BitVector], Any]

Function to convert a BitVector to a Python instance of the type

num_bitsCallable[[Any], int]

The number of bits in the BitVector representation of the Python instance

from_bits: Callable[[BitVector], Any]
classmethod from_bytes(bytes_obj) Any[source]

Function to convert a bytes object to a Python instance.

num_bits: Callable[[Any], int]
classmethod num_bytes(typeinstance) int[source]

Function to get the number of bytes in the BitVector representation of the Python instance.

pytype: type
to_bits: Callable[[Any], BitVector]
classmethod to_bytes(pytype) bytes[source]
Function to convert a Python instance to the bytes representation

of that instance.

Parameters:

pytype (type) – The Python instance to convert to bytes

Returns:

The bytes representation of the Python instance

Return type:

bytes

class bytemaker.conversions.pytypes.PyType[source]

Bases: object

class bytemaker.conversions.pytypes.PyTypeMeta[source]

Bases: type

This is used to create IsByteConvertible, a type to allow checking whether an object or instances of a class can be converted to a bytes object using isinstance or issubclass.

bytemaker.conversions.pytypes.bits_to_pytype(bits_obj: BitVector, pytype: type)[source]

Function to convert bits into instances of Python types.

Parameters:
  • bytes_obj (bytes) – The bits object to convert to a Python primitive

  • py_prim_type (type) – The type of the Python primitive to convert to. Must be a member of PyTypeWithDefaultBytes

Returns:

The instance of thee provided Python type represented by the

bits

Return type:

pytype

bytemaker.conversions.pytypes.bytes_to_pytype(bytes_obj: BitVector, pytype: type)[source]

Function to convert bytes into instances of Python types.

Parameters:
  • bytes_obj (bytes) – The bytes object to convert to a Python primitive

  • py_prim_type (type) – The type of the Python primitive to convert to. Must be a member of PyTypeWithDefaultBytes

Returns:

The instance of thee provided Python type represented by the

bytes

Return type:

pytype

bytemaker.conversions.pytypes.pytype_to_bits(py_prim: type) BitVector[source]
Function to convert Python instances into a default number of BitVector.

Uses the conversions in ConversionConfig.

Parameters:

py_prim – The python instance to convert to BitVector

Returns:

The BitVector representation of the python instance

Return type:

BitVector

bytemaker.conversions.pytypes.pytype_to_bytes(py_prim: type, reverse_endianness: bool = False) bytes[source]
Function to convert Python instances into a default number of bytes.

Uses the conversions in ConversionConfig.

Parameters:

py_prim – The python instance to convert to bytes

Returns:

The bytes representation of the python instance

Return type:

bytes

CTypes

bytemaker.conversions.ctypes_.bits_to_ctype(bits_obj: BitVector, ctype_type: type, reverse_endianness=True) _SimpleCData | Structure | Union | Array[source]

Function to convert bits into ctypes objects

Parameters:
  • bits_obj (BitVector) – The bits object to convert to a ctypes object

  • ctype_type (type) – The type of the ctypes object to convert to. Must be a member of CType

  • reverse_endianness (bool, optional) – Whether to reverse the endianness of the bits before converting. Defaults to False.

Returns:

ctypes._SimpleCData | ctypes.Structure | ctypes.Union
ctypes.Array:

The ctypes object representation of the bits

bytemaker.conversions.ctypes_.bytes_to_ctype(bytes_obj: bytes, ctype_type: type, reverse_endianness=True) _SimpleCData | Structure | Union | Array[source]

Function to convert bytes into ctypes objects

Parameters:
  • bytes_obj (bytes) – The bytes object to convert to a ctypes object

  • ctype_type (type) – The type of the ctypes object to convert to. Must be a member of CType

  • reverse_endianness (bool, optional) – Whether to reverse the endianness of the bytes before converting. Defaults to False.

Returns:

ctypes._SimpleCData | ctypes.Structure | ctypes.Union
ctypes.Array:

The ctypes object representation of the bytes

bytemaker.conversions.ctypes_.ctype_to_bits(ctype_obj: _SimpleCData | Structure | Union | Array, reverse_endianness=True) BitVector[source]

Function to convert ctypes into BitVector objects

Parameters:
  • ctype_obj (ctypes._SimpleCData | ctypes.Structure | ctypes.Union | ctypes.Array) – The ctypes object to convert to BitVector

  • reverse_endianness (bool, optional) – Whether to reverse the endianness of the BitVector after converting. Defaults to False.

Returns:

The BitVector representation of the ctypes object

Return type:

BitVector

bytemaker.conversions.ctypes_.ctype_to_bytes(ctype_obj: _SimpleCData | Structure | Union | Array, reverse_endianness=True) bytes[source]

Function to convert ctypes into bytes objects

Parameters:
  • | (ctype_obj (ctypes._SimpleCData | ctypes.Structure) – ctypes.Union | ctypes.Array): The ctypes object to convert to bytes

  • reverse_endianness (bool, optional) –

    Whether to reverse the endianness of the bytes after

    converting. Defaults to False.

Returns:

The bytes representation of the ctypes object

Return type:

bytes

bytemaker.conversions.ctypes_.reverse_bytes_unit(unit: _SimpleCData)[source]

Reverses the byte order of a ctypes object.

Parameters:

unit (_SimpleCData) – The ctypes object to reverse the byte order of.

Returns:

The ctypes object with the byte order reversed.

Return type:

_SimpleCData

bytemaker.conversions.ctypes_.reverse_ctype_endianness(ctype_instance: _SimpleCData | Structure | Union | Array) _SimpleCData | Structure | Union | Array[source]

Reverses the endianness of a ctypes object.

Parameters:

| (ctype_instance (ctypes._SimpleCData | ctypes.Structure) – ctypes.Union | ctypes.Array): The ctypes object to reverse the endianness of.

Returns:

The ctypes object with the endianness reversed.

Return type:

ctypes._SimpleCData | ctypes.Structure | ctypes.Union | ctypes.Array

Aggregate Types

bytemaker.conversions.aggregate_types.count_bits_in_aggregate_type(aggregate_type: type) int[source]
Function to count the number of bits in an aggregate type- a Python, type, ctype, BitType (bytemaker type), or

a dataclass annotated with those.

bytemaker.conversions.aggregate_types.count_bits_in_unit_type(unit_type: _SimpleCData | Structure | Union | Array | BitType | PyType) int[source]

Function to count the number of bits in a UnitType- a Python, type, ctype, or BitType (bytemaker type).

bytemaker.conversions.aggregate_types.count_bytes_in_unit_type(unit_type: _SimpleCData | Structure | Union | Array | BitType | PyType) int[source]
Function to count the number of bytes in a UnitType-

a Python numeric/binary/string type, ctype, or BitType (bytemaker type).

bytemaker.conversions.aggregate_types.from_bits_aggregate(unitbits: BitVector, aggregate_type: type) _SimpleCData | Structure | Union | Array | BitType | PyType | DataClassType | Iterable[source]

Function to convert a collection of BitVector objects into Python primitives, ctypes objects, BitTypes, or a dataclass of those types.

Essentially a bitfield deserializer.

Parameters:
  • unitbits (BitVector) – The BitVector object to convert to a Python primitive, ctypes object, BitType, or dataclass.

  • aggregate_type (type) – The type(s) of the object to convert to. Must be a member of UnitType or a dataclass annotated with UnitType members.

Returns:

The object(s)

represented by the bits.

Return type:

Union[UnitType, AggregateTypeByteConvertible]

bytemaker.conversions.aggregate_types.from_bits_individual(unitbits: BitVector, unittype: type) PyType[source]
Function to convert BitVector into a single UnitType-

a Python numeric/binary/string type, ctype, or YType (bytemaker type).

Parameters:
  • unitbits (BitVector) – The BitVector object to convert to a UnitType

  • unittype (type) – The type of the UnitType to convert to. Must be a member of UnitType

bytemaker.conversions.aggregate_types.from_bytes_aggregate(bytes_obj: bytes, aggregate_type: type, is_array=False, reverse_endianness: bool = False) _SimpleCData | Structure | Union | Array | BitType | PyType | DataClassType | Iterable[source]

Function to convert a collection of bytes into Python primitives, ctypes objects, BitTypes, or a dataclass of those types.

Essentially a bitfield deserializer.

Parameters:
  • bytes_obj (bytes) – The bytes object to convert to a Python primitive, ctypes object, BitType, or dataclass.

  • aggregate_type (type) – The type(s) of the object to convert to. Must be a member of UnitType or a dataclass annotated with UnitType members.

  • is_array (bool, optional) – Whether the object is an array of the aggregate type. Defaults to False.

  • reverse_endianness (bool, optional) – Whether to reverse the endianness of the bytes before converting. Defaults to False.

Returns:

The object(s) represented by

the bytes.

Return type:

Union[UnitType, AggregateTypeByteConvertible]

bytemaker.conversions.aggregate_types.from_bytes_individual(unitbytes: bytes, unittype: type, reverse_endianness: bool = False) PyType[source]
Function to convert bytes into a single UnitType-

a Python numeric/binary/string type, ctype, or YType (bytemaker type).

Parameters:
  • unitbytes (bytes) – The bytes object to convert to a UnitType

  • unittype (type) – The type of the UnitType to convert to. Must be a member of UnitType

  • reverse_endianness (bool, optional) – Whether to reverse the endianness of the bytes before converting. Defaults to False.

bytemaker.conversions.aggregate_types.to_bits_aggregate(convertible_object: DataClassType | BitType | _SimpleCData | Structure | Union | Array | PyType | Iterable) BitVector[source]

Function to convert a BitType, Python primitive, ctypes object, or dataclass of those types into a BitVector.

Essentially a bitfield serializer.

Parameters:

units (DataClassType | YType | CType | PyType | Iterable) – The object to convert to BitVector

Returns:

The BitVector representation of the object

Return type:

BitVector

bytemaker.conversions.aggregate_types.to_bits_individual(unit: _SimpleCData | Structure | Union | Array | BitType | PyType) BitVector[source]

Function to convert a single Python primitive or ctypes object into BitVector.

bytemaker.conversions.aggregate_types.to_bytes_aggregate(units: DataClassType | BitType | _SimpleCData | Structure | Union | Array | PyType | Iterable, reverse_endianness: bool = False) bytes[source]

Function to convert a collection of Python primitives or ctypes objects into bytes.

Essentially a bitfield serializer.

Parameters:

DataClassType]) (units [Iterable |) – The objects to convert to bytes

Returns:

The bytes representation of the objects

Return type:

bytes

bytemaker.conversions.aggregate_types.to_bytes_individual(unit: _SimpleCData | Structure | Union | Array | BitType | PyType, reverse_endianness: bool = False) bytes[source]

Function to convert a single Python primitive or ctypes object into bytes.

bytemaker.conversions.aggregate_types.trycast(obj, type_)[source]