o
    ij                     @   s  U d dl 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Zd dlmZ d dlmZ d d	lmZ d dlZd d
lmZ d dlmZ d dlmZ d dlmZ ejr^d dlmZ ejegdf Zejeegef Z ejej!egdf Z"ej#e$eje$ge%f f Z&ejej!egdf Z'ej#e$eje$ge%f f Z(ee)Z*da+g Z,e-e  e.d< g a/e-e e.d< dej!dej!de0e$ej!f fddZ1d<ddZ2de ddfddZ3de ddfddZ4deddfddZ5deddfd d!Z6d"edej7e
 fd#d$Z8d%e
dej7e
 fd&d'Z9d(e$dej7d) fd*d+Z:d,d- Z;d.ede%fd/d0Z<G d1d2 d2Z=G d3d4 d4e j>Z?G d5d6 d6e?Z@G d7d8 d8eZAd9ejg df ddfd:d;ZBdS )=    N)defaultdict)_init_module_attrs)
ModuleSpec)	find_specPath)CodeType)FunctionType)
ModuleType)WeakValueDictionary)
get_logger)get_argument_value)WrappingContext)Loader_run_module_transformers_post_run_module_hooksargskwargsreturnc               	   O   s   t d usJ ttt| |dd}ttt| |dd}tj| }tD ]}|||}q#|	dd  |g| dd  R }zt |i |W tj| }t
D ]}|| qJS tj| }t
D ]}|| qYw )Nr   code   mod_name   )	_run_codetcastr   r   strsysmodulesr   popr   )r   r   r   r   moduletransformernew_argshook r$   K/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/module.py_wrapped_run_code(   s$   




r&   c                  C   s$   t d u rdd l} | j a t| _ d S d S Nr   )r   runpyr&   )r(   r$   r$   r%   _patch_run_code@   s
   
r)   r!   c                 C      t |  dS )z"Register a run module transformer.N)r   appendr!   r$   r$   r%   register_run_module_transformerJ   s   r-   c                 C   r*   )z{Unregister a run module transformer.

    If the transformer was not registered, a ``ValueError`` exception is raised.
    N)r   remover,   r$   r$   r%   !unregister_run_module_transformerO   s   r/   r#   c                 C   s   t   t|  dS )zRegister a post run module hook.

    The hooks gets called after the module is loaded. For this to work, the
    hook needs to be registered during the interpreter initialization, e.g. as
    part of a sitecustomize.py script.
    N)r)   r   r+   r#   r$   r$   r%   register_post_run_module_hookW   s   	r1   c                 C   r*   )zrUnregister a post run module hook.

    If the hook was not registered, a ``ValueError`` exception is raised.
    N)r   r.   r0   r$   r$   r%   unregister_post_run_module_hooke   s   r2   r    c                 C   s   zt | dW S  tym   ztt | d }W n' ttfyA   ztt | dj }W n tttfy>   d}Y nw Y nw |durP|jdkrP|	d}|duriz|| _
W | Y S  tyh   Y | Y S w | Y S w )z)Get the origin source file of the module.__dd_origin____file____spec__Npycz.py)object__getattribute__AttributeErrorr   resolve	TypeErrororigin
ValueErrorsuffixwith_suffixr3   )r    origr$   r$   r%   r<   o   s2   	
r<   pathc                 C   sB   dd t jD D ]}| r||    }| r|  S qdS )z3Resolve a (relative) path with respect to sys.path.c                 s   s    | ]}t |V  qd S Nr   ).0_r$   r$   r%   	<genexpr>   s    z_resolve.<locals>.<genexpr>N)r   rA   is_dir
expanduserr:   is_file)rA   baseresolved_pathr$   r$   r%   _resolve   s   rK   fullnamer   c                 C   s   t t| dd S )Nloader)getattrr   )rL   r$   r$   r%   find_loader      rO   c                 C   s   t | d uS rB   )rO   )module_namer$   r$   r%   is_module_installed      rR   specc                 C   s   | j d u o	| jd uS rB   )r<   submodule_search_locations)rT   r$   r$   r%   is_namespace_spec   s   rV   c                	   @   sj  e Zd Zd&dejd deje ddfddZdd	 Zdedefd
dZ	dej
dejegdf ddfddZdej
dejegdf ddfddZdej
deddfddZdeddfddZdedeje fddZdd Zdededejejej
egdf  fddZdedejejej
egdf  fd d!Zdedejejej
egdf  fd"d#Zdeddfd$d%ZdS )'_ImportHookChainedLoaderNrM   r   rT   r   c                 C   sZ   || _ || _i | _i | _i | _|d u st|dr| j| _|d u s%t|dr+| j| _	d S d S )Ncreate_moduleexec_module)
rM   rT   	callbacksimport_exception_callbackstransformershasattr_create_modulerX   _exec_modulerY   )selfrM   rT   r$   r$   r%   __init__   s   z!_ImportHookChainedLoader.__init__c                 C   s   t | j|S rB   )rN   rM   )r`   namer$   r$   r%   __getattr__   s   z$_ImportHookChainedLoader.__getattr__c                 C   s6   t |j}d |_t||dd |j| _|  |_|_|S )NT)override)r
   rb   rM   r   
__loader__)r`   rT   r    r$   r$   r%   namespace_module   s   
z)_ImportHookChainedLoader.namespace_modulekeycallbackc                 C      || j |< d S rB   )rZ   r`   rg   rh   r$   r$   r%   add_callback      z%_ImportHookChainedLoader.add_callbackc                 C   ri   rB   )r[   rj   r$   r$   r%   add_import_exception_callback   rl   z6_ImportHookChainedLoader.add_import_exception_callbackr!   c                 C   ri   rB   )r\   )r`   rg   r!   r$   r$   r%   add_transformer   rl   z(_ImportHookChainedLoader.add_transformerr    c              	   C   s   z| j |_W n ttfy   Y nw z
| j t|d_ W n ttfy'   Y nw |jdkr4|t|j	 | j
 D ]}|| q9d S )NrT   pkg_resources)rM   re   r9   r;   r7   r8   __name__register_loader_typerW   DefaultProviderrZ   values)r`   r    rh   r$   r$   r%   	call_back   s   

z"_ImportHookChainedLoader.call_backrL   c                 C   sr   | j d u r| jd u rd S | | j tj| jj< }n| j |}z| | W |S  ty8   t	
d| Y |S w )N Failed to call back on module %s)rM   rT   rf   r   r   rb   load_modulert   	Exceptionlog	exception)r`   rL   r    r$   r$   r%   rv      s   

z$_ImportHookChainedLoader.load_modulec                 C   s,   | j d ur| j |S t|r| |S d S rB   )rM   rX   rV   rf   )r`   rT   r$   r$   r%   r^      s
   

z'_ImportHookChainedLoader._create_module
hooks_attrc              	   C   s   t jD ]@}t|trCz't||g D ]\}}t|tr||jks(t|r/||jr/|  W   S qW q tyB   t	j
d|dd Y qw qd S )Nz&Exception happened while processing %sT)exc_info)r   	meta_path
isinstanceModuleWatchdogrN   r   rp   callablerw   rx   debug)r`   r    rz   rD   condr#   r$   r$   r%   _find_first_hook  s(   


z)_ImportHookChainedLoader._find_first_hookc                 C      |  |dS )N_import_exception_hooksr   r`   r    r$   r$   r%   _find_first_exception_hook  rS   z3_ImportHookChainedLoader._find_first_exception_hookc                 C   r   )N_pre_exec_module_hooksr   r   r$   r$   r%   _find_first_pre_exec_hook  rS   z2_ImportHookChainedLoader._find_first_pre_exec_hookc           	         s:  t jdd   d ur+t  dds+ fdd}t|dd |jtjj_}|r8| nLjd u rRt dd }|d urQt|rQtj	|j
< n2zj W n) ty   }|d uro| t \}}}|d ur|d ur|j|_ w z W d S  ty   td Y d S w )	Nget_code_dd_get_codeFc                    s&    |}j  D ]}||}q	|S rB   )r\   rs   )_loaderrL   r   rh   	_get_coder    r`   r$   r%   r   "  s   z7_ImportHookChainedLoader._exec_module.<locals>.get_codeTr5   ru   )rN   rM   setattr__get__typer   r   rV   r   r   rb   rY   rw   r   r{   tb_next__traceback__rt   rx   ry   )	r`   r    r   pre_exec_hookrT   exception_hookrD   etbr$   r   r%   r_     s:   



z%_ImportHookChainedLoader._exec_modulerB   )rp   
__module____qualname__r   Optionalr   ra   rc   r
   rf   AnyCallablerk   rm   TransformerTypern   rt   r   rv   r^   r   r   r   r_   r$   r$   r$   r%   rW      s(    $$$	
((rW   c                
   @   s  e Zd ZU dZdZejd  ed< d"ddZd"ddZ	e
deje fd	d
Ze
d"ddZdeddfddZdededefddZd#dedeje dejd fddZ	d$dedeje deje deje fddZe
d"ddZe
dd Ze
d"d d!ZdS )%BaseModuleWatchdogz]Base module watchdog.

    Invokes ``after_import`` every time a new module is imported.
    N	_instancer   c              	   C   sx   t  | _tjd}|d ur:tdD ] }z|t|j W  d S  t	y2   ddl
m} |d Y qw td d S d S )Nro      r   )sleepg?z0Cannot ensure correct support with pkg_resources)set_findingr   r   getrangerq   rW   rr   r9   timer   rx   warning)r`   ro   rD   r   r$   r$   r%   ra   T  s   zBaseModuleWatchdog.__init__c                 C   s   t jd|  d S r'   )r   r|   insertr`   r$   r$   r%   _add_to_meta_pathm  s   z$BaseModuleWatchdog._add_to_meta_pathc                 C   s,   t tjD ]\}}t|| u r|  S qd S rB   )	enumerater   r|   r   )clsir|   r$   r$   r%   _find_in_meta_pathp  s
   z%BaseModuleWatchdog._find_in_meta_pathc                 C   s2   |   }|d u rtd| j d S tj| d S )Nz%s is not installed)r   rx   r   rp   r   r|   r   )r   r   r$   r$   r%   _remove_from_meta_pathw  s
   z)BaseModuleWatchdog._remove_from_meta_pathr    c                 C   s   t  rB   )NotImplementedErrorr   r$   r$   r%   after_import  s   zBaseModuleWatchdog.after_importr   _modulec                 C   s   |S rB   r$   )r`   r   r   r$   r$   r%   	transform  s   zBaseModuleWatchdog.transformrL   rA   r   c              	   C   s   || j v rd S | j | z;t|}|d ur@t|tst|n|}|t| | j |t| | j	 t
d|W | j | S W | j | d S | j | w )Nr   )r   addrO   r}   rW   rk   r   r   rn   r   r   r   r.   )r`   rL   rA   original_loaderrM   r$   r$   r%   find_module  s$   

zBaseModuleWatchdog.find_moduletargetc              	   C   s   || j v rd S | j | z_zt|}W n ty%   Y W | j | d S w |d u r3W | j | d S t|dd }t|tsHt	dt|||_
t	t|j
t| | j t	t|j
t| | j |W | j | S | j | w )NrM   r   )r   r   r   rw   r.   rN   r}   rW   r   r   rM   rk   r   r   rn   r   )r`   rL   rA   r   rT   rM   r$   r$   r%   r     s(   

zBaseModuleWatchdog.find_specc                 C   s.   |   rdS |  | _| j  td|  dS )zInstall the module watchdog.Nz%s installed)is_installedr   r   rx   r   r   r$   r$   r%   install  s
   
zBaseModuleWatchdog.installc                 C   s   | j duot| j | u S )z6Check whether this module watchdog class is installed.N)r   r   r   r$   r$   r%   r     s   zBaseModuleWatchdog.is_installedc                 C   s*   |   sdS |   d| _td|  dS )zUninstall the module watchdog.

        This will uninstall only the most recently installed instance of this
        class.
        Nz%s uninstalled)r   r   r   rx   r   r   r$   r$   r%   	uninstall  s
   zBaseModuleWatchdog.uninstallr   NrB   )NN)rp   r   r   __doc__r   r   r   __annotations__ra   r   classmethodintr   r   r
   r   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   r   L  s8   
 

	$

r   c                	       sX  e Zd ZdZd" fddZedejee	f fddZ
de	ddfd	d
Zededeje	 fddZedededdfddZedededdfddZedededdfddZedededdfddZededejegdf fddZeded  dededdfddZeded  dededdfddZeded  dedefd d!Z  ZS )#r~   ab  Module watchdog.

    Hooks into the import machinery to detect when modules are loaded/unloaded.
    This is also responsible for triggering any registered import hooks.

    Subclasses might customize the default behavior by overriding the
    ``after_import`` method, which is triggered on every module import, once
    the subclass is installed.
    r   Nc                    s.   t    tt| _d | _t | _t | _d S rB   )	superra   r   list	_hook_map_omr   r   r   r   	__class__r$   r%   ra     s
   

zModuleWatchdog.__init__c                 C   sp   dt jt dt jttf fdd}| jd u r5z|tj | _W | jS  t	y4   |t
tj  Y S w | jS )Nr   r   c              	   S   sH   t  }| D ]}t|}|d u rqz||t|< W q ty!   Y qw |S rB   )wvdictr<   r   r;   )r   resultmmodule_originr$   r$   r%   modules_with_origin  s   z7ModuleWatchdog._origin_map.<locals>.modules_with_origin)r   Iterabler
   MutableMappingr   r   r   r   rs   RuntimeErrorr   )r`   r   r$   r$   r%   _origin_map  s   "
zModuleWatchdog._origin_mapr    c                 C   s   t |}|d urt|nd }|d ur|| j|< g }|d ur*|| jv r*|| j|  |j| jv r9|| j|j  |rNtdt||j |D ]}|| qGd S d S )Nz4Calling %d registered hooks on import of module '%s')	r<   r   r   r   extendrp   rx   r   len)r`   r    module_pathrA   hooksr#   r$   r$   r%   r     s   

zModuleWatchdog.after_import_originc                 C   s|   |   sdS tt| j}t|}|dur<t|}|j|}|dur%|S t	j
d}|dur<t||kr<||j|< |S dS )zLookup a module by its origin.N__main__)r   r   r   r~   r   rK   r   r   r   r   r   r<   )r   r   instancerJ   rA   r    main_moduler$   r$   r%   get_by_origin)  s   
zModuleWatchdog.get_by_originr<   r#   c                 C   s   |    t|}|du rtd| dS t|}td|| tt| j	}|j
| | z#||}|du r;W dS ztj|j  W n tyO   |j|=  w W n
 ty[   Y dS w td||j || dS )zRegister a hook to be called when the module with the given origin is
        imported.

        The hook will be called with the module object as argument.
        NzCannot resolve module origin %sz"Registering hook '%r' on path '%s'1Calling hook '%r' on already imported module '%s')r   rK   rx   r   r   r   r   r   r~   r   r   r+   r   r   r   rp   KeyErrorr   )r   r<   r#   rJ   rA   r   r    r$   r$   r%   register_origin_hookB  s2   
z#ModuleWatchdog.register_origin_hookc                 C   s   |   sdS t|}|du rtd| dS t|}tt| j}||j	vr.td| dS z||j	v rG|j	| }|
| |sJ|j	|= W dS W dS W dS  ty]   td|| Y dS w )zZUnregister the hook registered with the given module origin and
        argument.
        Nz#Module origin %s cannot be resolvedz!No hooks registered for origin %sz$Hook %r not registered for origin %s)r   rK   rx   r   r   r   r   r~   r   r   r.   r=   )r   r<   r#   rJ   rA   r   r   r$   r$   r%   unregister_origin_hookm  s.   



z%ModuleWatchdog.unregister_origin_hookc                 C   sr   |    td|| tt| j}|j| | zt	j
| }W n
 ty+   Y dS w td|| || dS )zRegister a hook to be called when the module with the given name is
        imported.

        The hook will be called with the module object as argument.
        z$Registering hook '%r' on module '%s'Nr   )r   rx   r   r   r   r~   r   r   r+   r   r   r   )r   r    r#   r   module_objectr$   r$   r%   register_module_hook  s   z#ModuleWatchdog.register_module_hookc                 C   s   |   sdS tt| j}||jvrtd| dS z||jv r3|j| }|| |s6|j|= W dS W dS W dS  t	yI   td|| Y dS w )zXUnregister the hook registered with the given module name and
        argument.
        Nz!No hooks registered for module %sz$Hook %r not registered for module %r)
r   r   r   r~   r   r   rx   r   r.   r=   )r   r    r#   r   r   r$   r$   r%   unregister_module_hook  s$   



z%ModuleWatchdog.unregister_module_hookc                    s   dt dd f fdd}|S )Nr#   r   c                    s     |  d S rB   )r   r0   r   r    r$   r%   rD     rP   z/ModuleWatchdog.after_module_imported.<locals>._)ModuleHookType)r   r    rD   r$   r   r%   after_module_imported  s   z$ModuleWatchdog.after_module_importedr   r   c                 C   s8   |    td|| tt| j}|j||f dS )a[  Register a hook to execute before/instead of exec_module.

        The pre exec_module hook is executed before the module is executed
        to allow for changed modules to be executed as needed. To ensure
        that the hook is applied only to the modules that are required,
        the condition is evaluated against the module name.
        z7Registering pre_exec module hook '%r' on condition '%s'N)	r   rx   r   r   r   r~   r   r   r   r   r   r#   r   r$   r$   r%   register_pre_exec_module_hook  s   z,ModuleWatchdog.register_pre_exec_module_hookc                 C   s"   t t| j}|j||f dS )zSRegister a hook to execute before/instead of exec_module. Only for testing proposesN)r   r   r~   r   r   r.   r   r$   r$   r%   remove_pre_exec_module_hook  s   z*ModuleWatchdog.remove_pre_exec_module_hookc                 C   s*   |    tt| j}|j||f d S rB   )r   r   r   r~   r   r   r   r   r$   r$   r%   register_import_exception_hook  s   z-ModuleWatchdog.register_import_exception_hookr   ) rp   r   r   r   ra   propertyr   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   PreExecHookCondPreExecHookTyper   r   ImportExceptionHookCondImportExceptionHookTyper   __classcell__r$   r$   r   r%   r~     sL    
 *  r~   c                       s*   e Zd Zdejdejf fddZ  ZS )LazyWrappingContextvaluer   c                    s   | j j| j j t |S rB   )	__frame__	f_globalsupdatef_localsr   
__return__)r`   r   r   r$   r%   r     s   zLazyWrappingContext.__return__)rp   r   r   r   r   r   r   r$   r$   r   r%   r     s    "r   fc                    sF   t tt  tdj dtdtj	f fdd}| d< d S )Nr   rb   r   c                    sB     z |  W S  t y    td d d| }d|_|w )Nzmodule rp   z has no attribute T)r   r9   __suppress_context__)rb   h_globalsr   r$   r%   rc     s   
zlazy.<locals>.__getattr__rc   )
r   r   r   r	   wrapr   	_getframer   r   r   )r   rc   r$   r   r%   lazy  s   	r   r   )Cabccollectionsr   importlib._bootstrapr   importlib.machineryr   importlib.utilr   pathlibr   r   typesr   r	   r
   typingr   weakrefr   r   ddtrace.internal.loggerr   ddtrace.internal.utilsr   !ddtrace.internal.wrapping.contextr   TYPE_CHECKINGimportlib.abcr   r   r   r   r   r   Unionr   boolr   r   r   rp   rx   r   r   r   r   r   dictr&   r)   r-   r/   r1   r2   r   r<   rK   rO   rR   rV   rW   ABCr   r~   r   r   r$   r$   r$   r%   <module>   s^   
 $


! #    	