
    (i                     h    d dl mZmZ ddlmZ ddlmZmZ ddlm	Z	 ddZ
d Z G d	 d
e      ZddZy)    )IterableSequence   )Atomic)consume_length_prefixconsume_payload)DecodingErrorNc                     t        | d      \  }}|t        |       k7  rt        d|       t        |t              r||_        ||_        |S |r |j                  |fi |S |S )a  
    Decode an RLP encoded object in a lazy fashion.

    If the encoded object is a bytestring, this function acts similar to
    :func:`rlp.decode`. If it is a list however, a :class:`LazyList` 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.

    :param rlp: the RLP string to decode
    :param sedes: an object implementing a method ``deserialize(code)`` which
                  is used as described above, or ``None`` if no
                  deserialization should be performed
    :param `**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 :class:`rlp.LazyList`
    r   z(RLP length prefix announced wrong length)consume_item_lazylenr	   
isinstanceLazyListsedessedes_kwargsdeserialize)rlpr   r   itemends        9/var/www/br/venv/lib/python3.12/site-packages/rlp/lazy.pydecode_lazyr      sk    0 "#q)ID#
c#hFLL$!
(	 u  666    c                     t        | |      \  }}}}|t        u rt        | ||t        |      \  }}}||fS |t        u sJ t	        | |||z         ||z   fS )a  
    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 :class:`LazyList` is created.

    :param rlp: the rlp string to read from
    :param start: the position at which to start reading
    :returns: a tuple ``(item, end)`` where ``item`` is the read string or a
              :class:`LazyList` and ``end`` is the position of the first
              unprocessed byte.
    )r   bytesr   listr   )	r   startptlsr   _r   s	            r   r   r   7   si     'sE2JAq!QEz&sAq%;aSyDyyQA&A--r   c                   *    e Zd ZdZddZd Zd Zd Zy)r   a  
    A RLP encoded list which decodes itself when necessary.

    Both indexing with positive indices and iterating are supported.
    Getting the length with :func:`len` is possible as well but requires full
    horizontal encoding.

    :param rlp: the rlp string in which the list is encoded
    :param start: the position of the first payload byte of the encoded list
    :param end: the position of the last payload byte of the encoded list
    :param sedes: a sedes object which deserializes each element of the list,
                  or ``None`` for no deserialization
    :param `**sedes_kwargs`: keyword arguments which will be passed on to the
                             deserializer
    Nc                 t    || _         || _        || _        || _        g | _        d | _        || _        || _        y N)r   r   r   index	_elements_lenr   r   )selfr   r   r   r   r   s         r   __init__zLazyList.__init__^   s;    

	
(r   c                    | j                   | j                  k(  r t        | j                        | _        t
        | j                   | j                  k  sJ t        | j                  | j                         \  }}|| _         | j                  r' | j                  j                  |fi | j                  }| j                  j                  |       |S r#   )r$   r   r   r%   r&   StopIterationr   r   r   r   r   append)r'   r   r   s      r   nextzLazyList.nexth   s    ::!DNN+DIzzDHH$$$%dhh

;	c
::)4::))$D$2C2CDDd#r   c                    t        |t              r0|j                  t        d      |j                  }|j
                  }n|}|dz   }|| j                  dz
  }	 t        | j                        |k  r)| j                          t        | j                        |k  r)t        |t              r| j                  || S | j                  |   S # t        $ r* | j                  | j                  k(  sJ t        d|z        w xY w)NzStep not supportedr   zIndex %s out of range)r   slicestep	TypeErrorr   stopr   r   r%   r,   r*   r$   
IndexError)r'   ir   r1   s       r   __getitem__zLazyList.__getitem__t   s    avv! 455GGE66DEq5D<88a<D	:dnn%,		 dnn%, a>>%-->>%((  	:::)))4q899	:s   A C	 	3C<c                     | j                   s	 	 | j                          | j                   S # t        $ r( t        | j                        | _         Y | j                   S w xY wr#   )r&   r,   r*   r   r%   )r'   s    r   __len__zLazyList.__len__   sV    yy0IIK  yy ! 0/	yy0s   , #AAr#   )__name__
__module____qualname____doc__r(   r,   r4   r6    r   r   r   r   M   s     )
)2r   r   c                     t        |       }t        |t              s|g}|D ]"  }t        |t              rt	        d      ||   }$ |r|j                  |      S |S )a  
    Get a specific element from an rlp encoded nested list.

    This function uses :func:`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

    :param rlp: the rlp string
    :param index: the index of the element to peek at (can be a list for
                  nested data)
    :param sedes: a sedes used to deserialize the peeked at object, or `None`
                  if no deserialization should be performed
    :raises: :exc:`IndexError` if `index` is invalid (out of range or too many
             levels)
    zToo many indices given)r   r   r   r   r2   r   )r   r$   r   llr3   s        r   peekr>      sh    0 
S	BeX& b&!566U   $$	r   r#   )collections.abcr   r   atomicr   codecr   r   
exceptionsr	   r   r   r   r>   r;   r   r   <module>rC      s8   

"J.,Gx GT"r   