o
    v&iM                     @  s0  U d Z ddlm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mZmZmZmZ ddlmZ ddlmZmZmZ dd	lmZmZ dd
lmZm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ e"eZe0e1e2ddZ3e0e1e2ddZ4e5eddZ6ede7Z8ee8 Z9ee9e5e6ddZ:e3rG dd dZ;e;e6dZ6e6dusJ ej<eddddZe= Z>de?d< d:ddZ@d;d"d#ZAd<d'd(ZBn
d:d)dZ@d<d*d(ZBeG d+d, d,ZCd=d0d1ZDG d2d3 d3e*ZEejFd4d5d>d8d9ZGdS )?zBCallback functions and support for sys.monitoring data collection.    )annotationsN)	dataclass)CodeType)AnyCallableNewTypeOptionalcast)env)TBranchTrailsalways_jumpsbranch_trails)short_filenameshort_stack)NoSource	NotPython)isolate_module)PythonParser)
AnyCallableTFileDispositionTLineNoTOffsetTracerTShouldStartContextFnTShouldTraceFn
TTraceDataTTraceFileDataTWarnFnCOVERAGE_SYSMON_LOGCOVERAGE_SYSMON_STATS
monitoringDISABLE_TYPEDISABLEc                   @  s$   e Zd ZdZddd	ZdddZdS )LoggingWrapperz*Wrap a namespace to log all its functions.wrappedr   	namespacestrreturnNonec                 C  s   || _ || _d S N)r$   r%   )selfr$   r%    r+   U/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/coverage/sysmon.py__init__B   s   
zLoggingWrapper.__init__nameCallable[..., Any]c                   s   d fdd}|S )Nargsr   kwargsr'   c                    s2   t j d  |  |  tj | i |S )N.)logr%   getattrr$   )r0   r1   r.   r*   r+   r,   _wrappedG   s   z,LoggingWrapper.__getattr__.<locals>._wrapped)r0   r   r1   r   r'   r   r+   )r*   r.   r6   r+   r5   r,   __getattr__F   s   zLoggingWrapper.__getattr__N)r$   r   r%   r&   r'   r(   )r.   r&   r'   r/   )__name__
__module____qualname____doc__r-   r7   r+   r+   r+   r,   r#   ?   s    
r#   zsys.monitoringT)fullshort_filenames	frame_idszset[int]seen_threadsmsgr&   r'   r(   c                 C  s   t  }ttt j}|| d d}|tvr+t| t	d| d| dt
   dD ]E}t|ddd	4}zt| d
| d|  |dd W n tyb   t| d
| dt|  |dd Y nw W d   n1 smw   Y  q-dS )z1Write a message to our detailed debugging log(s).iw 07dzNew thread  z:
)z/tmp/foo.outazutf-8)encoding:: T)fileflushN)osgetpidr	   int	threadingcurrent_threadidentr?   addr3   r   openprintUnicodeErrorascii)r@   pidtidtslugfilenamefr+   r+   r,   r3   X   s"   
"&r3   argr   c                 C  sJ   t | tr!dt| dd| j d dt| jd| j d S t| S )z)Make a customized repr for logged values.z<code @#xz name=,z file=#>)
isinstancer   idco_namer   co_filenameco_firstlinenorepr)rY   r+   r+   r,   arg_reprp   s   
rd   names
str | Noner   c                    s   d fdd}|S )z%Decorate a function to log its calls.methodr   r'   c                   s   t  d fdd}|S )Nr*   r   r0   r'   c                   s   z<g }t |D ]\}}|d u rq|| dt|  qtt| dd j dd| d  | g|R  }|W S  ty} } z5td|jj d|  	 td
t	
| ztd usbJ ttjjd W   tyx   td Y  w d }~ww )N=rZ   rE   (z, )z!!rF       r   z/oops, shutting off events with disabled tool id)zipappendrd   r3   r_   r8   join	Exception	__class__	tracebackformat_exceptionsys_monitoring
set_eventssysr    COVERAGE_ID
ValueError)r*   r0   
args_reprsr.   rY   retexc)rg   re   r+   r,   r6   ~   s.   (
z0panopticon.<locals>._decorator.<locals>._wrapped)r*   r   r0   r   r'   r   )	functoolswraps)rg   r6   re   )rg   r,   
_decorator}   s   panopticon.<locals>._decoratorN)rg   r   r'   r   r+   re   r   r+   r~   r,   
panopticonz   s   r   c                 C  s   dS )zAWrite a message to our detailed debugging log(s), but not really.Nr+   )r@   r+   r+   r,   r3      s    c                  G  s   ddd}|S )z5Decorate a function to log its calls, but not really.methr   r'   c                 S  s   | S r)   r+   )r   r+   r+   r,   r      s   r   N)r   r   r'   r   r+   r   r+   r+   r,   r      s   
c                   @  s:   e Zd ZU dZded< ded< ded< ded	< d
ed< dS )CodeInfoz/The information we want about each code object.booltracingzTTraceFileData | None	file_datazdict[TOffset, TLineNo] | Nonebyte_to_liner   r   zdict[TOffset, TOffset]r   N)r8   r9   r:   r;   __annotations__r+   r+   r+   r,   r      s   
 r   coder   dict[TOffset, TLineNo]c                 C  s>   i }|   D ]\}}}|durt||dD ]}|||< qq|S )z6Make a dict mapping byte code offsets to line numbers.N   )co_linesrange)r   b2lbstartbendlinenoboffsetr+   r+   r,   bytes_to_lines   s   
r   c                   @  s   e Zd ZdZd0ddZd1d	d
Ze d2ddZe d2ddZe d2ddZ	d3ddZ
d2ddZd4ddZeddd5dd Zeddd!d6d$d%Zedd&d7d)d*Zedd&d7d+d,Zedddd8d.d/Zd!S )9
SysMonitorzHPython implementation of the raw data tracer for PEP669 implementations.tool_idrK   r'   r(   c                 C  s~   |  d| _ |  |  d | _d | _|  |  |  || _i | _g | _tt| _	d| _
t | _d | _tr:td d| _d| _d S )NFzHstarts start_tracing returns line_lines line_arcs branches branch_trailsr   )
trace_arcsshould_start_contextswitch_contextmyid
code_infoscode_objectscollectionsdefaultdictsetfilename_code_ids	sysmon_onrL   LocklockstatsCOLLECT_STATSdictfromkeyssplit	_activity)r*   r   r+   r+   r,   r-      s,   

zSysMonitor.__init__r&   c                 C  s@   t dd | j D }t| j}dt| dd| d| dS )Nc                 s  s    | ]}t |V  qd S r)   len).0vr+   r+   r,   	<genexpr>   s    z&SysMonitor.__repr__.<locals>.<genexpr>z<SysMonitor at rZ   rF   z data points in z files>)sumdatavaluesr   r_   )r*   pointsfilesr+   r+   r,   __repr__   s   
zSysMonitor.__repr__c                 C  s   | j c tdus
J t| jd ttj| j}tjj	}t
| j|j ||j| j | jrP||j| j ||j| j tjjrO||j| j ||j| j n||j| j t  d| _W d   dS 1 siw   Y  dS )zStart this Tracer.Nzcoverage.pyT)r   rt   use_tool_idr   r|   partialregister_callbackrv   r    eventsru   PY_STARTsysmon_py_startr   	PY_RETURNsysmon_py_returnLINEsysmon_line_arcsr
   
PYBEHAVIORbranch_right_leftBRANCH_RIGHTsysmon_branch_eitherBRANCH_LEFTsysmon_line_linesrestart_eventsr   )r*   registerr   r+   r+   r,   start   s0   "zSysMonitor.startc                 C  s  | j * | js	 W d   dS tdusJ t| jd d| _t| j W d   n1 s0w   Y  trt| j	 dd dd}t
dd	 |D }|t| }|rztd
| d| d |D ]\}}t|dkrwtt|dd|  qadS td dS dS )zStop this Tracer.Nr   Fc                 S  s   t | d S )Nrk   r   )itemr+   r+   r,   <lambda>$  s    z!SysMonitor.stop.<locals>.<lambda>T)keyreversec                 s  s    | ]	\}}t |V  qd S r)   r   )r   _code_idsr+   r+   r,   r   '  s    z"SysMonitor.stop.<locals>.<genexpr>z==== Duplicate code objects: z duplicates, z totalrk   z>5z
 objects: z!==== Duplicate code objects: none)r   r   rt   ru   r   free_tool_idLOGsortedr   itemsr   r   r3   )r*   r   	code_objsdupesrW   r   r+   r+   r,   stop  s4   
zSysMonitor.stopc                 C  s   |    dS )z+The process has forked, clean up as needed.N)r   r*   r+   r+   r,   	post_fork1  s   zSysMonitor.post_forkr   c                 C     | j S )zHas there been any activity?r   r   r+   r+   r,   activity6     zSysMonitor.activityc                 C  s
   d| _ dS )zReset the activity() flag.FNr   r   r+   r+   r,   reset_activity:  s   
zSysMonitor.reset_activitydict[str, int] | Nonec                 C  r   )z+Return a dictionary of statistics, or None.)r   r   r+   r+   r,   	get_stats>  r   zSysMonitor.get_statsr   @r   instruction_offsetr   MonitorReturnc                 C  sZ  d| _ | jdur| jd  d7  < | jt|}d}d}|dur'|j}|j}|du r+|j}| j|}|du rVt	
 }|durKt	
 j}trK|j}| ||}|| j|< |j}|r|j}	|	dusdJ |   z|	| jvrtt | j|	< W |   n|   w | j|	 }t|}
nd}d}
t|||
i i d}|| jt|< | j| |r+| jdur| jd  d7  < tjj}| jg | jr
tdusJ |j|jB |j B }| j!rt"j#j$sJ ||j%|j&B O }t'| j(|| tr|jdvr| j)|j d|j*  +t| W d   t,S W d   t,S W d   t,S W d   t,S 1 s&w   Y  t,S )	z-Handle sys.monitoring.events.PY_START events.TNstartsrk   )r   r   r   r   r   start_tracing>   <string>rE   )-r   r   r   getr_   r   r   ra   should_trace_cacheinspectcurrentframef_backr   should_tracetracesource_filename	lock_datar   r   unlock_datar   r   r   rn   rv   r    r   r   r   rt   r   	PY_RESUMEr   r   r
   r   r   r   r   set_local_eventsr   r   r`   rO   r"   )r*   r   r   	code_infotracing_coder   rW   dispframe	tracenamer   r   local_eventsr+   r+   r,   r   B  s   











zSysMonitor.sysmon_py_startNretvalobjectc                 C  s\   | j dur| j d  d7  < | jt|}|j|}|dur,||j f}|j| tS )zBHandle sys.monitoring.events.PY_RETURN events for branch coverage.Nreturnsrk   )	r   r   r   r_   r   rb   r   rO   r"   )r*   r   r   r   r   	last_linearcr+   r+   r,   r     s   
zSysMonitor.sysmon_py_returnlineline_numberr   c                 C  sN   | j dur| j d  d7  < | jt|}|dur%|jdur%|j| tS )z;Handle sys.monitoring.events.LINE events for line coverage.N
line_linesrk   )r   r   r   r_   r   rO   r"   )r*   r   r   r   r+   r+   r,   r     s   
zSysMonitor.sysmon_line_linesc                 C  sB   | j dur| j d  d7  < | jt| }||f}|j| tS )z=Handle sys.monitoring.events.LINE events for branch coverage.N	line_arcsrk   )r   r   r_   r   rO   r"   )r*   r   r   r   r   r+   r+   r,   r     s   
zSysMonitor.sysmon_line_arcsdestination_offsetc                 C  sT  | j dur| j d  d7  < | jt| }|js7| j dur&| j d  d7  < t|j}t||d|_t||_d}|j|}|h}|j| }	dur]|	}|| |j| }	dusL|dur}|	 D ]\}
}|
du rnqe||@ r||j
|
 d} nqe|s|jdusJ |j|}|dur|j|}|dur||kr||f}
|j
|
 tS )z+Handle BRANCH_RIGHT and BRANCH_LEFT events.Nbranchesrk   r   )multiline_mapFT)r   r   r_   r   get_multiline_mapra   r   r   rO   r   r   r   r"   )r*   r   r   r   r   r  	added_arc	dest_infodestsdestr   offsetsl1l2r+   r+   r,   r     sD   




zSysMonitor.sysmon_branch_either)r   rK   r'   r(   )r'   r&   )r'   r(   )r'   r   )r'   r   )r   r   r   r   r'   r   )r   r   r   r   r   r   r'   r   )r   r   r   r   r'   r   )r   r   r   r   r   r   r'   r   )r8   r9   r:   r;   r-   r   r   r   r   r   r   r   r   r   r   r   r   r   r+   r+   r+   r,   r      s.    

&



M
r      )maxsizerW   dict[TLineNo, TLineNo]c                 C  sF   zt | d}|  W |jS  ty   i  Y S  ty"   i  Y S w )z2Get a PythonParser for the given filename, cached.)rW   )r   parse_sourcer   r   r  )rW   parserr+   r+   r,   r    s   


r  )r@   r&   r'   r(   )rY   r   r'   r&   )re   rf   r'   r   )r   r   r'   r   )rW   r&   r'   r  )Hr;   
__future__r   r   r|   r   rI   os.pathrv   rL   rr   dataclassesr   typesr   typingr   r   r   r   r	   coverager
   coverage.bytecoder   r   r   coverage.debugr   r   coverage.exceptionsr   r   coverage.miscr   coverage.parserr   coverage.typesr   r   r   r   r   r   r   r   r   r   r   rK   getenvr   r   r4   rt   r   r!   r   r"   r#   r   r   r?   r   r3   rd   r   r   r   r   	lru_cacher  r+   r+   r+   r,   <module>   sf   0





#
	

  
,