o
    ZhD                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZ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 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 W n' ey   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 Y nw ze W n ey   eZY nw zd dl m!Z! W n ey   dZ!Y nw d dl"m"Z" d dl#m$Z$ z
d dl#m%Z% dZ&W n ey   dZ&Y nw d dl'Z'd dl(m)Z)m*Z*m+Z+m,Z,m-Z-mZ d dl.m/Z/ d dl0m1Z1 e/r%d dl2m3Z3m4Z4 d dl5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ d dl.mAZAmBZBmCZC e"dddZDeEdZFdaGeHdZIdZJdd ZKdd ZLd d! ZMd"d# ZNd$d% ZOG d&d' d'ePZQeQ ZRd(d) ZSd*d+ ZTd,d- ZUd.d/ ZVdd0d1ZWG d2d3 d3eXZYe,G d4d5 d5ePZZG d6d7 d7ePZ[G d8d9 d9ePZ\e/rd d:l5m]Z] e]d;Z^e@e\e^f Z_d<d= Z`d>d? Zad@dA ZbdBdC Zc			ddDdEZd	ddFdGZedHdI Zfe)rdJdK ZgndLdK ZgdMdN Zh				ddOdPZi			ddQdRZjdSdT ZkdUdV Zl					ddWdXZmeneodYZpeprdZd[ Zqnd\d[ Zq			 	 	dd]d^Zr		dd_d`Zsdadb Ztdcdd Zudedf ZvddgdhZwddidjZxdkdl Zy		ddmdnZzdodp Z{dqdr Z|dsdt Z}dudv Z~dwdx ZddydzZd{d| Zd}d~ Zdd Zdd Zdd Ze \ZZZdZdd Zdd ZedZG dd deoZG dd de
jZdd Zdd Zedg dZdddZedg dZdddZdd ZdddZdd Zdd Zdd Zdd Zdd Ze+rdd Zne*rdd Zndd Ze)rdd Zndd Zzd dlmZ W n ey   dd ZY nw dd ZdS )    N)
namedtuple)copy)Decimal)Real)parse_qs)unquote)	urlencode)urlsplit)
urlunsplit)BaseExceptionGroup)datetime)partial)partialmethodTF)PY2PY33PY37implements_str	text_typeurlparse)TYPE_CHECKING)DEFAULT_MAX_VALUE_LENGTH)	FrameTypeTracebackType)AnyCallableContextManagerDictIteratorListOptionalSetTupleTypeUnion)EndpointTypeEventExcInfoi     zsentry_sdk.errorsz^[a-zA-Z0-9/+=]*$z
[Filtered]c                 C   s   t j| ddddS )zCSerialize data into a compact JSON representation encoded as UTF-8.F),:)	allow_nan
separatorsutf-8)jsondumpsencode)data r1   G/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/utils.py
json_dumps]   s   r3   c                   C   s   d S Nr1   r1   r1   r1   r2   _get_debug_hubc      r5   c               
   C   s~   z1t tjjd} tjg dtj| | d d  	d}W d    W |S 1 s*w   Y  W |S  t
ttfy>   Y d S w )Nzw+)gitz	rev-parseHEAD)stdoutstderrstdinr   r,   )openospathdevnull
subprocessPopenPIPEcommunicatestripdecodeOSErrorIOErrorFileNotFoundError)nullrevisionr1   r1   r2   get_git_revisioni   s(   rK   c                  C   sH   t jd} | r
| S t } | r| S dD ]}t j|} | r!|   S qdS )zTry to guess a default release.ZSENTRY_RELEASE)ZHEROKU_SLUG_COMMITZSOURCE_VERSIONZ!CODEBUILD_RESOLVED_SOURCE_VERSIONZCIRCLE_SHA1ZGAE_DEPLOYMENT_IDN)r=   environgetrK   )releasevarr1   r1   r2   get_default_release~   s   rP   c                 C   s,   g d}|D ]}|| v rd |  S qdS )zAReturn the SDK name including the name of the used web framework.)djangoZflaskZfastapiZbottleZfalconZquartZsanicZ	starletteZchaliceZ
serverlessZpyramidtornadoZaiohttpZ
aws_lambdaZgcpZbeamZasgiZwsgizsentry.python.{}zsentry.python)format)Zinstalled_integrationsZframework_integrationsZintegrationr1   r1   r2   get_sdk_name   s   rT   c                   @   s    e Zd ZdZdd Zdd ZdS )CaptureInternalExceptionr1   c                 C   s   | S r4   r1   selfr1   r1   r2   	__enter__      z"CaptureInternalException.__enter__c                 C   s"   |d ur|d urt |||f dS )NT)capture_internal_exception)rW   tyvaluetbr1   r1   r2   __exit__   s   z!CaptureInternalException.__exit__N)__name__
__module____qualname__	__slots__rX   r^   r1   r1   r1   r2   rU      s    rU   c                   C   s   t S r4   )_CAPTURE_INTERNAL_EXCEPTIONr1   r1   r1   r2   capture_internal_exceptions   rY   rd   c                 C   s    t  }|d ur||  d S d S r4   )r5   Z_capture_internal_exception)exc_infohubr1   r1   r2   rZ      s   rZ   c                 C   s   | t   S r4   )epochtotal_secondsr\   r1   r1   r2   to_timestamp      rj   c                 C   s
   |  dS )Nz%Y-%m-%dT%H:%M:%S.%fZ)strftimeri   r1   r1   r2   format_timestamp   s   
rm   c                 C   s2   | du r	t  } nt| } | d du rd} d| iS )z+Creates a hint with the exc info filled in.Nr   re   )sysre   exc_info_from_errorre   r1   r1   r2   event_hint_with_exc_info   s   
rq   c                   @      e Zd ZdZdS )BadDsnzRaised on invalid DSNs.Nr_   r`   ra   __doc__r1   r1   r1   r2   rs      s    rs   c                   @   s6   e Zd ZdZdd Zedd ZdddZd	d
 ZdS )DsnzRepresents a DSN.c              	   C   s  t |trt|j| _d S tt|}|jdvr td|j |j| _|j	d u r-td|j	| _
|jd u rA| jdkr=dp>d| _n|j| _|jsLtd|j| _|j| _|jdd	}ztt| | _W n ttfy|   td
|jpudd	d   w d|d | _d S )N)httphttpszUnsupported scheme %rzMissing hostnamerx     P   zMissing public key/r'   zInvalid project in DSN (%r) )
isinstancerv   dict__dict__r   r	   r   schemers   hostnamehostportusername
public_keypassword
secret_keyr>   rsplitintpop
project_id
ValueError	TypeErrorjoin)rW   r\   partsr>   r1   r1   r2   __init__   s0   



zDsn.__init__c                 C   s(   | j }| j| jfdvrd|| jf }|S )zThe netloc part of a DSN.))rw   rz   )rx   ry   z%s:%s)r   r   r   rW   rvr1   r1   r2   netloc  s   z
Dsn.netlocNc              	   C   s"   t | j| j| j| j| j| j|dS )z*Returns the auth info object for this dsn.)r   r   r>   r   r   r   client)Authr   r   r>   r   r   r   )rW   r   r1   r1   r2   to_auth!  s   zDsn.to_authc                 C   s.   d| j | j| jrd| j pd| j| j| jf S )Nz%s://%s%s@%s%s%s@r|   )r   r   r   r   r>   r   rV   r1   r1   r2   __str__.  s   zDsn.__str__r4   )	r_   r`   ra   ru   r   propertyr   r   r   r1   r1   r1   r2   rv      s    $

rv   c                   @   sB   e Zd ZdZ				dddZedd Z		dd
dZdd ZdS )r   z,Helper object that represents the auth info.N   r{   c	           	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S r4   )r   r   r>   r   r   r   versionr   )	rW   r   r   r   r   r   r   r   r>   r1   r1   r2   r   =  s   
zAuth.__init__c                 C   s   | j ddS )z^Returns the API url for storing events.

        Deprecated: use get_api_url instead.
        store)type)get_api_urlrV   r1   r1   r2   store_api_urlR  s   zAuth.store_api_urlr   c                 C   s   d| j | j| j| j|f S )z'Returns the API url for storing events.z%s://%s%sapi/%s/%s/)r   r   r>   r   )rW   r   r1   r1   r2   r   [  s   zAuth.get_api_urlc                 C   s`   d| j fd| jfg}| jdur|d| jf | jdur$|d| jf dddd	 |D  S )
z!Returns the auth header a string.Z
sentry_keyZsentry_versionNZsentry_clientZsentry_secretzSentry z, c                 s   s     | ]\}}d ||f V  qdS )z%s=%sNr1   ).0keyr\   r1   r1   r2   	<genexpr>p  s    z!Auth.to_header.<locals>.<genexpr>)r   r   r   appendr   r   r   r1   r1   r2   	to_headerh  s   

zAuth.to_header)Nr   Nr{   )r   )	r_   r`   ra   ru   r   r   r   r   r   r1   r1   r1   r2   r   :  s    

	
r   c                   @   sH   e Zd ZdZdZdd Zdd Zedd Zed	d
 Z	edd Z
dS )AnnotatedValuea  
    Meta information for a data field in the event payload.
    This is to tell Relay that we have tampered with the fields value.
    See:
    https://github.com/getsentry/relay/blob/be12cd49a0f06ea932ed9b9f93a655de5d6ad6d1/relay-general/src/types/meta.rs#L407-L423
    r\   metadatac                 C   s   || _ || _d S r4   r   )rW   r\   r   r1   r1   r2   r   }  s   
zAnnotatedValue.__init__c                 C   s&   t |tsdS | j|jko| j|jkS NF)r}   r   r\   r   )rW   otherr1   r1   r2   __eq__  s   
zAnnotatedValue.__eq__c                 C      t ddddggidS )zxThe value was removed because it could not be parsed. This is done for request body values that are not json nor a form.r|   remz!rawxr   r   clsr1   r1   r2   removed_because_raw_data     z'AnnotatedValue.removed_because_raw_datac                 C   r   )zThe actual value was removed because the size of the field exceeded the configured maximum size (specified with the max_request_body_size sdk option)r|   r   !configr   r   r   r   r1   r1   r2   removed_because_over_size_limit  r   z.AnnotatedValue.removed_because_over_size_limitc                 C   s   t tdddggidS )zHThe actual value was removed because it contained sensitive information.r   r   sr   )r   SENSITIVE_DATA_SUBSTITUTEr   r1   r1   r2   +substituted_because_contains_sensitive_data  r   z:AnnotatedValue.substituted_because_contains_sensitive_dataN)r_   r`   ra   ru   rb   r   r   classmethodr   r   r   r1   r1   r1   r2   r   s  s    

r   )TypeVarTc                 C   s   t | dd pt | dd S )Nra   r_   getattrr   r1   r1   r2   get_type_name  s   r   c                 C   s   t | dd }|dvr|S d S )Nr`   )Nbuiltins__builtins__r   )r   modr1   r1   r2   get_type_module  s   r   c              	   C   sl   z| j d }|drW dS W n ttfy   Y nw dD ]}z| j| r)W  dS W q ty3   Y qw dS )Nr_   zsentry_sdk.T)Z__traceback_hide____tracebackhide__F)	f_globals
startswithAttributeErrorKeyErrorf_locals	Exception)framer   Z	flag_namer1   r1   r2   should_hide_frame  s"   


r   c                 c   s4    | }|d urt |js|V  |j}|d usd S d S r4   )r   tb_frametb_next)r]   Ztb_r1   r1   r2   iter_stacks  s   
r   c              	      s6  d}d }|d ur*t |dr*z||}W n ttfy!   d }Y nw |d ur*| }|d u rFzt| }W n ttfyE   g d g f Y S w |sMg d g fS td|| }t	|d | t
|}	z- fdd||| D }
t|| d d} fd	d||d |	 D }|
||fW S  ty   g d g f Y S w )
N   
get_sourcer   r'   c                       g | ]}t |d  dqS 

max_lengthstrip_stringrD   r   liner   r1   r2   
<listcomp>	      z'get_lines_from_file.<locals>.<listcomp>r   r   c                    r   r   r   r   r   r1   r2   r     r   )hasattrr   ImportErrorrG   
splitlines	linecachegetlinesrF   maxminlenr   rD   
IndexError)filenamelinenor   loadermoduleZcontext_linessourceZ
source_strlower_boundupper_boundpre_contextcontext_linepost_contextr1   r   r2   get_lines_from_file  s@   



r   c                 C   s   z| j j}W n ty   d }Y nw z| jd }W n ty'   g d g f Y S w z| jd }W n ty:   d }Y nw |d }|d urN|rNt|||||dS g d g fS )Nr_   
__loader__r'   )r   r   )f_codeco_filenamer   r   r   )r   	tb_linenomax_value_lengthabs_pathr   r   r   r1   r1   r2   get_source_context  s*   

r   c                 C   s&   zt | W S  ty   t|  Y S w r4   )r   r   	safe_reprri   r1   r1   r2   safe_str2  s
   
r   c                 C   sT   zt | dd}z
|ddW W S  ty   | Y W S w  ty)   Y dS w )Nr,   replacelatin1zunicode-escape<broken repr>)reprrE   r/   r   )r\   r   r1   r1   r2   r   <  s   

r   c                 C   s    zt | W S  ty   Y dS w )Nr   )r   r   ri   r1   r1   r2   r   W  s
   
c                 C   s   |r| s|S z=| dr|d d }| ddd }|| kr%tj|W S tj| j}|s0|W S ||tj	dd dd 
tj	W S  tyN   | Y S w )Nz.pyc.r'   r      )endswithsplitr=   r>   basenamern   modules__file__r   seplstripr   )r   r   base_moduleZbase_module_pathr1   r1   r2   filename_for_module_  s"   
r  c           
      C   s   t | dd }|sd }d }n| jj}| jj}z| jd }W n ty'   d }Y nw |d u r/| j}t||p5d |r=tj	
|nd |pAd||d}	|rWt| ||\|	d< |	d< |	d< |r`t| j|	d< |	S )	Nr   r_   z	<unknown>)r   r   functionr   r   r   r   r   vars)r   r   r   co_namer   r   f_linenor  r=   r>   abspathr   r   r   )
r   r   include_local_variablesinclude_source_contextr   r   r   r  r   r   r1   r1   r2   serialize_framew  s4   r  c                 C   sT   d}g }t  }|d ur"t|s|t|| ||d |j}|d us|  d|iS )NTr  r  r   frames)rn   	_getframer   r   r  f_backreverse)r  r  r   r   r  fr1   r1   r2   current_stacktrace  s"   r  c                 C   s   t | dd S )Nerrnor   	exc_valuer1   r1   r2   	get_errno  rk   r  c                 C   s    t | ddpt | ddpt| S )Nmessager|   detail)r   r   r  r1   r1   r2   get_error_message  s
   
r  c                    sn  i }|r|  nddd|d< |dur||d d< |dur"t|}	nd}	|	dur8|d di di d	|	 |durB||d d
< |dk}
|
sX|durX||d d< d|d d< |
rfd|d vrfd|d d< tduont|t}|rwd|d d< t| |d< t| |d< t||d< |du rd dtn|d  |d |d  fddt	|D }|rd|i|d< |S )z
    Creates a dict that goes into the events `exception.values` list and is ingestible by Sentry.

    See the Exception Interface documentation for more details:
    https://develop.sentry.dev/sdk/event-payloads/exception/
    ZgenericT)r   Zhandled	mechanismNexception_idmetar  numberr   r   	parent_idZchainedr   is_exception_groupr   r\   r  r  r   c              	      s"   g | ]}t |j|j d qS ))r   r  r  r   )r  r   r   )r   r]   r  r1   r2   r   
  s    z5single_exception_from_error_tuple.<locals>.<listcomp>r  
stacktrace)
r   r  
setdefaultr   r}   r   r   r  r   r   )exc_typer  r]   client_optionsr  r  r"  r   Zexception_valuer  Zis_root_exceptionr#  r  r1   r  r2   !single_exception_from_error_tuple  sT   


r(  __suppress_context__c                 c   s    | \}}}g }t  }|d urW|d urYt||vr[|||fV  || |t| |jr2|j}n|j}|d u r;d S t|}|}t|dd }|d ur]|d ur_t||vsd S d S d S d S d S d S )N__traceback__)	setidr   addr)  	__cause____context__r   r   )re   r&  r  r]   Zseen_exceptionsZseen_exception_idscauser1   r1   r2   walk_exception_chain  s4   

r1  c                 c   s    | V  d S r4   r1   rp   r1   r1   r2   r1  ?  s   
c                 C   sH  t | |||||||d}|g}	|}|d7 }t|do|j}
|
rI|o*t|do*|jdu}|rH|j}tt||t|dd|||dd\}}|	| n)|oTt|doT|jdu}|rr|j}tt||t|dd|||dd\}}|	| |oxt|d	}|rt	|j
D ]\}}tt||t|dd||||d
| d\}}|	| q||	fS )z
    Creates the list of exceptions.
    This can include chained exceptions and exceptions from an ExceptionGroup.

    See the Exception Interface documentation for more details:
    https://develop.sentry.dev/sdk/event-payloads/exception/
    )r&  r  r]   r'  r  r  r"  r   r'   r)  r.  Nr*  )r&  r  r]   r'  r  r  r   r/  
exceptionszexceptions[%s])r(  r   r)  r.  exceptions_from_errorr   r   extendr/  	enumerater2  )r&  r  r]   r'  r  r  r"  r   parentr2  Zshould_supress_contextZexception_has_causer0  Zchild_exceptionsZexception_has_contentcontextr#  idxer1   r1   r2   r3  D  s   



	


	


r3  c           	   	   C   sv   | \}}}t d uot|t }|rt|||||ddd\}}ng }t| D ]\}}}|t||||| q$|  |S )Nr   )r&  r  r]   r'  r  r  r"  )r   r}   r3  r1  r   r(  r  )	re   r'  r  r&  r  r]   r#  _r2  r1   r1   r2   exceptions_from_error_tuple  s.   


r;  c                 C   s.   zt | W S  ty   t| dd  Y S w )Nr'   r   )r   UnicodeDecodeErrorr   ri   r1   r1   r2   	to_string  s
   
r=  c                 c   s    d| v r
| d V  d| v r#| d  dpdD ]}d|v r"|d V  qd| v r<| d  dp/dD ]}d|v r;|d V  q0d S d S )Nr$  threadsvaluesr1   	exception)rM   )eventthreadr@  r1   r1   r2   iter_event_stacktraces  s   


rC  c                 c   s.    t | D ]}|dpdD ]}|V  qqd S )Nr  r1   )rC  rM   )rA  r$  r   r1   r1   r2   iter_event_frames  s   rD  c                 C   s(   t | D ]}t|d|||d q| S )Nr  )in_app_excludein_app_includeproject_root)rC  set_in_app_in_framesrM   )rA  rE  rF  rG  r$  r1   r1   r2   handle_in_app  s   rI  c                 C   s   | sd S | D ]B}| d}|d urq| d}t||r!d|d< qt||r+d|d< q| d}|d u r5qt|r>d|d< qt||rHd|d< qq| S )NZin_appr   TFr   )rM   _module_in_list_is_external_source_is_in_project_root)r  rE  rF  rG  r   Zcurrent_in_appr   r   r1   r1   r2   rH    s0   





rH  c                 C   s   t | trt| dkr| \}}}n2t | tr;t| dd }|d ur't| }| }nt \}}}|| ur:d }| }t| }ntdt|  |||fS )N   r*  z,Expected Exception object to report, got %s!)	r}   tupler   BaseExceptionr   r   rn   re   r   )errorr&  r  r]   r1   r1   r2   ro     s   

ro   c                 C   s*   t | } t| }ddt| ||id|fS )NrP  r?  )levelr@  )ro   rq   r;  )re   r'  r  hintr1   r1   r2   event_from_exception0  s   rS  c                 C   s>   | d u rdS |s
dS |D ]}|| ks|  |d r dS qdS )NFr   Tr   )nameitemsitemr1   r1   r2   rJ  E  s   rJ  c                 C   s   t d| d u}|S )Nz [\\/](?:dist|site)-packages[\\/]research)r   Zexternal_sourcer1   r1   r2   rK  T  s   rK  c                 C   s   |d u rdS |  |rdS dS )NFTrT  )r   rG  r1   r1   r2   rL  ]  s
   
rL  c                 C   sJ   t | tr| d|d  }|d S | dd|d  jddd}|d S )zd
    Truncate a UTF-8-encodable string to the last full codepoint so that it fits in max_bytes.
    NrM  r,   ignore)errors...)r}   bytesr/   rE   )stringZ	max_bytes	truncatedr1   r1   r2   _truncate_by_bytesi  s   
ra  c              	   C   sN   t | ttfs	d S t | trt| S zt| dW S  ttfy&   Y d S w )Nr,   )r}   r^  r   r   r/   UnicodeEncodeErrorr<  ri   r1   r1   r2   _get_size_in_bytesz  s   
rc  c                 C   s   | s| S |d u r
t }t| }d }t| trt| }|d ur'||kr't| |}n|d ur:||kr:| d |d  d }n| S t||pA|dd|d |ggddS )NrM  r]  z!limitr   )r   r   r   )r   rc  r}   r   r   ra  r   )r\   r   Z	byte_sizeZ	text_sizeZtruncated_valuer1   r1   r2   r     s&   
r   c              
   C   sp   d}t d| d t jt jB }z||  d }ttt|	ddd }W |S  t
ttfy7   Y dS w )z
    Parses a version string into a tuple of integers.
    This uses the parsing loging from PEP 440:
    https://peps.python.org/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions
    a    # noqa: N806
        v?
        (?:
            (?:(?P<epoch>[0-9]+)!)?                           # epoch
            (?P<release>[0-9]+(?:\.[0-9]+)*)                  # release segment
            (?P<pre>                                          # pre-release
                [-_\.]?
                (?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
                [-_\.]?
                (?P<pre_n>[0-9]+)?
            )?
            (?P<post>                                         # post release
                (?:-(?P<post_n1>[0-9]+))
                |
                (?:
                    [-_\.]?
                    (?P<post_l>post|rev|r)
                    [-_\.]?
                    (?P<post_n2>[0-9]+)?
                )
            )?
            (?P<dev>                                          # dev release
                [-_\.]?
                (?P<dev_l>dev)
                [-_\.]?
                (?P<dev_n>[0-9]+)?
            )?
        )
        (?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?       # local version
    z^\s*z\s*$rN   r   NrM  )rY  compileVERBOSE
IGNORECASEmatch	groupdictrN  mapr   r   r   r   r   )r   VERSION_PATTERNpatternrN   Zrelease_tupler1   r1   r2   parse_version  s   

rl  c                  C   s   z7ddl } ddlm} tdd td| jdd D }|dd	r6tjd
kr+|dks0|ddr3W dS W dS W n	 t	y@   Y nw z,ddl
}ddlm} t|j}|du r]td W dS |drh|dk rkW dS W dS W dS  t	yw   Y dS w )z
    Returns whether gevent/eventlet have patched the stdlib in a way where thread locals are now more "correct" than contextvars.
    r   N)is_object_patchedc                 S   s   g | ]}t |qS r1   )r   )r   partr1   r1   r2   r     s    z*_is_contextvars_broken.<locals>.<listcomp>z	a|b|rc|\.r   	threadinglocalrM  r   )   	   contextvars
ContextVarFT)is_monkey_patchedzYInternal error in Sentry SDK: Could not parse Greenlet version from greenlet.__version__.rB  )r   r   )geventgevent.monkeyrm  rN  rY  r   __version__rn   version_infor   greenletZeventlet.patcherrv  rl  loggerrP  )rw  rm  version_tupler{  rv  Zgreenlet_versionr1   r1   r2   _is_contextvars_broken  sF   
	
r~  c                    s   G  fdddt }|S )Nc                       s4   e Zd Zd
 fdd	Zd
ddZdd Zdd	 ZdS )z1_make_threadlocal_contextvars.<locals>.ContextVarNc                    s    || _ || _  | _  | _d S r4   )_name_default_local_original_local)rW   rU  defaultrp  r1   r2   r     s   z:_make_threadlocal_contextvars.<locals>.ContextVar.__init__c                 S   s   t | jd|p| jS )Nr\   )r   r  r  )rW   r  r1   r1   r2   rM     s   z5_make_threadlocal_contextvars.<locals>.ContextVar.getc                 S   s0   t td}|  }t| j|| || j_|S )N@   )strrandomgetrandbitsrM   setattrr  r  r\   )rW   r\   tokenoriginal_valuer1   r1   r2   r+  !  s
   z5_make_threadlocal_contextvars.<locals>.ContextVar.setc                 S   s   t | j|| j_| j|= d S r4   )r   r  r  r\   )rW   r  r1   r1   r2   reset)  s   z7_make_threadlocal_contextvars.<locals>.ContextVar.resetr4   )r_   r`   ra   r   rM   r+  r  r1   r  r1   r2   ru    s
    
ru  )object)rp  ru  r1   r  r2   _make_threadlocal_contextvars  s   r  c                     s   G dd d  fdd} | S )Nc                   @   s   e Zd Zdd ZdS )z,_make_noop_copy_context.<locals>.NoOpContextc                 _   s   ||i |S r4   r1   )rW   funcargskwargsr1   r1   r2   run4     z0_make_noop_copy_context.<locals>.NoOpContext.runN)r_   r`   ra   r  r1   r1   r1   r2   NoOpContext3  s    r  c                      s     S r4   r1   r1   r  r1   r2   copy_context8  s   z-_make_noop_copy_context.<locals>.copy_contextr1   )r  r1   r  r2   _make_noop_copy_context1  s   r  c                  C   s   t  s8tjdk r zddlm} m} d| |fW S  ty   Y nw zddlm} m} d| |fW S  ty7   Y nw ddlm	} dt
|t fS )z
    Figure out the "right" contextvars installation to use. Returns a
    `contextvars.ContextVar`-like class with a limited API.

    See https://docs.sentry.io/platforms/python/contextvars/ for more information.
    rq  r   )ru  r  Tr  F)r~  rn   rz  Zaiocontextvarsru  r  r   rt  ro  rp  r  r  )ru  r  rp  r1   r1   r2   _get_contextvars?  s    
r  z

With asyncio/ASGI applications, the Sentry SDK requires a functional
installation of `contextvars` to avoid leaking scope/context data across
requests.

Please refer to https://docs.sentry.io/platforms/python/contextvars/ for more information.
c                 C   s   d}zd| j j| j j| jf W S  ty   Y nw d\}}tr3t| dr3t| jtr3d\}}| jj	} nt| t
rEt| j	drEd\}}| j	} t| drN| j}nt| drV| j}|durlt| d	rf| jd
 | }|| | }|S )zbReturn the qualified name of func. Works with regular function, lambda, partial and partialmethod.Nz%s.%s.%s)r|   r|   _partialmethod)zpartialmethod(<function >)r_   )zpartial(<function r  ra   r`   r   )Zim_classr`   r_   r   _PARTIALMETHOD_AVAILABLEr   r}   r  r   r  r   ra   )r  Zfunc_qualnameprefixsuffixr1   r1   r2   qualname_from_functionq  s>   




r  c                 C   s   t | S r4   )r  )r  r1   r1   r2   transaction_from_function     r  disable_capture_eventc                   @   rr   )ServerlessTimeoutWarningz>Raised when a serverless method is about to reach its timeout.Nrt   r1   r1   r1   r2   r    s    r  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TimeoutThreadzCreates a Thread which runs (sleeps) for a time duration equal to
    waiting_time and raises a custom ServerlessTimeout exception.
    c                 C   s&   t j|  || _|| _t  | _d S r4   )ro  Threadr   waiting_timeconfigured_timeoutr%   _stop_event)rW   r  r  r1   r1   r2   r     s   zTimeoutThread.__init__c                 C   s   | j   d S r4   )r  r+  rV   r1   r1   r2   stop  r  zTimeoutThread.stopc                 C   sF   | j | j | j  rd S t| j}|| jk r|d }td|)Nr'   zZWARNING : Function is expected to get timed out. Configured timeout duration = {} seconds.)r  waitr  is_setr   r  r  rS   )rW   Zinteger_configured_timeoutr1   r1   r2   r    s   


zTimeoutThread.runN)r_   r`   ra   ru   r   r  r  r1   r1   r1   r2   r    s
    r  c              
   C   sd   d}z|  d}t|}|d}W |S  ty1 } ztdj| d| W Y d}~|S d}~ww )zO
    Convert a string to base64, via UTF-8. Returns None on invalid input.
    NUTF-8z"Unable to encode {orig} to base64:)orig)r/   base64	b64encoderE   r   r|  warningrS   )originalbase64_string
utf8_bytesbase64_byteserrr1   r1   r2   	to_base64  s   

r  c              
   C   sv   d}zt | }|sJ | d}t|}|d}W |S  ty: } ztdj	| d| W Y d}~|S d}~ww )zQ
    Convert a string from base64, via UTF-8. Returns None on invalid input.
    Nr  z#Unable to decode {b64} from base64:)Zb64)
BASE64_ALPHABETrg  r/   r  	b64decoderE   r   r|  r  rS   )r  Zutf8_stringZonly_valid_charsr  r  r  r1   r1   r2   from_base64  s   


r  
Components)r   r   r>   queryfragmentc           
      C   s   t | }t|jdd}|r'|jd}t|dkr#dtt|d f }n|j}n|j}|r8ttdd |D }n|j}t	|j
|||j|jd	}	|rK|	S t|	S )
zL
    Removes the authority and query parameter values from a given URL.
    T)keep_blank_valuesr   r'   z%s:%s@%sr   c                 S   s   i | ]}|t qS r1   )r   )r   r   r1   r1   r2   
<dictcomp>  s    z sanitize_url.<locals>.<dictcomp>r   r   r  r>   r  )r	   r   r  r   r   r   r   r   r   r  r   r>   r  r
   )
urlremove_authorityremove_query_valuesr   
parsed_urlZquery_paramsZnetloc_partsr   Zquery_string
componentsr1   r1   r2   sanitize_url  s6   r  	ParsedUrlr  r  r  c                 C   s>   t | d|dd}tt|j|jd|jdd}t||j|jdS )z
    Splits a URL into a url (including path), query and fragment. If sanitize is True, the query
    parameters will be sanitized to remove sensitive data. The autority (username and password)
    in the URL will always be removed.
    T)r  r  r   r|   r  r  )	r  r
   r  r   r   r>   r  r  r  )r  sanitizer  base_urlr1   r1   r2   	parse_url(  s"   
r  c                 C   sj   t | ttfrt| rtdj|| t| d dS t	| } | dk s'| dkr3tdj|| d dS dS )	z
    Checks the given sample rate to make sure it is valid type and value (a
    boolean or a number between 0 and 1, inclusive).
    z|{source} Given sample rate is invalid. Sample rate must be a boolean or a number between 0 and 1. Got {rate} of type {type}.)r   rater   Fr   r'   zW{source} Given sample rate is invalid. Sample rate must be between 0 and 1. Got {rate}.)r   r  T)
r}   r   r   mathisnanr|  r  rS   r   float)r  r   r1   r1   r2   is_valid_sample_rateD  s    

r  c                 C   sH   |d u rdS |D ]}|s|d dkr|d7 }t || }|r! dS qdS )NFr   $TrX  )rW  Z
regex_listZsubstring_matchingZitem_matcherZmatchedr1   r1   r2   match_regex_listc  s   r  c                 C   s4   | j duo| j jduo| j jjduo| j jjj|v S )zB
    Determines whether the given URL matches the Sentry DSN.
    N)r   	transportZ
parsed_dsnr   )rf   r  r1   r1   r2   is_sentry_urls  s   

r  c                  c   s    z4ddl m}  t }|  D ]#}|jd }|d ur2t|}|jd ur2||vr2||jfV  || qW d S  ty`   zdd l}W n tyM   Y Y d S w |j	D ]}t|j
|jfV  qQY d S w )Nr   )r   Name)	importlibr   r+  distributions_normalize_module_namer   r-  r   pkg_resourcesworking_setr   )r   yieldeddistrU  normalized_namer  infor1   r1   r2   _generate_installed_modules  s.   


r  c                 C   s   |   S r4   )lower)rU  r1   r1   r2   r    r  r  c                   C   s   t d u r	tt a t S r4   )_installed_modulesr~   r  r1   r1   r1   r2   _get_installed_modules  s   
r  c                 C   s$   t  }|| }|d u rd S t|S r4   )r  rM   rl  )packageZinstalled_packagesr   r1   r1   r2   package_version  s
   
r  c                   C      t  S r4   )timeperf_counter_nsr1   r1   r1   r2   nanosecond_time  r  r  c                   C   s   t t d S Ng    eA)r   r  perf_counterr1   r1   r1   r2   r       c                   C   s   t t d S r  )r   r  r1   r1   r1   r2   r    r  c                   C   s   t   S r4   )r  r1   r1   r1   r2   now  r  r  c                   C   r  r4   )r  r  r1   r1   r1   r2   r    r  is_module_patchedc                  O   s   dS r   r1   )r  r  r1   r1   r2   r    r6   r  c                   C   s   t dpt dS )Nro  _threadr  r1   r1   r1   r2   	is_gevent  r  r  r4   )NNN)NTTN)TTN)NNNNN)NNr   r   N)NN)TTF)Tr   )r  r-   r   loggingr  r=   r  rY  r@   rn   ro  r  collectionsr   r   decimalr   numbersr   urllib.parser   r   r   r	   r
   r   Zcgiurllibr   rH   	NameErrorrG   r   r   r   	functoolsr   r   r  Z
sentry_sdkZsentry_sdk._compatr   r   r   r   r   Zsentry_sdk._typesr   Zsentry_sdk.constsr   typesr   r   typingr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   rg   	getLoggerr|  r  rd  r  r   r3   r5   rK   rP   rT   r  rU   rc   rd   rZ   rj   rm   rq   r   rs   rv   r   r   r   r   	Annotatedr   r   r   r   r   r   r   r   r  r  r  r  r  r(  r   r   ZHAS_CHAINED_EXCEPTIONSr1  r3  r;  r=  rC  rD  rI  rH  ro   rS  rJ  rK  rL  ra  rc  r   rl  r~  r  r  r  ZHAS_REAL_CONTEXTVARSru  r  ZCONTEXTVARS_ERROR_MESSAGEr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rx  r  r  r1   r1   r1   r2   <module>   sh    4

"
I9G
1


+


Q
 	
b
%

'
	
44 &
*&
,





