o
    f                     @   s   d Z g dZddlZddlZddlZdddZdddZejj e_ ejj e_ G d	d
 d
ejZ	dd Z
dd Zedkr@e  dS dS )zUPython interface for the 'lsprof' profiler.
   Compatible with the 'profile' module.
)runrunctxProfile    Nc                 C   s   t t| ||S N)
_pyprofile_Utilsr   r   )	statementfilenamesort r   /usr/lib/python3.10/cProfile.pyr      s   r   c                 C   s   t t| ||||S r   )r   r   r   r   )r	   globalslocalsr
   r   r   r   r   r      s   r   c                   @   sZ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )r   a`  Profile(timer=None, timeunit=None, subcalls=True, builtins=True)

    Builds a profiler object using the specified timer function.
    The default timer is a fast built-in one based on real time.
    For custom timer functions returning integers, timeunit can
    be a float specifying a scale (i.e. how long each integer unit
    is, in seconds).
    r   c                 C   s$   dd l }||  |  d S Nr   )pstatsStats
strip_dirs
sort_statsprint_stats)selfr   r   r   r   r   r   )   s   zProfile.print_statsc                 C   sN   dd l }t|d}|   || j| W d    d S 1 s w   Y  d S )Nr   wb)marshalopencreate_statsdumpstats)r   filer   fr   r   r   
dump_stats-   s
   "zProfile.dump_statsc                 C   s   |    |   d S r   )disablesnapshot_statsr   r   r   r   r   3   s   zProfile.create_statsc              
   C   s$  |   }i | _i }|D ](}t|j}|j}||j }|j}|j}i }	|	|t|j< |||||	f| j|< q|D ]Y}|j	rt|j}|j	D ]K}
z	|t|
j }	W n	 t
yW   Y qCw |
j}||
j }|
j}|
j}||	v r|	| }||d 7 }||d 7 }||d 7 }||d 7 }||||f|	|< qCq6d S )Nr            )getstatsr   labelcode	callcountreccallcount
inlinetime	totaltimeidcallsKeyError)r   entriescallersdictsentryfuncncccttctcallerssubentryprevr   r   r   r!   7   sD   




zProfile.snapshot_statsc                 C   s   dd l }|j}| |||S r   )__main____dict__r   )r   cmdr;   dictr   r   r   r   ]   s   zProfile.runc                 C   s.   |    zt||| W |   | S |   w r   )enableexecr    )r   r=   r   r   r   r   r   r   b   s   
zProfile.runctxc                O   s,   |    z||i |W |   S |   w r   )r?   r    )r   r3   argskwr   r   r   runcallk   s   zProfile.runcallc                 C   s   |    | S r   )r?   r"   r   r   r   	__enter__r   s   zProfile.__enter__c                 G   s   |    d S r   )r    )r   exc_infor   r   r   __exit__v   s   zProfile.__exit__N)r   )__name__
__module____qualname____doc__r   r   r   r!   r   r   rC   rD   rF   r   r   r   r   r      s    
&	r   c                 C   s$   t | tr
dd| fS | j| j| jfS )N~r   )
isinstancestrco_filenameco_firstlinenoco_name)r(   r   r   r   r'   {   s   

r'   c               
   C   s  dd l } dd l}dd l}dd l}ddlm} d}||d}d|_|jdddd	d d
 |jdddddt|j	j
d |jdddddd |jdd  sS|  |d | \}}||jd d < |jd urm| j|j|_t|dkr|jrd}	|j|d d}
n3|d }|jd| j| t|}t| |d}	W d    n1 sw   Y  |dd d d}
zt|	|
d |j|j W |S  ty } zd |_||j W Y d }~|S d }~ww |  |S )Nr   )OptionParserzNcProfile.py [-o output_file_path] [-s sort] [-m module | scriptfile] [arg] ...)usageFz-oz	--outfileoutfilezSave stats to <outfile>)desthelpdefaultz-sz--sortr   z?Sort order when printing to stdout, based on pstats.Stats classr   )rT   rU   rV   choicesz-mmodule
store_truezProfile a library module)rT   actionrU   rV   r#   r$   z(run_module(modname, run_name='__main__'))
run_modulemodnamer@   r;   )__file__rG   __package__
__cached__) ossysrunpyr   optparserQ   allow_interspersed_args
add_optionsortedr   sort_arg_dict_defaultargvprint_usageexit
parse_argsrS   pathabspathlenrX   r[   insertdirnameio	open_codecompilereadr   r   BrokenPipeErrorstdouterrno)r`   ra   rb   r   rQ   rR   parseroptionsrA   r(   globsprognamefpexcr   r   r   main   sj   






r~   r;   )Nr   )rJ   __all___lsprofrq   profiler   r   r   Profilerr   r'   r~   rG   r   r   r   r   <module>   s   



_;
