
    uYf              
       T   d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d d	lmZ d
dlmZ eegef   Ze	eeef   ZdZ G d de      Z e       Zedk\  rej1                  d       n(edk\  rej1                  d       nej1                  d       eD  cg c]  } | j2                   c} Zdededededdf
dZedk  rd nd
Zedk  rd
ndZdededededdf
dZdedee   dee   fd Zdedee   dee   fd!Z dededededef
d"Z!dededededef
d#Z"yc c} w )$    )deque)FunctionType)Any)Callable)Deque)List)Tuple)Bytecode)Assembly   )PYTHON_VERSION_INFO	_hook_argc                       e Zd ZdZy)InvalidLineza
    Raised when trying to inject a hook on an invalid line, e.g. a comment or a blank line.
    N)__name__
__module____qualname____doc__     Z/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/ddtrace/internal/injection.pyr   r      s    r   r   )      z
        push_null
        load_const      {hook}
        load_const      {arg}
        call            1
        pop_top
        )r      z
        push_null
        load_const      {hook}
        load_const      {arg}
        precall         1
        call            1
        pop_top
        zp
        load_const      {hook}
        load_const      {arg}
        call_function   1
        pop_top
        codehooklinenoargreturnNc                 p   t               }d}t        |       D ]Q  \  }}	 |j                  |k(  r|j                  }|j                  |k(  r |j                  dk7  r|j	                  |       S |st        d|z        |D ]'  }t        j                  t        ||      |      | || ) y# t
        $ r Y w xY w)zInject a hook at the given line number inside an abstract code object.

    The hook is called with the given argument, which is also used as an
    identifier for the hook itself. This should be kept in case the hook needs
    to be removed.
    NNOPz6Line %d does not exist or is either blank or a comment)r   r   )r   )
r   	enumerater   name
appendleftAttributeErrorr   INJECTION_ASSEMBLYbinddict)r   r   r   r   locslast_linenoiinstrs           r   _inject_hookr-   ?   s     wDKdO 5	||{*,,K ||v%%***=" RU[[\\  U&++Dd,DV+TQq	U  		s   B);B))	B54B5   linec                    t               }t        |       D ]  \  }}	 |j                  |k(  r| |t        z      j                  |k(  rg| |t
        z      j                  |u rOt        ||t        t              z         D cg c]  }| |   j                   c}t        k(  r|j                  |        |st        d|z        |D ]  }| ||t        t              z   =  yc c}w # t        $ r Y t        $ r Y w xY w)zEject a hook from the abstract code object at the given line number.

    The hook is identified by its argument. This ensures that only the right
    hook is ejected.
    zLine %d does not contain a hookN)r   r"   r   _INJECT_HOOK_OPCODE_POSr   _INJECT_ARG_OPCODE_POSrangelen_INJECT_HOOK_OPCODESr#   r$   r%   
IndexErrorr   )r   r   r/   r   r)   r+   r,   _s           r   _eject_hookr8   j   s    wDdO 5	 $44599TA33488C?+0A<P8Q4Q+RSaT!W\\SWkk"" ;dBCC 4Q122234 T  	 		s*   AC ;CC C  	C6+C65C6fhooksc                    t        j                  | j                        }g }|D ]  \  }}}	 t        ||||        t        |      t        |      k  r|j                         | _        |S # t        $ r |j                  |||f       Y ew xY w)a  Bulk-inject a list of hooks into a function.

    Hooks are specified via a list of tuples, where each tuple contains the hook
    itself, the line number and the identifying argument passed to the hook.

    Returns the list of hooks that failed to be injected.
    )r
   	from_code__code__r-   r   appendr4   to_coder9   r:   abstract_codefailedr   r/   r   s          r   inject_hooksrC      s     &&qzz2MF  -dC	-dC8- 6{SZ"**,
M  	-MM4s+,	-   A**B
	B
c                    t        j                  | j                        }g }|D ]  \  }}}	 t        ||||        t        |      t        |      k  r|j                         | _        |S # t        $ r |j                  |||f       Y ew xY w)zBulk-eject a list of hooks from a function.

    The hooks are specified via a list of tuples, where each tuple contains the
    hook line number and the identifying argument.

    Returns the list of hooks that failed to be ejected.
    )r
   r<   r=   r8   r   r>   r4   r?   r@   s          r   eject_hooksrF      s     &&qzz2MF  -dC	-tT37- 6{SZ"**,
M  	-MM4s+,	-rD   c                     t        j                  | j                        }t        ||||       |j	                         | _        | S )zInject a hook into a function.

    The hook is injected at the given line number and called with the given
    argument. The latter is also used as an identifier for the hook. This should
    be kept in case the hook needs to be removed.
    )r
   r<   r=   r-   r?   r9   r   r/   r   rA   s        r   inject_hookrI      s;     &&qzz2MdC0&&(AJHr   c                     t        j                  | j                        }t        ||||       |j	                         | _        | S )zzEject a hook from a function.

    The hook is identified by its line number and the argument passed to the
    hook.
    )r
   r<   r=   r8   r?   rH   s        r   
eject_hookrK      s;     &&qzz2MtT3/&&(AJHr   )#collectionsr   typesr   typingr   r   r   r   r	   bytecoder
   ddtrace.internal.assemblyr   compatr   PYHookTypeintHookInfoTypeHOOK_ARG_PREFIX	Exceptionr   r&   parser#   r5   r-   r1   r2   r8   rC   rF   rI   rK   )r7   s   0r   <module>rY      s           . - SE3JXsC'()  Z =	 7]		 	 );;1; $Ux $Ux $U $U3 $U4 $UN  "G|!  7l 4h 4h 4c 4 4 4>L l); \@R .< \(: tL?Q .< x s    , h c   W <s   $D%