o
    8ήcj2                     @   s  d 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 dd Ze Zd	Zd$d
dZd%ddZG dd deZdd Zdd ZeefZefddZG dd deZG dd deZG dd deZd$ddZdd Zd  Zd!d" Z e!d#kre   dS dS )&z
This file contains `__main__` so that it can be run as a commandline tool.

This file contains functions to inspect Numba's support for a given Python
module or a Python package.
    N)errors)get_versions)
cpu_target)captured_stdoutc                  C   s0   t  d dd } | stjdtjd dS | S )Nfull.r   z>Cannot find git commit hash. Source links could be inaccurate.)categorymain)r   splitwarningswarnr   NumbaWarning)r    r   @/tmp/pip-target-vg8gfxp4/lib/python/numba/misc/help/inspector.py_get_commit   s   r   zLhttps://github.com/numba/numba/blob/{commit}/{path}#L{firstline}-L{lastline}c              	   C   s   |pt }|j}|  |j  i }i }z|| }W n ty(   d}d}Y n!w ||}|jD ]}z| ||< W q1 t	yH   d||< Y q1w ||d< ||d< ||d< |S )a  Return information about the support of a function.

    Returns
    -------
    info : dict
        Defined keys:
        - "numba_type": str or None
            The numba type object of the function if supported.
        - "explained": str
            A textual description of the support.
        - "source_infos": dict
            A dictionary containing the source location of each definition.
    Nznot supported
numba_type	explainedsource_infos)
r   typing_contextrefreshtarget_contextresolve_value_type
ValueErrorexplain_function_type	templatesget_source_infoAttributeError)functiontargettyctinfor   nbtyr   tempr   r   r   inspect_function"   s.   


r#   c                 c   s    |du ri n|}t | D ]A}|drqt| |}tjtjf}t||s&qt| ||d}||v r8|| |d< n
dj| j	|d||< |
t||d |V  qdS )zwInspect a module object and yielding results from `inspect_function()`
    for each function object in the module.
    N_)modulenameobjaliasz{module}.{name})r%   r&   )r   )dir
startswithgetattrpytypesFunctionTypeBuiltinFunctionType
isinstancedictformat__name__updater#   )r%   r   r(   r&   r'   supported_typesr    r   r   r   inspect_moduleM   s$   



r5   c                   @   s@   e Zd ZdZdd Zedd Zedd Zdd	 Zd
d Z	dS )_Statz9For gathering simple statistic of (un)supported functionsc                 C   s   d| _ d| _d S Nr   	supportedunsupportedselfr   r   r   __init__j   s   
z_Stat.__init__c                 C   s   | j | j }|S Nr8   )r<   totalr   r   r   r?   n   s   z_Stat.totalc                 C   s   | j | j d }|S )Nd   )r9   r?   )r<   ratior   r   r   rA   s   s   z_Stat.ratioc                 C   s$   | j dkrdS dj| j| j | jdS )Nr   emptyz0supported = {supported} / {total} = {ratio:.2f}%)r9   r?   rA   )r?   r1   r9   rA   r;   r   r   r   describex   s   
z_Stat.describec                 C   s   dj | jj|  dS )Nz{clsname}({describe}))clsnamerC   )r1   	__class__r2   rC   r;   r   r   r   __repr__   s   z_Stat.__repr__N)
r2   
__module____qualname____doc__r=   propertyr?   rA   rC   rF   r   r   r   r   r6   h   s    

	r6   c                 C      t dd | D  S )Nc                 s   s    | ]}| d V  qdS )r$   N)r*   .0xr   r   r   	<genexpr>   s    z(filter_private_module.<locals>.<genexpr>anymodule_componentsr   r   r   filter_private_module      rT   c                 C   rK   )Nc                 s   s    | ]}|d kV  qdS )testsNr   rL   r   r   r   rO      s    z&filter_tests_module.<locals>.<genexpr>rP   rR   r   r   r   filter_tests_module   rU   rW   c           
      #   s
   dd }| j d }tj| j||d} fdd}| j }||s#| V  |D ]]}|d }||r0q%t = zt|}W n tyI   Y W d   q%w |ddd D ]}	zt||	}W qS t	yh   d}Y  nw W d   n1 ssw   Y  t
|tjsq%|V  q%dS )	zSYield all modules in a given package.

    Recursively walks the package tree.
    c                 S      d S r>   r   )r$   r   r   r   <lambda>   s    z)list_modules_in_package.<locals>.<lambda>r   )onerrorc                    s    |  d t fddD S )Nr   c                 3   s    | ]}|  V  qd S r>   r   )rM   	filter_fnrR   r   r   rO      s    z@list_modules_in_package.<locals>.check_filter.<locals>.<genexpr>)r
   rQ   )modnamemodule_filtersrR   r   check_filter   s   
z-list_modules_in_package.<locals>.check_filter   N)r2   pkgutilwalk_packages__path__r   
__import__	Exceptionr
   r+   r   r/   r,   
ModuleType)
packager^   onerror_ignoreprefixpackage_walkerr_   r\   pkginfomodpartr   r]   r   list_modules_in_package   sH   
rn   c                   @   s    e Zd ZdZdd Zdd ZdS )	FormatterzBase class for formatters.
    c                 C   s
   || _ d S r>   )_fileobj)r<   fileobjr   r   r   r=      s   
zFormatter.__init__c                 O   s    | d| j t|i | d S )Nfile)
setdefaultrp   print)r<   argskwargsr   r   r   rt      s   zFormatter.printN)r2   rG   rH   rI   r=   rt   r   r   r   r   ro      s    ro   c                   @   H   e Zd Z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 )HTMLFormatterz Formatter that outputs HTML
    c                 C   s   dd l }||S r7   )htmlescape)r<   textry   r   r   r   rz      s   
zHTMLFormatter.escapec                 C   s   |  d|d d S )Nz<h1></h2>rt   r<   r{   r   r   r   title   s   zHTMLFormatter.titlec                 C   s   |  d|d |  d d S )Nz<h2>r|   <ul>r}   r<   r\   r   r   r   begin_module_section   s   z"HTMLFormatter.begin_module_sectionc                 C   s   |  d d S )N</ul>r}   r;   r   r   r   end_module_section   s   z HTMLFormatter.end_module_sectionc                 C   s  |  d |  d|| |  d| |  d|d |  d | D ]V\}}|rj|  d |d }	|d }
|d	 }|d
 }|  d| |	| |
| ||d |d  |  d| |d ped n|  d| t| |  d q&|  d |  d d S )N<li>z{}.<b>{}</b>z: <b>{}</b>z
<div><pre>z</pre></div>r   r&   sigfilenamelinesz)<p>defined by <b>{}</b>{} at {}:{}-{}</p>r   r`   	<p>{}</p>	docstring z<li>{}</li>r   )rt   r1   itemsrz   str)r<   r\   itemnametypenamer   sourcesr(   tclssourceimplr   r   r   r   r   r   write_supported_item   s8   



z"HTMLFormatter.write_supported_itemc                 C   s*   |  d |  d|| |  d d S )Nr   z{}.<b>{}</b>: UNSUPPORTEDr   )rt   r1   r<   r\   r   r   r   r   write_unsupported_item  s   
z$HTMLFormatter.write_unsupported_itemc                 C   s   |  d|  d S )Nr   )rt   r1   rC   )r<   statsr   r   r   write_statistic  s   zHTMLFormatter.write_statisticNr2   rG   rH   rI   rz   r   r   r   r   r   r   r   r   r   r   rx      s    !rx   c                   @   rw   )ReSTFormatterzDFormatter that output ReSTructured text format for Sphinx docs.
    c                 C   s   |S r>   r   r~   r   r   r   rz        zReSTFormatter.escapec                 C   (   |  | |  dt|  |    d S )N=rt   lenr~   r   r   r   r        
zReSTFormatter.titlec                 C   r   )N-r   r   r   r   r   r     r   z"ReSTFormatter.begin_module_sectionc                 C   s   |    d S r>   r}   r;   r   r   r   r   $  s   z ReSTFormatter.end_module_sectionc                 C   s   |  d|| |  d |    |r|  d| |    | D ]?\}}|rY|d }	|d }
|d }|d }tjt||d |d	 d
}|  d|	|
||d |d	 | q$|  dt| q$|    d S )Nz.. function:: {}.{}z   :noindex:z   Alias to: ``{}``r&   r   r   r   r   r`   )commitpath	firstlinelastlinez,   - defined by ``{}{}`` at `{}:{}-{} <{}>`_z   - defined by ``{}``)rt   r1   r   
github_urlr   r   )r<   r\   r   r   r   r   r(   r   r   r   r   r   r   source_linkr   r   r   r   '  s2   
z"ReSTFormatter.write_supported_itemc                 C   rX   r>   r   r   r   r   r   r   G  r   z$ReSTFormatter.write_unsupported_itemc                 C   sN   |j dkr| d nd}| ||j |   | |  |   d S )Nr   zThis module is not supported.z%Not showing {} unsupported functions.)r9   rt   r1   r:   rC   )r<   statmsgr   r   r   r   J  s   
zReSTFormatter.write_statisticNr   r   r   r   r   r     s     r   c           
      C   s   |  d| i }|D ]q}t }|j}| | | t|||dD ]P}|d }	|	durZ| jd7  _| j| |d j| |d | t	|	| |d |d	 |
d
d q#| jd7  _| j| |d j| |d d q#| | |   qdS )zFormat modules.
    zListings for {})r   r(   r   Nr`   r%   r&   r   r   r(   )r\   r   r   r   r   r(   )r\   r   )r   r1   r6   r2   r   rz   r5   r9   r   r   getr:   r   r   r   )
	formatterpackage_namemod_sequencer   	alias_maprl   r   r\   r    nbtyper   r   r   _format_module_infosU  s4   


r   c                 C   s   t | }t|drt|}n|g}|dkr:t|d d}t|d}t|| | W d   dS 1 s3w   Y  dS |dkrct|d d}t|d}t|| | W d   dS 1 s\w   Y  dS td	|)
a  Write listing information into a file.

    Parameters
    ----------
    package_name : str
        Name of the package to inspect.
    filename : str
        Output filename. Always overwrite.
    output_format : str
        Support formats are "html" and "rst".
    rc   ry   z.htmlw)rq   Nrstz.rstz#Output format '{}' is not supported)	rd   hasattrrn   openrx   r   r   r   r1   )r   r   output_formatrg   modsfoutfmtrr   r   r   write_listingsv  s"   


"
"r   z6
Inspect Numba support for a given top-level package.
c                  C   sl   t jtd} | jddtdd | jddddd	 | jd
dddd	 |  }|j}|j}|j}t	||| d S )N)descriptionrg   zPackage to inspect)metavartypehelpz--formatr1   ry   z!Output format; i.e. "html", "rst")destdefaultr   z--filerr   inspector_outputz8Output filename. Defaults to "inspector_output.<format>")
argparseArgumentParserprogram_descriptionadd_argumentr   
parse_argsrg   r1   rr   r   )parserru   r   r   r   r   r   r   r	     s$   r	   __main__r>   )NN)"rI   r   ra   r   typesr,   
numba.corer   numba._versionr   numba.core.registryr   numba.tests.supportr   r   r   r   r#   r5   objectr6   rT   rW   _default_module_filtersrn   ro   rx   r   r   r   stripr   r	   r2   r   r   r   r   <module>   sB    

+ 4?
A!
