o
    ŒŽ•Z  ã                   @   sT   d dl T d dlT d dlZd dlZd dlZd dlZG dd„ deƒZG dd„ deƒZ	dS )é    )Ú*Nc                       sJ   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	‡  Z
S )ÚProbeaD  
    Probe that dumps the context, and some stream content (peeks into it) to the screen to aid the debugging process. It can optionally limit itself to a single context entry, instead of printing entire context.

    :param into: optional, None by default, or context lambda
    :param lookahead: optional, integer, number of bytes to dump from the stream

    Example::

        >>> d = Struct(
        ...     "count" / Byte,
        ...     "items" / Byte[this.count],
        ...     Probe(lookahead=32),
        ... )
        >>> d.parse(b"\x05abcde\x01\x02\x03")

        --------------------------------------------------
        Probe, path is (parsing), into is None
        Stream peek: (hexlified) b'010203'...
        Container: 
            count = 5
            items = ListContainer: 
                97
                98
                99
                100
                101
        --------------------------------------------------

    ::

        >>> d = Struct(
        ...     "count" / Byte,
        ...     "items" / Byte[this.count],
        ...     Probe(this.count),
        ... )
        >>> d.parse(b"\x05abcde\x01\x02\x03")

        --------------------------------------------------
        Probe, path is (parsing), into is this.count
        5
        --------------------------------------------------

    Nc                    s$   t t| ƒ ¡  d| _|| _|| _d S )NT)Úsuperr   Ú__init__ÚflagbuildnoneÚintoÚ	lookahead)Úselfr   r   ©Ú	__class__© úA/var/www/html/env/lib/python3.10/site-packages/construct/debug.pyr   3   s   
zProbe.__init__c                 C   s   |   |||¡ d S ©N©Úprintout©r	   ÚstreamÚcontextÚpathr   r   r   Ú_parse9   ó   zProbe._parsec                 C   s   |   |||¡ d S r   r   ©r	   Úobjr   r   r   r   r   r   Ú_build<   r   zProbe._buildc                 C   s   |   d ||¡ dS )Nr   r   ©r	   r   r   r   r   r   Ú_sizeof?   s   zProbe._sizeofc                 C   s   | j r	d| j f S dS )Nz	print(%s)zprint(this))r   ©r	   Úcoder   r   r   Ú
_emitparseC   s   zProbe._emitparsec                 C   sÄ   t dƒ t d|| jf ƒ | jr3|d ur3| ¡ }| | j¡}| |¡ |r/t dt|ƒf ƒ nt dƒ |d ur\| jrXz|  |¡}t |ƒ W n tyW   t d| j|f ƒ Y nw t |ƒ t dƒ d S )Nú2--------------------------------------------------zProbe, path is %s, into is %rzStream peek: (hexlified) %s...zStream peek: EOF reachedz&Failed to compute %r on the context %r)Úprintr   r   ÚtellÚreadÚseekÚhexlifyÚ	Exception)r	   r   r   r   ÚfallbackÚdatafollowsÚ
subcontextr   r   r   r   F   s&   

ÿzProbe.printout)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   Ú__classcell__r   r   r
   r   r      s    ,r   c                   @   s:   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zddd„Zd
S )ÚDebuggera  
    PDB-based debugger. When an exception occurs in the subcon, a debugger will appear and allow you to debug the error (and even fix it on-the-fly).
    
    :param subcon: Construct instance, subcon to debug
    
    Example::
    
        >>> Debugger(Byte[3]).build([])

        --------------------------------------------------
        Debugging exception of <Array: None>
        path is (building)
          File "/media/arkadiusz/MAIN/GitHub/construct/construct/debug.py", line 192, in _build
            return self.subcon._build(obj, stream, context, path)
          File "/media/arkadiusz/MAIN/GitHub/construct/construct/core.py", line 2149, in _build
            raise RangeError("expected %d elements, found %d" % (count, len(obj)))
        construct.core.RangeError: expected 3 elements, found 0

        > /media/arkadiusz/MAIN/GitHub/construct/construct/core.py(2149)_build()
        -> raise RangeError("expected %d elements, found %d" % (count, len(obj)))
        (Pdb) q
        --------------------------------------------------
    c                 C   sL   z	| j  |||¡W S  ty%   t| _| j|dd� | jtu r ‚ | j Y S w )Nz=(you can set self.retval, which will be returned from method))Úmsg)Úsubconr   r%   ÚNotImplementedÚretvalÚ
handle_excr   r   r   r   r   x   s   

úzDebugger._parsec                 C   s4   z
| j  ||||¡W S  ty   |  |¡ Y d S w r   )r0   r   r%   r3   r   r   r   r   r   ƒ   s
   ÿzDebugger._buildc                 C   s0   z| j  ||¡W S  ty   |  |¡ Y d S w r   )r0   r   r%   r3   r   r   r   r   r   ‰   s
   ÿzDebugger._sizeofc                 C   s   | j  |¡S r   )r0   Ú_compileparser   r   r   r   r   �   s   zDebugger._emitparseNc                 C   sp   t dƒ t d| jf ƒ t d|f ƒ t d tjt ¡ Ž dd … ¡ƒ |r)t |ƒ t t ¡ d ¡ t dƒ d S )Nr   zDebugging exception of %rz
path is %sÚ é   é   )	r    r0   ÚjoinÚ	tracebackÚformat_exceptionÚsysÚexc_infoÚpdbÚpost_mortem)r	   r   r/   r   r   r   r3   ’   s    zDebugger.handle_excr   )	r)   r*   r+   r,   r   r   r   r   r3   r   r   r   r   r.   _   s    r.   )
Ú	constructÚconstruct.libr;   r9   r=   ÚinspectÚ	Constructr   ÚSubconstructr.   r   r   r   r   Ú<module>   s
     Y