o
    Á¿i‡  ã                   @   sâ   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 d dl
mZ d dlmZ d dlmZ d dlmZ eeeeegef ZG dd„ de	eƒZG dd„ deƒZdS )é    )ÚCodeType)ÚFunctionType)ÚAny)ÚCallable)ÚOptional)Úcast)Ú
FullyNamed)ÚHookInfoType)ÚHookType©Úeject_hooks©Úinject_hooks)Úget_function_code)ÚContextWrappedFunction)ÚWrappingContextc                   @   s   e Zd ZdZdS )Ú FullyNamedContextWrappedFunctionzA fully named wrapper function.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úU/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/debugging/_function/store.pyr      s    r   c                
   @   s  e Zd ZdZd"deee  ddfdd„Zd#dd„Zd	e	ddfd
d„Z
deddfdd„Zdedee dee fdd„Zdedee dee fdd„Zdededede	def
dd„Zdededede	def
dd„Zdededdfdd„Zdeddfdd„Zd$d d!„ZdS )%ÚFunctionStoreaê  Function object store.

    This class provides a storage layer for patching operations, which allows us
    to store the original code object of functions being patched with either
    hook injections or wrapping. This also enforce a single wrapping layer.
    Multiple wrapping is implemented as a list of wrappers handled by the single
    wrapper function.

    If extra attributes are defined during the patching process, they will get
    removed when the functions are restored.
    NÚextra_attrsÚreturnc                 C   s,   i | _ i | _dg| _|r| j |¡ d S d S )NÚ__dd_context_wrapped__)Ú	_code_mapÚ_wrapper_mapÚ_extra_attrsÚextend)Úselfr   r   r   r   Ú__init__&   s   ÿzFunctionStore.__init__c                 C   s   | S ©Nr   )r!   r   r   r   Ú	__enter__-   s   zFunctionStore.__enter__Úexcc                 G   s   |   ¡  d S r#   )Úrestore_all)r!   r%   r   r   r   Ú__exit__0   s   zFunctionStore.__exit__Úfunctionc                 C   s    || j vrt|ƒ| j |< d S d S r#   )r   r   ©r!   r(   r   r   r   Ú_store3   s   
ÿzFunctionStore._storeÚhooksc                 C   sX   zt t|ƒ}t t|jjƒ}W n ty   t t|ƒ}Y nw |  |¡ dd„ t||ƒD ƒS )zyBulk-inject hooks into a function.

        Returns the set of probe IDs for those probes that failed to inject.
        c                 S   ó   h | ]\}}}|j ’qS r   ©Úprobe_id©Ú.0Ú_Úpr   r   r   Ú	<setcomp>B   ó    z-FunctionStore.inject_hooks.<locals>.<setcomp>)r   r   r   r   Ú__wrapped__ÚAttributeErrorr*   r   ©r!   r(   r+   Úwrapped_funcÚfr   r   r   r   7   s   
ÿ
zFunctionStore.inject_hooksc                 C   sH   zt t|ƒ}|jj}W n ty   |}Y nw dd„ tt t|ƒ|ƒD ƒS )zwBulk-eject hooks from a function.

        Returns the set of probe IDs for those probes that failed to eject.
        c                 S   r,   r   r-   r/   r   r   r   r3   P   r4   z,FunctionStore.eject_hooks.<locals>.<setcomp>)r   r   r   r5   r6   r   r   r7   r   r   r   r   D   s   
þzFunctionStore.eject_hooksÚhookÚlineÚargc                 C   ó   |   ||||fg¡  S )zInject a hook into a function.r   ©r!   r(   r:   r;   r<   r   r   r   Úinject_hookR   ó   zFunctionStore.inject_hookc                 C   r=   )zEject a hook from a function.r   r>   r   r   r   Ú
eject_hookV   r@   zFunctionStore.eject_hookÚwrapping_contextc                 C   s    |   |¡ || j|< | ¡  dS )zWrap a function with a hook.N)r*   r   Úwrap)r!   r(   rB   r   r   r   rC   Z   s   

zFunctionStore.wrapc                 C   s   | j  tt|ƒ¡ ¡  dS )z(Unwrap a hook around a wrapped function.N)r   Úpopr   r   Úunwrapr)   r   r   r   rE   `   s   zFunctionStore.unwrapc              
   C   sJ   | j  ¡ D ]\}}||_| jD ]}zt||ƒ W q ty!   Y qw qdS )z9Restore all the patched functions to their original form.N)r   ÚitemsÚ__code__r   Údelattrr6   )r!   r(   ÚcodeÚattrr   r   r   r&   d   s   
ÿýþzFunctionStore.restore_allr#   )r   r   )r   N)r   r   r   r   r   ÚlistÚstrr"   r$   r   r'   r   r*   r   r	   Úsetr   r   r
   ÚintÚboolr?   rA   r   rC   rE   r&   r   r   r   r   r      s    
r   N)Útypesr   r   Útypingr   r   r   r   Ú%ddtrace.debugging._function.discoveryr   Ú#ddtrace.internal.bytecode_injectionr	   r
   r   r   Úddtrace.internal.wrappingr   Ú!ddtrace.internal.wrapping.contextr   r   ÚWrapperTyper   Úobjectr   r   r   r   r   Ú<module>   s"    