o
    h                     @   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.mAZAmBZB e"dddZCeDdZEdaFeGdZHdZIdd ZJdd ZKd d! ZLd"d# ZMd$d% ZNG d&d' d'eOZPeP ZQd(d) ZRd*d+ ZSd,d- ZTd.d/ ZUdd0d1ZVG d2d3 d3eWZXe,G d4d5 d5eOZYG d6d7 d7eOZZG d8d9 d9eOZ[e/rd d:l5m\Z\ e\d;Z]e@e[e]f Z^d<d= Z_d>d? Z`d@dA ZadBdC Zb			ddDdEZc	ddFdGZddHdI Zee)rdJdK ZfndLdK ZfdMdN Zg				ddOdPZh			ddQdRZidSdT ZjdUdV Zk					ddWdXZlemendYZoeordZd[ Zpnd\d[ Zp			 	 	dd]d^Zq		dd_d`Zrdadb Zsdcdd Ztdedf ZuddgdhZvddidjZwdkdl Zx		ddmdnZydodp Zzdqdr Z{dsdt Z|dudv Z}dwdx Z~ddydzZd{d| Zd}d~ Zdd Zdd Zdd Ze \ZZZdZdd Zdd ZedZG dd den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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 r0   S/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/sentry_sdk/utils.py
json_dumps]   s   r2   c                   C   s   d S Nr0   r0   r0   r0   r1   _get_debug_hubc      r4   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revisionr0   r0   r1   get_git_revisioni   s(   rJ   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.SENTRY_RELEASE)HEROKU_SLUG_COMMITSOURCE_VERSION!CODEBUILD_RESOLVED_SOURCE_VERSIONCIRCLE_SHA1GAE_DEPLOYMENT_IDN)r<   environgetrJ   )releasevarr0   r0   r1   get_default_release~   s   rU   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.)djangoflaskfastapibottlefalconquartsanic	starlettechalice
serverlesspyramidtornadoaiohttp
aws_lambdagcpbeamasgiwsgizsentry.python.{}zsentry.python)format)installed_integrationsframework_integrationsintegrationr0   r0   r1   get_sdk_name   s   rl   c                   @   s    e Zd ZdZdd Zdd ZdS )CaptureInternalExceptionr0   c                 C   s   | S r3   r0   selfr0   r0   r1   	__enter__      z"CaptureInternalException.__enter__c                 C   s"   |d ur|d urt |||f dS )NT)capture_internal_exception)ro   tyvaluetbr0   r0   r1   __exit__   s   z!CaptureInternalException.__exit__N)__name__
__module____qualname__	__slots__rp   rv   r0   r0   r0   r1   rm      s    rm   c                   C   s   t S r3   )_CAPTURE_INTERNAL_EXCEPTIONr0   r0   r0   r1   capture_internal_exceptions   rq   r|   c                 C   s    t  }|d ur||  d S d S r3   )r4   _capture_internal_exception)exc_infohubr0   r0   r1   rr      s   rr   c                 C   s   | t   S r3   )epochtotal_secondsrt   r0   r0   r1   to_timestamp      r   c                 C   s
   |  dS )Nz%Y-%m-%dT%H:%M:%S.%fZ)strftimer   r0   r0   r1   format_timestamp   s   
r   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   r~   )sysr~   exc_info_from_errorr~   r0   r0   r1   event_hint_with_exc_info   s   
r   c                   @      e Zd ZdZdS )BadDsnzRaised on invalid DSNs.Nrw   rx   ry   __doc__r0   r0   r0   r1   r      s    r   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 hostnamer     P   zMissing public key/r&   zInvalid project in DSN (%r) )
isinstancer   dict__dict__r   r	   r   schemer   hostnamehostportusername
public_keypassword
secret_keyr=   rsplitintpop
project_id
ValueError	TypeErrorjoin)ro   rt   partsr=   r0   r0   r1   __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.))r   r   )r   r   z%s:%s)r   r   r   ro   rvr0   r0   r1   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   )ro   r   r0   r0   r1   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   rn   r0   r0   r1   __str__.  s   zDsn.__str__r3   )	rw   rx   ry   r   r   propertyr   r   r   r0   r0   r0   r1   r      s    $

r   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 r3   )r   r   r=   r   r   r   versionr   )	ro   r   r   r   r   r   r   r   r=   r0   r0   r1   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_urlrn   r0   r0   r1   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   )ro   r   r0   r0   r1   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.
sentry_keysentry_versionNsentry_clientsentry_secretzSentry z, c                 s   s     | ]\}}d ||f V  qdS )z%s=%sNr0   ).0keyrt   r0   r0   r1   	<genexpr>p  s    z!Auth.to_header.<locals>.<genexpr>)r   r   r   appendr   r   r   r0   r0   r1   	to_headerh  s   

zAuth.to_header)Nr   Nr   )r   )	rw   rx   ry   r   r   r   r   r   r   r0   r0   r0   r1   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
    rt   metadatac                 C   s   || _ || _d S r3   r   )ro   rt   r   r0   r0   r1   r   }  s   
zAnnotatedValue.__init__c                 C   s&   t |tsdS | j|jko| j|jkS NF)r   r   rt   r   )ro   otherr0   r0   r1   __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   clsr0   r0   r1   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   r0   r0   r1   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   r0   r0   r1   +substituted_because_contains_sensitive_data  r   z:AnnotatedValue.substituted_because_contains_sensitive_dataN)rw   rx   ry   r   rz   r   r   classmethodr   r   r   r0   r0   r0   r1   r   s  s    

r   )TypeVarTc                 C   s   t | dd pt | dd S )Nry   rw   getattrr   r0   r0   r1   get_type_name  s   r   c                 C   s   t | dd }|dvr|S d S )Nrx   )Nbuiltins__builtins__r   )r   modr0   r0   r1   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 )Nrw   zsentry_sdk.T)__traceback_hide____tracebackhide__F)	f_globals
startswithAttributeErrorKeyErrorf_locals	Exception)framer   	flag_namer0   r0   r1   should_hide_frame  s"   


r   c                 c   s4    | }|d urt |js|V  |j}|d usd S d S r3   )r   tb_frametb_next)ru   tb_r0   r0   r1   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_stringrC   r   liner   r0   r1   
<listcomp>	      z'get_lines_from_file.<locals>.<listcomp>r   r   c                    r   r   r   r   r   r0   r1   r     r   )hasattrr   ImportErrorrF   
splitlines	linecachegetlinesrE   maxminlenr   rC   
IndexError)filenamelinenor   loadermodulecontext_linessource
source_strlower_boundupper_boundpre_contextcontext_linepost_contextr0   r   r1   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 )Nrw   
__loader__r&   )r  r  )f_codeco_filenamer   r   r  )r   	tb_linenomax_value_lengthabs_pathr  r  r  r0   r0   r1   get_source_context  s*   

r  c                 C   s&   zt | W S  ty   t|  Y S w r3   )r   r   	safe_reprr   r0   r0   r1   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>)reprrD   r.   r   )rt   r   r0   r0   r1   r  <  s   

r  c                 C   s    zt | W S  ty   Y dS w )Nr  )r  r   r   r0   r0   r1   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=   basenamer   modules__file__r   seplstripr   )r  r  base_modulebase_module_pathr0   r0   r1   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  rw   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   r0   r0   r1   serialize_framew  s4   r2  c                 C   sT   d}g }t  }|d ur"t|s|t|| ||d |j}|d us|  d|iS )NTr0  r1  r  frames)r   	_getframer   r   r2  f_backreverse)r0  r1  r  r   r4  fr0   r0   r1   current_stacktrace  s"   r9  c                 C   s   t | dd S )Nerrnor   	exc_valuer0   r0   r1   	get_errno  r   r=  c                 C   s    t | ddpt | ddpt| S )Nmessager   detail)r   r  r;  r0   r0   r1   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/
    genericT)r   handled	mechanismNexception_idmetar:  numberr	  r   	parent_idchainedr   is_exception_groupr  rt   r0  r1  r  c              	      s"   g | ]}t |j|j d qS ))r  r0  r1  r  )r2  r   r  )r   ru   r3  r0   r1   r   
  s    z5single_exception_from_error_tuple.<locals>.<listcomp>r4  
stacktrace)
r   r=  
setdefaultr   r   r   r   r@  r   r   )exc_typer<  ru   client_optionsrC  rD  rG  r	  exception_valuer:  is_root_exceptionrI  r4  r0   r3  r1   !single_exception_from_error_tuple  sT   


rP  __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   addrQ  	__cause____context__r   r   )r~   rL  r<  ru   seen_exceptionsseen_exception_idscauser0   r0   r1   walk_exception_chain  s4   

r[  c                 c   s    | V  d S r3   r0   r   r0   r0   r1   r[  ?  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/
    )rL  r<  ru   rM  rC  rD  rG  r	  r&   rQ  rV  NrR  )rL  r<  ru   rM  rC  rD  r	  rW  
exceptionszexceptions[%s])rP  r   rQ  rV  exceptions_from_errorr   r   extendrW  	enumerater\  )rL  r<  ru   rM  rC  rD  rG  r	  parentr\  should_supress_contextexception_has_causerZ  child_exceptionsexception_has_contentcontextrI  idxer0   r0   r1   r]  D  s   



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

	


r]  c           	   	   C   sv   | \}}}t d uot|t }|rt|||||ddd\}}ng }t| D ]\}}}|t||||| q$|  |S )Nr   )rL  r<  ru   rM  rC  rD  rG  )r   r   r]  r[  r   rP  r7  )	r~   rM  rC  rL  r<  ru   rI  _r\  r0   r0   r1   exceptions_from_error_tuple  s.   


ri  c                 C   s.   zt | W S  ty   t| dd  Y S w )Nr&   r  )r   UnicodeDecodeErrorr  r   r0   r0   r1   	to_string  s
   
rk  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 )NrJ  threadsvaluesr0   	exception)rR   )eventthreadrn  r0   r0   r1   iter_event_stacktraces  s   


rq  c                 c   s.    t | D ]}|dpdD ]}|V  qqd S )Nr4  r0   )rq  rR   )ro  rJ  r   r0   r0   r1   iter_event_frames  s   rr  c                 C   s(   t | D ]}t|d|||d q| S )Nr4  )in_app_excludein_app_includeproject_root)rq  set_in_app_in_framesrR   )ro  rs  rt  ru  rJ  r0   r0   r1   handle_in_app  s   rw  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 )Nin_appr  TFr  )rR   _module_in_list_is_external_source_is_in_project_root)r4  rs  rt  ru  r   current_in_appr  r  r0   r0   r1   rv    s0   





rv  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   rR  z,Expected Exception object to report, got %s!)	r   tupler  BaseExceptionr   r   r   r~   r   )errorrL  r<  ru   r0   r0   r1   r     s   

r   c                 C   s*   t | } t| }ddt| ||id|fS )Nr  rm  )levelrn  )r   r   ri  )r~   rM  rC  hintr0   r0   r1   event_from_exception0  s   r  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itemr0   r0   r1   ry  E  s   ry  c                 C   s   t d| d u}|S )Nz [\\/](?:dist|site)-packages[\\/]research)r  external_sourcer0   r0   r1   rz  T  s   rz  c                 C   s   |d u rdS |  |rdS dS )NFTr  )r  ru  r0   r0   r1   r{  ]  s
   
r{  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.
    Nr}  r+   ignore)errors...)r   bytesr.   rD   )string	max_bytes	truncatedr0   r0   r1   _truncate_by_bytesi  s   
r  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.   UnicodeEncodeErrorrj  r   r0   r0   r1   _get_size_in_bytesz  s   
r  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 )Nr}  r  z!limitr   )r  r   r   )r   r  r   r   r  r  r   )rt   r   	byte_size	text_sizetruncated_valuer0   r0   r1   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*$rS   r  Nr}  )r  compileVERBOSE
IGNORECASEmatch	groupdictr~  mapr   r"  r   r   r   )r   VERSION_PATTERNpatternrS   release_tupler0   r0   r1   parse_version  s   

r  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 r0   )r   )r   partr0   r0   r1   r     s    z*_is_contextvars_broken.<locals>.<listcomp>z	a|b|rc|\.r   	threadinglocalr}  r   )   	   contextvars
ContextVarFT)is_monkey_patchedzYInternal error in Sentry SDK: Could not parse Greenlet version from greenlet.__version__.rp  )r   r   )geventgevent.monkeyr  r~  r  r"  __version__r   version_infor   greenleteventlet.patcherr  r  loggerr  )r  r  version_tupler  r  greenlet_versionr0   r0   r1   _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 r3   )_name_default_local_original_local)ro   r  defaultr  r0   r1   r     s   z:_make_threadlocal_contextvars.<locals>.ContextVar.__init__c                 S   s   t | jd|p| jS )Nrt   )r   r  r  )ro   r  r0   r0   r1   rR     s   z5_make_threadlocal_contextvars.<locals>.ContextVar.getc                 S   s0   t td}|  }t| j|| || j_|S )N@   )strrandomgetrandbitsrR   setattrr  r  rt   )ro   rt   tokenoriginal_valuer0   r0   r1   rS  !  s
   z5_make_threadlocal_contextvars.<locals>.ContextVar.setc                 S   s   t | j|| j_| j|= d S r3   )r   r  r  rt   )ro   r  r0   r0   r1   reset)  s   z7_make_threadlocal_contextvars.<locals>.ContextVar.resetr3   )rw   rx   ry   r   rR   rS  r  r0   r  r0   r1   r    s
    
r  )object)r  r  r0   r  r1   _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 r3   r0   )ro   funcargskwargsr0   r0   r1   run4     z0_make_noop_copy_context.<locals>.NoOpContext.runN)rw   rx   ry   r  r0   r0   r0   r1   NoOpContext3  s    r  c                      s     S r3   r0   r0   r  r0   r1   copy_context8  s   z-_make_noop_copy_context.<locals>.copy_contextr0   )r  r0   r  r1   _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.
    r  r   )r  r  Tr  F)r  r   r  aiocontextvarsr  r  r   r  r  r  r  r  )r  r  r  r0   r0   r1   _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 >)rw   )zpartial(<function r  ry   rx   r  )im_classrx   rw   r   _PARTIALMETHOD_AVAILABLEr   r   r  r   r  r   ry   )r  func_qualnameprefixsuffixr0   r0   r1   qualname_from_functionq  s>   




r  c                 C   s   t | S r3   )r  )r  r0   r0   r1   transaction_from_function     r  disable_capture_eventc                   @   r   )ServerlessTimeoutWarningz>Raised when a serverless method is about to reach its timeout.Nr   r0   r0   r0   r1   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 r3   )r  Threadr   waiting_timeconfigured_timeoutEvent_stop_event)ro   r  r  r0   r0   r1   r     s   zTimeoutThread.__init__c                 C   s   | j   d S r3   )r  rS  rn   r0   r0   r1   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  rh   )ro   integer_configured_timeoutr0   r0   r1   r    s   


zTimeoutThread.runN)rw   rx   ry   r   r   r  r  r0   r0   r0   r1   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	b64encoderD   r   r  warningrh   )originalbase64_string
utf8_bytesbase64_byteserrr0   r0   r1   	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:)b64)
BASE64_ALPHABETr  r.   r  	b64decoderD   r   r  r  rh   )r  utf8_stringonly_valid_charsr  r  r  r0   r0   r1   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 r0   )r   )r   r   r0   r0   r1   
<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_urlquery_paramsnetloc_partsr   query_string
componentsr0   r0   r1   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_urlr0   r0   r1   	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  rh   r   float)r  r	  r0   r0   r1   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  $Tr  )r  
regex_listsubstring_matchingitem_matchermatchedr0   r0   r1   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   	transport
parsed_dsnr   )r   r  r0   r0   r1   is_sentry_urls  s   

r  c                  c   s    z)ddl m}  |  D ]}|jd }|d ur'| |}|d ur't||fV  qW d S  tyU   zdd l}W n tyB   Y Y d S w |jD ]}t|j|jfV  qFY d S w )Nr   )r   Name)		importlibr   distributionsr   _normalize_module_namer   pkg_resourcesworking_setr   )r   distr  r   r  infor0   r0   r1   _generate_installed_modules  s*   


r#  c                 C   s   |   S r3   )lower)r  r0   r0   r1   r    r  r  c                   C   s   t d u r	tt a t S r3   )_installed_modulesr   r#  r0   r0   r0   r1   _get_installed_modules  s   
r&  c                 C   s$   t  }|| }|d u rd S t|S r3   )r&  rR   r  )packageinstalled_packagesr   r0   r0   r1   package_version  s
   
r)  c                   C      t  S r3   )timeperf_counter_nsr0   r0   r0   r1   nanosecond_time  r  r-  c                   C   s   t t d S Ng    eA)r   r+  perf_counterr0   r0   r0   r1   r-       c                   C   s   t t d S r.  )r   r+  r0   r0   r0   r1   r-    r0  c                   C   s   t   S r3   )r+  r0   r0   r0   r1   now  r  r1  c                   C   r*  r3   )r+  r/  r0   r0   r0   r1   r1    r  is_module_patchedc                  O   s   dS r   r0   )r  r  r0   r0   r1   r3    r5   r3  c                   C   s   t dpt dS )Nr  _threadr2  r0   r0   r0   r1   	is_gevent  r0  r5  r3   )NNN)NTTN)TTN)NNNNN)NNr   r   N)NN)TTF)Tr   )r  r,   r   loggingr  r<   r  r  r?   r   r  r+  collectionsr   r   decimalr   numbersr   urllib.parser   r   r   r	   r
   r   cgiurllibr   rG   	NameErrorrF   r   r   r   	functoolsr   r   r  
sentry_sdksentry_sdk._compatr   r   r   r   r   sentry_sdk._typesr   sentry_sdk.constsr   typesr   r   typingr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r   	getLoggerr  r%  r  r  r   r2   r4   rJ   rU   rl   r  rm   r{   r|   rr   r   r   r   r   r   r   r   r   r   r   	Annotatedr   r   r   r   r  r  r  r  r*  r2  r9  r=  r@  rP  r   r   HAS_CHAINED_EXCEPTIONSr[  r]  ri  rk  rq  rr  rw  rv  r   r  ry  rz  r{  r  r  r   r  r  r  r  r  HAS_REAL_CONTEXTVARSr  r  CONTEXTVARS_ERROR_MESSAGEr  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r#  r  r&  r)  r-  r1  r  r3  r5  r0   r0   r0   r1   <module>   sh    4

"
I9G
1


+


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

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