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:
objectClass to configure conversions for Python primitives.
- classmethod get_conversion_info(pytype: type) ConversionInfo[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:
objectClass 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
- classmethod from_bytes(bytes_obj) Any[source]¶
Function to convert a bytes object to a Python instance.
- classmethod num_bytes(typeinstance) int[source]¶
Function to get the number of bytes in the BitVector representation of the Python instance.
- pytype: type¶
- class bytemaker.conversions.pytypes.PyTypeMeta[source]¶
Bases:
typeThis 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:
- 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:
- 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