o
    i                     @   s  U 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dee fddZejdedee fddZejdedee fddZedddede	fddZdede	fddZdedede	defdd Zdedee fd!d"Ze a eeef e!d#< ded$edd%fd&d'Z"ed(ddede#e fd)d*Z$dede#e fd+d,Z%d-d. Z&d%S )/    )deque)findlinestarts)	lru_cache)partial)singledispatch)Path)CodeType)FunctionType)Iterator)MutableMapping)cast)_isinstance)cachedreturnc                 C   s   t  N)NotImplementedError)_ r   U/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/utils/inspection.pylinenos   s   r   codec                 C   s   dd t | D | jh S )z#Get the line numbers of a function.c                 S   s   h | ]
\}}|d ur|qS r   r   ).0r   lnr   r   r   	<setcomp>   s    z_.<locals>.<setcomp>)r   co_firstlinenor   r   r   r   r      s   r   fc                 C   
   t | jS r   )r   __code__)r   r   r   r   r      s   
i   )maxsizefilenamec                 C   s   t |  S r   )r   resolve)r    r   r   r   _filename_to_resolved_path!   s   r"   c                 C   r   r   )r"   co_filenamer   r   r   r   resolved_code_origin&   s   
r$   namepathc                    s  fdd}| h}t | g}|ro|  dD ]/}z$t |}t|tr=||vr=||r3|W   S || || W q tyG   Y qw t t	rt
t	 }||jr]t
t|jS |jD ]}	t|	tr}|	|vr}||	rs|	  S ||	 ||	 q`|j D ]}	t|	tr|	|vr||	r|	  S ||	 ||	 qt trdd  jpg D D ]}
|
|vr||
r|
  S ||
 ||
 qz	t d  W S  ty   Y nw zt d D ]}t|tr||vr||r|  W S qW n? ty:   z* fddt d	D D ]}t|tr*||vr*||r*|  W  Y S qW n
 ty7   Y nw Y nw z' fd
dt D D ]}t|tr_||vr_||r_|  W S qHW n
 tyl   Y nw |s| S )Nc                    s   | j j kot| j kS r   )r   co_namer$   g)r%   r&   r   r   match0   s   zundecorated.<locals>.match)__wrapped__funcc                 s   s"    | ]}t |jtr|jV  qd S r   )r   cell_contentsr	   r   r   r   r   r   	<genexpr>\   s     zundecorated.<locals>.<genexpr>__dd_wrapped____dict__c                 3       | ]	}t  |V  qd S r   object__getattribute__r.   r(   r   r   r/   v       	__slots__c                 3   r2   r   r3   )r   ar(   r   r   r/   ~   r6   )r   popleftr4   r5   r   r	   appendaddAttributeErrorr   r   r,   argskeywordsvalues__closure____dir__)r   r%   r&   r*   seen_functionsqattrwrappedpargcvr   )r)   r%   r&   r   undecorated*   s   















 
 
NrJ   c                 c   sJ    t | g}|r#| }dd |jD D ]
}|V  || q|sd S d S )Nc                 s   s    | ]
}t |tr|V  qd S r   )
isinstancer   r.   r   r   r   r/      s    z'collect_code_objects.<locals>.<genexpr>)r   r9   	co_constsr:   )r   rC   rH   new_coder   r   r   collect_code_objects   s   
rN   "_CODE_TO_ORIGINAL_FUNCTION_MAPPINGfunctionNc                 C   s   |t | < dS )z
    Link a function to a code object. This is used to speed up the search for
    the original function from a code object.
    N)rO   )r   rP   r   r   r   link_function_to_code   s   rQ   i @  c                    s    dd l } fdd| D S )Nr   c                    s$   g | ]}t |tr|j u r|qS r   )rK   r	   r   r.   r   r   r   
<listcomp>   s   $ z*_functions_for_code_gc.<locals>.<listcomp>)gcget_referrers)r   rS   r   r   r   _functions_for_code_gc   s   rU   c                 C   s(   zt |  gW S  ty   t|  Y S w r   )rO   KeyErrorrU   r   r   r   r   functions_for_code   s
   rW   c                   C   s   t   t  dS )zClear the inspection state.

    This should be called when modules are reloaded to ensure that the mappings
    stay relevant.
    N)rU   cache_clearrO   clearr   r   r   r   rY      s   rY   )'collectionsr   disr   	functoolsr   r   r   pathlibr   typesr   r	   typingr
   r   r   ddtrace.internal.safetyr   ddtrace.internal.utils.cacher   setintr   registerr   strr"   r$   rJ   rN   dictrO   __annotations__rQ   listrU   rW   rY   r   r   r   r   <module>   s<    ]	
