rlp package
Subpackages
- rlp.sedes package
- Submodules
- rlp.sedes.big_endian_int module
- rlp.sedes.binary module
- rlp.sedes.boolean module
- rlp.sedes.lists module
- rlp.sedes.raw module
- rlp.sedes.serializable module
- rlp.sedes.text module
- Module contents
Submodules
rlp.atomic module
rlp.codec module
- rlp.codec.consume_item(rlp, start)
Read an item from an RLP string.
- Parameters:
rlp – the rlp string to read from
start – the position at which to start reading
- Returns:
a tuple
(item, per_item_rlp, end)
, whereitem
is the read item, per_item_rlp is a list containing the RLP encoding of each item andend
is the position of the first unprocessed byte
- rlp.codec.consume_length_prefix(rlp, start)
Read a length prefix from an RLP string.
- Parameters:
rlp – the rlp byte string to read from
start – the position at which to start reading
- Returns:
a tuple
(prefix, type, length, end)
, wheretype
is eitherstr
orlist
depending on the type of the following payload,length
is the length of the payload in bytes, andend
is the position of the first payload byte in the rlp string
- rlp.codec.consume_payload(rlp, prefix, start, type_, length)
Read the payload of an item from an RLP string.
- Parameters:
rlp – the rlp string to read from
type – the type of the payload (
bytes
orlist
)start – the position at which to start reading
length – the length of the payload in bytes
- Returns:
a tuple
(item, per_item_rlp, end)
, whereitem
is the read item, per_item_rlp is a list containing the RLP encoding of each item andend
is the position of the first unprocessed byte
- rlp.codec.decode(rlp, sedes=None, strict=True, recursive_cache=False, **kwargs)
Decode an RLP encoded object.
If the deserialized result obj has an attribute
_cached_rlp
(e.g. if sedes is a subclass ofrlp.Serializable
) it will be set to rlp, which will improve performance on subsequentrlp.encode()
calls. Bear in mind however that obj needs to make sure that this value is updated whenever one of its fields changes or prevent such changes entirely (rlp.sedes.Serializable
does the latter).- Parameters:
sedes – an object implementing a function
deserialize(code)
which will be applied after decoding, orNone
if no deserialization should be performed**kwargs – additional keyword arguments that will be passed to the deserializer
strict – if false inputs that are longer than necessary don’t cause an exception
- Returns:
the decoded and maybe deserialized Python object
- Raises:
rlp.DecodingError
if the input string does not end after the root item and strict is true- Raises:
rlp.DeserializationError
if the deserialization fails
- rlp.codec.decode_raw(item, strict, _)
- rlp.codec.encode(obj, sedes=None, infer_serializer=True, cache=True)
Encode a Python object in RLP format.
By default, the object is serialized in a suitable way first (using
rlp.infer_sedes()
) and then encoded. Serialization can be explicitly suppressed by setting infer_serializer toFalse
and not passing an alternative as sedes.If obj has an attribute
_cached_rlp
(as, notably,rlp.Serializable
) and its value is not None, this value is returned bypassing serialization and encoding, unless sedes is given (as the cache is assumed to refer to the standard serialization which can be replaced by specifying sedes).If obj is a
rlp.Serializable
and cache is true, the result of the encoding will be stored in_cached_rlp
if it is empty.- Parameters:
sedes – an object implementing a function
serialize(obj)
which will be used to serializeobj
before encoding, orNone
to use the infered one (if any)infer_serializer – if
True
an appropriate serializer will be selected usingrlp.infer_sedes()
to serialize obj before encodingcache – cache the return value in obj._cached_rlp if possible (default True)
- Returns:
the RLP encoded item
- Raises:
rlp.EncodingError
in the rather unlikely case that the item is too big to encode (will not happen)- Raises:
rlp.SerializationError
if the serialization fails
- rlp.codec.encode_raw(item)
RLP encode (a nested sequence of)
Atomic
s.
- rlp.codec.infer_sedes(obj)
Try to find a sedes objects suitable for a given Python object.
The sedes objects considered are obj’s class, big_endian_int and binary. If obj is a sequence, a
rlp.sedes.List
will be constructed recursively.- Parameters:
obj – the python object for which to find a sedes object
- Raises:
TypeError
if no appropriate sedes could be found
- rlp.codec.length_prefix(length, offset)
Construct the prefix to lists or strings denoting their length.
- Parameters:
length – the length of the item in bytes
offset –
0x80
when encoding raw bytes,0xc0
when encoding a list
rlp.exceptions module
- exception rlp.exceptions.DecodingError(message, rlp)
Bases:
RLPException
Exception raised if decoding fails.
- Variables:
rlp – the RLP string that could not be decoded
- exception rlp.exceptions.DeserializationError(message, serial)
Bases:
RLPException
Exception raised if deserialization fails.
- Variables:
serial – the decoded RLP string that could not be deserialized
- exception rlp.exceptions.EncodingError(message, obj)
Bases:
RLPException
Exception raised if encoding fails.
- Variables:
obj – the object that could not be encoded
- exception rlp.exceptions.ListDeserializationError(message=None, serial=None, element_exception=None, index=None)
Bases:
DeserializationError
Exception raised if deserialization by a
sedes.List
fails.- Variables:
element_exception – the exception that occurred during the deserialization of one of the elements, or None if the error is unrelated to a specific element
index – the index in the list that produced the error or None if the error is unrelated to a specific element
- exception rlp.exceptions.ListSerializationError(message=None, obj=None, element_exception=None, index=None)
Bases:
SerializationError
Exception raised if serialization by a
sedes.List
fails.- Variables:
element_exception – the exception that occurred during the serialization of one of the elements, or None if the error is unrelated to a specific element
index – the index in the list that produced the error or None if the error is unrelated to a specific element
- exception rlp.exceptions.ObjectDeserializationError(message=None, serial=None, sedes=None, list_exception=None)
Bases:
DeserializationError
Exception raised if deserialization by a
sedes.Serializable
fails.- Variables:
sedes – the
sedes.Serializable
that failedlist_exception – exception raised by the underlying list sedes, or None if no such exception has been raised
field – name of the field of the object that produced the error, or None if no field responsible for the error
- exception rlp.exceptions.ObjectSerializationError(message=None, obj=None, sedes=None, list_exception=None)
Bases:
SerializationError
Exception raised if serialization of a
sedes.Serializable
object fails.- Variables:
sedes – the
sedes.Serializable
that failedlist_exception – exception raised by the underlying list sedes, or None if no such exception has been raised
field – name of the field of the object that produced the error, or None if no field responsible for the error
- exception rlp.exceptions.RLPException
Bases:
Exception
Base class for exceptions raised by this package.
- exception rlp.exceptions.SerializationError(message, obj)
Bases:
RLPException
Exception raised if serialization fails.
- Variables:
obj – the object that could not be serialized
rlp.lazy module
- class rlp.lazy.LazyList(rlp, start, end, sedes=None, **sedes_kwargs)
Bases:
Sequence
A RLP encoded list which decodes itself when necessary.
Both indexing with positive indices and iterating are supported. Getting the length with
len()
is possible as well but requires full horizontal encoding.- Parameters:
rlp – the rlp string in which the list is encoded
start – the position of the first payload byte of the encoded list
end – the position of the last payload byte of the encoded list
sedes – a sedes object which deserializes each element of the list, or
None
for no deserialization**sedes_kwargs – keyword arguments which will be passed on to the deserializer
- next()
- rlp.lazy.consume_item_lazy(rlp, start)
Read an item from an RLP string lazily.
If the length prefix announces a string, the string is read; if it announces a list, a
LazyList
is created.- Parameters:
rlp – the rlp string to read from
start – the position at which to start reading
- Returns:
a tuple
(item, end)
whereitem
is the read string or aLazyList
andend
is the position of the first unprocessed byte.
- rlp.lazy.decode_lazy(rlp, sedes=None, **sedes_kwargs)
Decode an RLP encoded object in a lazy fashion.
If the encoded object is a bytestring, this function acts similar to
rlp.decode()
. If it is a list however, aLazyList
is returned instead. This object will decode the string lazily, avoiding both horizontal and vertical traversing as much as possible.The way sedes is applied depends on the decoded object: If it is a string sedes deserializes it as a whole; if it is a list, each element is deserialized individually. In both cases, sedes_kwargs are passed on. Note that, if a deserializer is used, only “horizontal” but not “vertical lazyness” can be preserved.
- Parameters:
rlp – the RLP string to decode
sedes – an object implementing a method
deserialize(code)
which is used as described above, orNone
if no deserialization should be performed**sedes_kwargs – additional keyword arguments that will be passed to the deserializers
- Returns:
either the already decoded and deserialized object (if encoded as a string) or an instance of
rlp.LazyList
- rlp.lazy.peek(rlp, index, sedes=None)
Get a specific element from an rlp encoded nested list.
This function uses
rlp.decode_lazy()
and, thus, decodes only the necessary parts of the string.Usage example:
>>> import rlp >>> rlpdata = rlp.encode([1, 2, [3, [4, 5]]]) >>> rlp.peek(rlpdata, 0, rlp.sedes.big_endian_int) 1 >>> rlp.peek(rlpdata, [2, 0], rlp.sedes.big_endian_int) 3
- Parameters:
rlp – the rlp string
index – the index of the element to peek at (can be a list for nested data)
sedes – a sedes used to deserialize the peeked at object, or None if no deserialization should be performed
- Raises:
IndexError
if index is invalid (out of range or too many levels)