
    uYfN                        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mZ d dl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 ej2                  egdf   Zej2                  ej6                  egdf   Zej:                  eej2                  egef   f   Z  ee!      Z"da#g a$ejJ                  e   e&d<   dej6                  dej6                  dejN                  eej6                  f   fdZ(d$dZ)deddfdZ*deddfdZ+dedejX                  e   fdZ-dedejX                  e   fdZ.dedejX                  e   fdZ/d Z0dedefdZ1 G d d      Z2 G d  d!e jf                        Z4 G d" d#e4      Z5y)%    N)defaultdict)_init_module_attrs)Loader)
ModuleSpec)	find_specPath)
ModuleType)WeakValueDictionary)
get_logger)get_argument_value_post_run_module_hooksargskwargsreturnc                      t         J t        | |dd      }	 t        | i |t        j                  |   }t        D ]
  } ||        S # t        j                  |   }t        D ]
  } ||        w xY w)N   mod_name)	_run_coder   sysmodulesr   )r   r   r   modulehooks        W/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/ddtrace/internal/module.py_wrapped_run_coder      s~    
    !$:>H$)&)X&* 	DL	 X&* 	DL	s   
A	 	(A1c                  H    t         dd l} | j                   a t        | _         y y Nr   )r   runpyr   )r   s    r   _patch_run_coder   .   s"     OO	+	     r   c                 B    t                t        j                  |        y)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   appendr   s    r   register_post_run_module_hookr$   8   s     !!$'r    c                 .    t         j                  |        y)zrUnregister a post run module hook.

    If the hook was not registered, a ``ValueError`` exception is raised.
    N)r   remover#   s    r   unregister_post_run_module_hookr'   F   s     !!$'r    r   c                    	 t        t        j                  | d            j                         }|2|j                         r"|j                  dk(  r|j                  d      S |S y# t        t
        f$ rX 	 t        t        j                  | d      j                        j                         }n# t        t        t
        f$ r d}Y nw xY wY w xY w)z)Get the origin source file of the module.__file____spec__Nz.pycz.py)r	   object__getattribute__resolveAttributeError	TypeErrororigin
ValueErroris_filesuffixwith_suffix)r   origs     r   r0   r0   P   s    
F++FJ?@HHJ DLLN*.++*?t&ITI I& 	//
CJJKSSUD
I6 	D	s5   -A$ $C47B,+C,CCCC
Cpathc                     d t         j                  D        D ]H  }|j                         s|| j                         z  j	                         }|j                         sF|c S  y)z3Resolve a (relative) path with respect to sys.path.c              3   2   K   | ]  }t        |        y wNr   ).0_s     r   	<genexpr>z_resolve.<locals>.<genexpr>f   s     +Qa+s   N)r   r6   is_dir
expanduserr-   r2   )r6   baseresolved_paths      r   _resolverA   d   sT    +#((+ %;;=!DOO$55>>@M$$&$$	%
 r    fullnamec                 .    t        t        |       dd       S Nloader)getattrr   )rB   s    r   find_loaderrG   r   s    9X&$77r    c                     t        |       d uS r9   )rG   )module_names    r   is_module_installedrJ   v   s    {#4//r    specc                 >    | j                   d u xr | j                  d uS r9   )r0   submodule_search_locations)rK   s    r   is_namespace_specrN   z   s!    ;;$N4#B#B$#NNr    c                      e Zd Zddej                  e   dej                  e   ddfdZd Zdede	fdZ
dej                  d	ej                  e	gdf   ddfd
Zde	ddfdZdedej                  e	   fdZd Zde	ddfdZy)_ImportHookChainedLoaderNrE   rK   r   c                     || _         || _        i | _        |t        |d      r| j                  | _        |t        |d      r| j                  | _        y y )Ncreate_moduleexec_module)rE   rK   	callbackshasattr_create_modulerR   _exec_modulerS   )selfrE   rK   s      r   __init__z!_ImportHookChainedLoader.__init__   sV    	HJ >WV_=!%!4!4D>WV];#00D <r    c                 .    t        | j                  |      S r9   )rF   rE   )rX   names     r   __getattr__z$_ImportHookChainedLoader.__getattr__   s    t{{D))r    c                     t        |j                        }d |_        t        ||d       |j                  | _        | x|_        |_        |S )NT)override)r
   r[   rE   r   
__loader__)rX   rK   r   s      r   namespace_modulez)_ImportHookChainedLoader.namespace_module   sF    DII& 4$7 kk*..DKr    keycallbackc                 "    || j                   |<   y r9   )rT   )rX   ra   rb   s      r   add_callbackz%_ImportHookChainedLoader.add_callback   s    &sr    r   c                     |j                   dk(  r |j                  t        |j                         | j                  j                         D ]
  } ||        y Npkg_resources)__name__register_loader_typerP   DefaultProviderrT   values)rX   r   rb   s      r   	call_backz"_ImportHookChainedLoader.call_back   sK    ??o- ''(@&BXBXY--/ 	HV	r    rB   c                    | j                   P| j                  y | j                  | j                        xt        j                  | j                  j
                  <   }n| j                   j                  |      }| j                  |       |S r9   )rE   rK   r`   r   r   r[   load_modulerl   )rX   rB   r   s      r   rn   z$_ImportHookChainedLoader.load_module   sh    ;;yy 373H3H3SSCKK		'&[[,,X6Fvr    c                     | j                   | j                   j                  |      S t        |      r| j                  |      S y r9   )rE   rR   rN   r`   )rX   rK   s     r   rV   z'_ImportHookChainedLoader._create_module   s=    ;;";;,,T22T"((..r    c                 J   d }t         j                  D ]l  }t        |t              rV	 |j                  D ]F  \  }}t        |t
              r||j                  k(  st        |      s1 ||j                        sD|} n |l n |r
 || |       n_| j                  8t        |dd       }|Dt        |      r9|t         j                  |j                  <   n| j                  j!                  |       | j#                  |       y # t        $ r t        j                  dd       Y w xY w)Nz9Exception happened while processing pre_exec_module_hooksT)exc_infor*   )r   	meta_path
isinstanceModuleWatchdog_pre_exec_module_hooksstrrh   callable	ExceptionlogdebugrE   rF   rN   r   r[   rS   rl   )rX   r   pre_exec_hookr;   condr   rK   s          r   rW   z%_ImportHookChainedLoader._exec_module   s	    	A!^,	j&'&>&> "
d&tS1dfoo6M$TNtFOO/D -1M!" (	  $'{{"vz48#(9$(?-3CKK		*''/v! ! jIIYdhIijs   <C?$C?7C?? D"!D"r9   )rh   
__module____qualname__tOptionalr   r   rY   r\   r
   r`   AnyCallablerd   rl   rv   rn   rV   rW    r    r   rP   rP   ~   s    
1qzz&1 
1J9O 
1[_ 
1*Z J ' 'ZL$<N1O 'TX '
 t 
C 
AJJz,B 
: $ r    rP   c            
          e Zd ZU dZdZej                  d    ed<   ddZddZ	e
dej                  e   fd       Ze
dd       Zd	eddfd
Zddedej                  e   dej                  e   fdZ	 ddedej                  e   dej                  e   dej                  e   fdZe
dd       Ze
dd       Ze
d        Ze
dd       Zy)BaseModuleWatchdogz]Base module watchdog.

    Invokes ``after_import`` every time a new module is imported.
    N	_instancer   c                     t               | _        t        j                  j	                  d      }|!|j                  t        |j                         y y rf   )set_findingr   r   getri   rP   rj   )rX   rg   s     r   rY   zBaseModuleWatchdog.__init__   sA    $'E 8$../GIfIfg %r    c                 D    t         j                  j                  d|        y r   )r   rr   insert)rX   s    r   _add_to_meta_pathz$BaseModuleWatchdog._add_to_meta_path   s    Q%r    c                 h    t        t        j                        D ]  \  }}t        |      | u s|c S  y r9   )	enumerater   rr   type)clsirr   s      r   _find_in_meta_pathz%BaseModuleWatchdog._find_in_meta_path   s4    %cmm4 	LAyI#%	 r    c                     | j                         }|t        d| j                  z        t        j                  j                  |       y Nz%s is not installed)r   RuntimeErrorrh   r   rr   pop)r   r   s     r   _remove_from_meta_pathz)BaseModuleWatchdog._remove_from_meta_path   s<    ""$94s||CDD!r    r   c                     t               r9   )NotImplementedError)rX   r   s     r   after_importzBaseModuleWatchdog.after_import  s    !##r    rB   r6   c                    || j                   v ry | j                   j                  |       	 t        |      }|wt        |t              st	        |      n|}|j                  t        |       | j                         t        j                  t        |      | j                   j                  |       S 	 | j                   j                  |       y # | j                   j                  |       w xY wr9   )r   addrG   rs   rP   rd   r   r   r   castr   r&   )rX   rB   r6   original_loaderrE   s        r   find_modulezBaseModuleWatchdog.find_module  s    t}}$(#	+)(3O* &o7OP -_=(  ##DJ0A0ABvvff- MM  * + MM  * MM  *s   A(C C*targetc                    || j                   v ry | j                   j                  |       	 	 t        |      }|	 | j                   j	                  |       y t        |dd       }t        |t              s)t        j                  t        t        ||            |_        t        j                  t        |j                        j                  t        |       | j                         || j                   j	                  |       S # t        $ r Y | j                   j	                  |       y w xY w# | j                   j	                  |       w xY wrD   )r   r   r   rx   r&   rF   rs   rP   r   r   r   rE   rd   r   r   )rX   rB   r6   r   rK   rE   s         r   r   zBaseModuleWatchdog.find_spec   s    t}}$(#	+ * | MM  * T8T2Ff&>?ffV-Efd-STFF+T[[9FFtDzSWSdSde MM  *!   MM  *!  MM  *s/   D D+ BD+ 	D(
D+ 'D((D+ +Ec                 T    | j                         st        d| j                  z        y r   )is_installedr   rh   r   s    r   _check_installedz#BaseModuleWatchdog._check_installed>  s(    !4s||CDD "r    c                     | j                         rt        d| j                  z         |        | _        | j                  j	                          t
        j                  d|        y)zInstall the module watchdog.z%s is already installedz%s installedN)r   r   rh   r   r   ry   rz   r   s    r   installzBaseModuleWatchdog.installC  sL     83<<GHH'')		.#&r    c                 P    | j                   duxr t        | j                         | u S )z6Check whether this module watchdog class is installed.N)r   r   r   s    r   r   zBaseModuleWatchdog.is_installedM  s&     }}D(GT#---@C-GGr    c                 ~    | j                          | j                          d| _        t        j	                  d|        y)zUninstall the module watchdog.

        This will uninstall only the most recently installed instance of this
        class.
        Nz%s uninstalled)r   r   r   ry   rz   r   s    r   	uninstallzBaseModuleWatchdog.uninstallR  s2     	""$		"C(r    r   Nr9   )NN)rh   r}   r~   __doc__r   r   r   __annotations__rY   r   classmethodintr   r   r
   r   rv   r   r   r   r   r   r   r   r   r   r    r   r   r      s8   
 37Iqzz./6h& 1::c?    $: $$ $C qzz# !**U[J\ 2 ]a++#$::c?+CD::jCY+	
J	+< E E ' ' H H ) )r    r   c            	           e Zd ZdZd fdZedej                  ee	f   fd       Z
de	ddfdZededej                  e	   fd	       Zed
ededdfd       Zed
ededdfd       Zedededdfd       Zedededdfd       Zededej*                  egdf   fd       Zedej.                  d    dededdfd       Z xZS )rt   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                 v    t         |           t        t              | _        d | _        t               | _        y r9   )superrY   r   list	_hook_map_omr   ru   )rX   	__class__s    r   rY   zModuleWatchdog.__init__l  s1    EPQUEV8< Y\X]#r    c                    dt         j                  t           dt         j                  t        t         j
                  f   fd}| j                  6	  |t        j                  j                               | _        | j                  S | j                  S # t        $ r0  |t        t        j                  j                                     cY S w xY w)Nr   r   c                     t               }| D ]  }t        |      }|	 ||t        |      <   ! t	        j
                  t        j                  t        t        j                  f   |      S # t        $ r Y hw xY wr9   )wvdictr0   rv   r/   r   r   Dictr   )r   resultmmodule_origins       r   modules_with_originz7ModuleWatchdog._origin_map.<locals>.modules_with_originw  sx    #XF  &q	 (12F3}-. 66!&&aee,f55 !  s   A**	A65A6)r   Iterabler
   r   rv   r   r   r   r   rk   r   r   )rX   r   s     r   _origin_mapzModuleWatchdog._origin_mapu  s    	6J)? 	6AFF3PQPUPU:DV 	6( 88G.s{{/A/A/CD xxtxx   G
 +40B0B0D+EFFGs   )B 6C
	C
r   c                    t        |      }|t        |      nd }||| j                  |<   g }|,|| j                  v r|j	                  | j                  |          |j
                  | j                  v r(|j	                  | j                  |j
                            |r:t        j                  dt        |      |j
                         |D ]
  } ||        y y )Nz4Calling %d registered hooks on import of module '%s')	r0   rv   r   r   extendrh   ry   rz   len)rX   r   module_pathr6   hooksr   s         r   r   zModuleWatchdog.after_import  s    Vn#.#:s;%+DT"  6LL-.??dnn,LL89IILcRWjZ`ZiZij V r    _originc                 Z   | j                          t        j                  t        | j                        }t        |      }|jt        |      }|j                  j                  |      }||S t        j                  j                  d      }|t        |      |k(  r||j                  |<   |S y)zLookup a module by its origin.N__main__)r   r   r   rt   r   rA   rv   r   r   r   r   r0   )r   r   instancer@   r6   r   main_modules          r   get_by_originzModuleWatchdog.get_by_origin  s     	66.#--8 )$}%D))--d3F! ++//*5K&6++>$+F-8$$T*""r    r0   r   c                 &   | j                          t        |      }|t        d|z        t        |      }t        j                  d||       t        j                  t        | j                        }|j                  |   j                  |       	 |j                  |   }	 t        j                  |j                      	 t        j                  d||j                          ||       y# t         $ r |j                  |=  w xY w# t         $ r Y yw xY w)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   rA   r1   rv   ry   rz   r   r   rt   r   r   r"   r   r   r   rh   KeyError)r   r0   r   r@   r6   r   r   s          r   register_origin_hookz#ModuleWatchdog.register_origin_hook  s     	
 !( >GHH=!		6dC66.#--84 ''-	))$/FFOO, 			EtV__]V  ((.  		s$   D !C) )DD 	DDc                    | j                          t        |      }|t        d|      t        |      }t	        j
                  t        | j                        }||j                  vrt        d|z        	 ||j                  v r1|j                  |   }|j                  |       |s|j                  |= yyy# t        $ r t        d|d|      w xY w)zZUnregister the hook registered with the given module origin and
        argument.
        Nz#Module origin %s cannot be resolvedz!No hooks registered for origin %sHook z not registered for origin )
r   rA   r1   rv   r   r   rt   r   r   r&   )r   r0   r   r@   r6   r   r   s          r   unregister_origin_hookz%ModuleWatchdog.unregister_origin_hook  s    
 	 ( BFKK=!66.#--8x)))@6IJJ	Vx))) **40T" **40  *
  	VtVTUU	Vs   6=B6 6Cc                 Z   | j                          t        j                  d||       t        j                  t
        | j                        }|j                  |   j                  |       	 t        j                  |   }t        j                  d||        ||       y# t        $ r Y yw xY w)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   ry   rz   r   r   rt   r   r   r"   r   r   r   )r   r   r   r   module_objects        r   register_module_hookz#ModuleWatchdog.register_module_hook   s     			8$G66.#--86"))$/	KK/M 			EtVT]  		s   +B 	B*)B*c                 `   | j                          t        j                  t        | j                        }||j
                  vrt        d|z        	 ||j
                  v r1|j
                  |   }|j                  |       |s|j
                  |= yyy# t        $ r t        d|d|      w xY w)zXUnregister the hook registered with the given module name and
        argument.
        z!No hooks registered for module %sr   z not registered for module N)r   r   r   rt   r   r   r1   r&   )r   r   r   r   r   s        r   unregister_module_hookz%ModuleWatchdog.unregister_module_hook  s    
 	66.#--8+++@6IJJ	V+++ **62T" **62  ,
  	VtVTUU	Vs   =B B-c                 (     dt         dd f fd}|S )Nr   r   c                 *    j                  |        y r9   )r   )r   r   r   s    r   r;   z/ModuleWatchdog.after_module_imported.<locals>._,  s    $$VT2r    )ModuleHookType)r   r   r;   s   `` r   after_module_importedz$ModuleWatchdog.after_module_imported*  s    	3N 	3t 	3 r    r   r|   c                     | j                          t        j                  d||       t        j                  t
        | j                        }|j                  j                  ||f       y)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   ry   rz   r   r   rt   r   ru   r   )r   r|   r   r   s       r   register_pre_exec_module_hookz,ModuleWatchdog.register_pre_exec_module_hook1  sN     			KTSWX66.#--8''++T4L9r    r   )rh   r}   r~   r   rY   propertyr   r   rv   r
   r   r   r   r	   r   r   r   r   r   r   r   r   r   TypePreExecHookCondPreExecHookTyper   __classcell__)r   s   @r   rt   rt   a  s   ^ QVVCO4  @: $ $ D QZZ
-C  . #$ #n # # #J VD V V4 V V2 # ^   * VC V~ V$ V V& 3 1::~>NPT>T3U   :VV$%:-<:DS:	: :r    rt   r   )6abccollectionsr   importlib._bootstrapr   importlib.abcr   importlib.machineryr   importlib.utilr   pathlibr	   r   typesr
   typingr   weakrefr   r   ddtrace.internal.loggerr   ddtrace.internal.utilsr   r   r   r   r   Unionrv   boolr   rh   ry   r   r   Listr   r   r   r   r$   r'   r   r0   rA   rG   rJ   rN   rP   ABCr   rt   r   r    r   <module>r      s   
 # 3   * $  
   1 . 5 ZL$./**aeeZ0$67''#qzz3%+667  	13 ~. 3QUU aee sAEEz8J ",( (4 ((. (T (: !**T"2 (4 AJJt, 8# 8!**V"4 80OJ O4 O` `F}) })@_:' _:r    