B
    \ cb                 @   sz   d Z dddg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rve
  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   lib/python3.7/cProfile.pyr      s    c             C   s   t t| ||||S )N)r   r   r   r   )r   globalslocalsr	   r
   r   r   r   r      s    c               @   sJ   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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StatsZ
strip_dirsZ
sort_statsprint_stats)selfr
   r   r   r   r   r   (   s    zProfile.print_statsc          	   C   s8   dd l }t|d}|   || j| W d Q R X 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 )N)disablesnapshot_stats)r   r   r   r   r   2   s    zProfile.create_statsc          
   C   s6  |   }i | _i }xX|D ]P}t|j}|j}||j }|j}|j}i }	|	|t|j< |||||	f| j|< qW x|D ]}|j	rrt|j}x|j	D ]}
y|t|
j }	W n t
k
r   wY nX |
j}||
j }|
j}|
j}||	kr|	| }||d 7 }||d 7 }||d 7 }||d 7 }||||f|	|< qW qrW d S )Nr            )Zgetstatsr   labelcodeZ	callcountZreccallcountZ
inlinetimeZ	totaltimeidZcallsKeyError)r   entriesZcallersdictsentryfuncZncZccZttZctZcallersZsubentryprevr   r   r   r   6   s>    






zProfile.snapshot_statsc             C   s   dd l }|j}| |||S )Nr   )__main____dict__r   )r   cmdr)   dictr   r   r   r   \   s    zProfile.runc             C   s(   |    zt||| W d |   X | S )N)enableexecr   )r   r+   r   r   r   r   r   r   a   s
    
zProfile.runctxc              O   sx   t | dkr| ^}}} n>| s&tdn0d|krB|d}| ^}} ntdt | d  |  z
|| |S |  X d S )Nr   z:descriptor 'runcall' of 'Profile' object needs an argumentr'   z7runcall expected at least 1 positional argument, got %dr   )len	TypeErrorpopr-   r   )argskwr   r'   r   r   r   runcallj   s    



zProfile.runcallN)r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r4   r   r   r   r   r      s   
&	c             C   s(   t | trdd| fS | j| j| jfS d 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  s|  |d | \}}||jd d < t|dkrR|jrd}	|j|d d}
nR|d }|jd| j| t|d}t| |d}	W d Q R X |dd d d}
t|	|
d |j|j n|  |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   )rB   rC   rD   choicesz-mmodule
store_truezProfile a library module)rB   actionrC   rD   r   r   z(run_module(modname, run_name='__main__'))
run_modulemodnamerbr.   r)   )__file__r5   __package__
__cached__)ossysrunpyr   Zoptparser?   Zallow_interspersed_argsZ
add_optionsortedr   Zsort_arg_dict_defaultargvZprint_usageexit
parse_argsr/   rF   rI   pathinsertdirnamer   compilereadr   rA   r
   )rO   rP   rQ   r   r?   r@   parserZoptionsr2   r"   ZglobsZprognamefpr   r   r   main   sJ    






r]   r)   )Nr   )Nr   )r8   __all__Z_lsprofZprofiler   r   r   ZProfilerr   r!   r]   r5   r   r   r   r   <module>   s   




d1