a
    zn„fc  ã                   @  sh   d Z ddlmZ ddlZddlZddlZddlZdd„ Zdd„ Z	dd	„ Z
e d
¡Zdd„ Zddd„ZdS )a†  
This module defines low-level methods for serialization, including :doc:`uproot.serialization.string`,
which prepends string lengths following ROOT's convention, :doc:`uproot.serialization.numbytes_version`,
the opposite of :doc:`uproot.deserialization.numbytes_version`, and :doc:`uproot.serialization.serialize_object_any`,
the opposite of :doc:`uproot.deserialization.read_object_any`.
é    )ÚannotationsNc                 C  s   t | jdd�ƒS )uÿ   
    Converts a Python string into bytes, ready to be written to a file.

    If the string's byte representation (UTF-8) has fewer than 255 bytes, it
    is preceded by a 1-byte length; otherwise, it is preceded by ``b'Ã¿'`` and a
    4-byte length.
    Úsurrogateescape©Úerrors)Ú
bytestringÚencode)Údata© r	   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/uproot/serialization.pyÚstring   s    r   c                 C  s:   t | ƒ}|dk r"t d| || ¡S t d| d|| ¡S dS )u  
    Converts Python bytes into a length-prefixed bytestring, ready to be written to a file.

    If the string's byte representation (UTF-8) has fewer than 255 bytes, it
    is preceded by a 1-byte length; otherwise, it is preceded by ``b'Ã¿'`` and a
    4-byte length.
    éÿ   z>B%dsz>BI%dsN)ÚlenÚstructÚpack)r   Úlengthr	   r	   r
   r      s    r   c                 C  s"   t jj t | d ¡t jjB |¡S )a  
    Args:
        num_bytes (int): The first value to include in the header.
        version (int): The second value to include in the header.

    Returns a 6-byte bytes object consisting of ``num_bytes`` and ``version``,
    with the appropriate offset (+2) and ``uproot.const.kByteCountMask`` applied
    to ``num_bytes``.

    This function is the opposite of :doc:`uproot.deserialization.numbytes_version`,
    but only generates one case (whereas the deserializer must handle special flags
    in the ``num_bytes``).
    é   )ÚuprootZdeserializationZ_numbytes_version_1r   ÚnumpyÚuint32ÚconstÚkByteCountMask)Ú	num_bytesÚversionr	   r	   r
   Únumbytes_version-   s    ÿr   z>IIc                 C  s    |d u r|   d¡ nˆt| ƒ}| | d|t d¡¡ |jjdd�d }tdd„ | |d … D ƒƒt|ƒ d	 }t |¡tj	j
B }tj	j}|  |t ||¡| ¡ d S )
Ns       Tr   r   r   ó    c                 s  s   | ]}t |ƒV  qd S )N)r   )Ú.0Úxr	   r	   r
   Ú	<genexpr>L   ó    z(_serialize_object_any.<locals>.<genexpr>é   )Úappendr   Z
_serializer   r   Ú	classnamer   Úsumr   r   r   ZkNewClassTagÚinsertÚ_serialize_object_any_format1r   )ÚoutÚmodelÚnameÚwherer!   r   ZbcntÚtagr	   r	   r
   Ú_serialize_object_anyC   s    &r*   c                 C  s   g }t || |ƒ d |¡S )zÿ
    Args:
        model (:doc:`uproot.model.Model`): Object to serialize.
        name (str or None): If not None, overrides the object's name.

    Serializes the object in a form that could be read by :doc:`uproot.deserialization.read_object_any`.
    r   )r*   Újoin)r&   r'   r%   r	   r	   r
   Úserialize_object_anyS   s    r,   )N)Ú__doc__Ú
__future__r   r   r   Zuproot.constr   Zuproot.deserializationr   r   r   ÚStructr$   r*   r,   r	   r	   r	   r
   Ú<module>   s   
