a
    xd                     @   sF  d dl Z d dlZd dl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 d dlmZmZmZ d dlmZ erd 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mZ zd dlmZ W n ey   d dlmZ Y n0 e dejdd  ej dZ!G dd deZ"dd Z#dddZ$dd Z%dd Z&dS )    N)Hub)Integration)add_global_event_processor)EnvironHeaders)capture_internal_exceptionslogger	safe_repr)MYPY)Any)Callable)Dict)Optional)List)EventHint)HTTPConnectionz%s.%s.%s   )nameversionbuildc                   @   s   e Zd ZdZedd ZdS )StdlibIntegrationstdlibc                  C   s   t   t  tdd } d S )Nc                 S   s:   t jtd ur6| di }t|tr6d|vr6t|d< | S )NcontextsZruntime)r   currentget_integrationr   
setdefault
isinstancedict_RUNTIME_CONTEXT)eventhintr    r!   V/var/www/html/Ranjet/env/lib/python3.9/site-packages/sentry_sdk/integrations/stdlib.pyadd_python_runtime_context.   s
    z@StdlibIntegration.setup_once.<locals>.add_python_runtime_context)_install_httplib_install_subprocessr   )r#   r!   r!   r"   
setup_once(   s    zStdlibIntegration.setup_onceN)__name__
__module____qualname__
identifierstaticmethodr&   r!   r!   r!   r"   r   %   s   r   c                     s4   t jt j fdd}  fdd}| t _|t _d S )Nc                    s  t j}|td u r. | ||g|R i |S | j}| j}| j}|}	|	dszd|dkr\dp^d|||krpd| prd|f }	|jdd||	f d	}
|
	d
| |
	d|	  | ||g|R i |}|
|
D ]*\}}tdj|||	d | || q|
| _|S )N)zhttp://zhttps://z%s://%s%s%si  httpshttpz:%s z%s %sopdescriptionmethodurlzJ[Tracing] Adding `{key}` header {value} to outgoing request to {real_url}.)keyvaluereal_url)r   r   r   r   hostportdefault_port
startswith
start_spanset_dataiter_trace_propagation_headersr   debugformat	putheader_sentrysdk_span)selfr2   r3   argskwargshubr7   r8   r9   r6   spanrvr4   r5   )real_putrequestr!   r"   
putrequest>   s6    
z$_install_httplib.<locals>.putrequestc                    sx   t | dd }|d u r* | g|R i |S  | g|R i |}|d|j |t|j |d|j |  |S )NrA   status_codereason)getattrr<   statusZset_http_statusintrK   finish)rB   rC   rD   rF   rG   )real_getresponser!   r"   getresponsed   s    z%_install_httplib.<locals>.getresponse)r   rI   rQ   )rI   rQ   r!   )rP   rH   r"   r$   9   s    &r$   c                 C   s   ||v r2|| }|dur ||}|dur|||< nR|t | k rh| | }|durV||}|dur|| |< n|or|d}|dur|||< |S )a?  
    given (*args, **kwargs) of a function call, retrieve (and optionally set a
    default for) an argument by either name or position.

    This is useful for wrapping functions with complex type signatures and
    extracting a few arguments without needing to redefine that function's
    entire type signature.
    N)len)rC   rD   r   positionZsetdefault_callbackrG   r!   r!   r"   _init_argumentx   s     

rT   c                     sX   t jjfdd} | t j_t jjfdd}|t j_t jj  fdd}|t j_d S )Nc              	      sp  t j}|td u r* | g|R i |S t|}t||ddpBg }t||dd}d }t|ttfrt|dk rt	   d
tt|}W d    n1 s0    Y  |d u rt|}d }|jd|d}||D ]<\}	}
|d u rt||d	d
dd }|
|d|	 dd < q|r$|d|  | g|R i |}|d| j |W  d    S 1 sb0    Y  d S )NrC   r   cwd	   d    
subprocessr/   env
   c                 S   s   t | p
tjS )N)r   osenviron)xr!   r!   r"   <lambda>       zH_install_subprocess.<locals>.sentry_patched_popen_init.<locals>.<lambda>SUBPROCESS_-_zsubprocess.cwdsubprocess.pid)r   r   r   r   listrT   r   tuplerR   r   joinmapstrr   r;   r=   upperreplacer<   set_tagpid)rB   akwrE   rC   rU   r1   rZ   rF   kvrG   )old_popen_initr!   r"   sentry_patched_popen_init   s2    	.z6_install_subprocess.<locals>.sentry_patched_popen_initc                    s~   t j}|td u r* | g|R i |S |jdd4}|d| j  | g|R i |W  d    S 1 sp0    Y  d S )Nzsubprocess.waitr0   rd   r   r   r   r   r;   rl   rm   rB   rn   ro   rE   rF   )old_popen_waitr!   r"   sentry_patched_popen_wait   s    z6_install_subprocess.<locals>.sentry_patched_popen_waitc                    s~   t j}|td u r* | g|R i |S |jdd4}|d| j  | g|R i |W  d    S 1 sp0    Y  d S )Nzsubprocess.communicatert   rd   ru   rv   )old_popen_communicater!   r"    sentry_patched_popen_communicate   s    z=_install_subprocess.<locals>.sentry_patched_popen_communicate)rY   Popen__init__waitcommunicate)rs   rx   rz   r!   )ry   rr   rw   r"   r%      s    0r%   c                   C   s   t tjddS )Nra   )prefix)r   r\   r]   r!   r!   r!   r"   "get_subprocess_traceparent_headers   s    r   )N)'r\   rY   sysplatformZsentry_sdk.hubr   Zsentry_sdk.integrationsr   Zsentry_sdk.scoper   Zsentry_sdk.tracing_utilsr   Zsentry_sdk.utilsr   r   r   Zsentry_sdk._typesr	   typingr
   r   r   r   r   r   r   httplibr   ImportErrorhttp.clientpython_implementationversion_infor   r   r   r$   rT   r%   r   r!   r!   r!   r"   <module>   s:   ?
U