term.py_codec_impl

Module implements encoder and decoder from ETF (Erlang External Term Format) used by the network distribution layer.

term.py_codec_impl.binary_to_term(data, options=None)

Strip 131 header and unpack if the data was compressed.

Parameters:
Raises:

PyCodecError – when the tag is not 131, when compressed data is incomplete or corrupted, on any malformed input, and when an option callable raises (the original is its __cause__)

Returns:

The value and the remaining unconsumed bytes

Return type:

tuple[str | List[Any] | tuple | dict | int | float | bytes | BaseTerm | None, bytes]

term.py_codec_impl.binary_to_term_2(data, options=None)

Decode one term that has no version byte 131.

Parameters:
  • data (bytes) – the encoded term; more bytes may follow it

  • options (dict[str, Any] | None) – the decode options described in term.codec

Raises:

PyCodecError – on malformed input, and when an option callable raises (the original exception is its __cause__)

Returns:

the value, and the bytes that follow the term

Return type:

tuple[str | List[Any] | tuple | dict | int | float | bytes | BaseTerm | None, bytes]

term.py_codec_impl.term_to_binary(val, opt=None)

Encode a value, starting with the version byte 131.

Parameters:
Raises:

PyCodecError – as term_to_binary_2()

Returns:

the encoded term

Return type:

bytes

term.py_codec_impl.term_to_binary_2(val, encode_hook, max_depth=4096)

Encode a value without the 131 header byte.

Parameters:
  • val (Any) – the value to encode

  • encode_hook (dict[str, Callable[[Any], Any]] | None) – the "encode_hook" dict described in term.codec, or None

  • max_depth (int) – how deeply compound values may nest

Returns:

the encoded term

Raises:

PyCodecError – for a value nested deeper than max_depth, a value that contains itself, and a value with no encoding

Return type:

bytes

exception term.py_codec_impl.PyCodecError

Bases: Exception

A term could not be decoded or a value could not be encoded.

term.py_codec_impl.generic_serialize_object(obj, cycle_detect=None)

The stand-in for an object that has no encoding of its own: the tuple (ClassName, Fields) of its class name and public attributes.

Parameters:
  • obj (Any) – the object to describe

  • cycle_detect (set[int] | None) – ids of the objects already visited

Returns:

the tuple, or None for an object met a second time; and the set of visited ids

Return type:

tuple[tuple[bytes, dict[bytes, Any]] | None, set[int]]