o
    8Va%                     @   st  d Z ddlmZ edkr6ddlZddlZddlZddlZddlm	Z	m
Z
mZmZ e	eZee
edddZeeZejde ejddZeed	d
Zejdddd ejdddd ee eeddZejdddd ejdddd ee e \aZeredeejd f  tjdkred tjdkred tjdkred tj dkred tjdkt_tjdkt_tjdkt_tj dkt_ tjstjsed tjrtjred tjrtj red tj otj  t_!ej"Z#G d d! d!Z$i a%d"d# Z&d$d% Z'g a(d&d' Z)e* g dd(fd)d*Z+e+e_"e"d+ t(,  t(D ]	Z-e.e- q.dS dS ),zE
Import diagnostics. Run bin/diagnose_imports.py --help for details.
    )Dict__main__N)abspathdirnamejoinnormpathz..zQUsage: %prog option [options]

Import analysis for imports between SymPy modules.)usagezAnalysis optionszCOptions that define what to do. Exactly one of these must be given.z
--problemszPrint all import problems, that is: If an import pulls in a package instead of a module (e.g. sympy.core instead of sympy.core.add); if it imports a symbol that is already present; if it imports a symbol from somewhere other than the defining module.count)helpactionz	--originszsFor each imported symbol in each module, print the module that defined it. (This is useful for import refactoring.)zSort optionszThese options define the sort order for output lines. At most one of these options is allowed. Unsorted output will reflect the order in which imports happened.z--by-importerz.Sort output lines by name of importing module.z--by-originz-Sort output lines by name of imported module.z'Unexpected arguments %s (try %s --help)   z,--problems must not be given more than once.z+--origins must not be given more than once.z/--by-importer must not be given more than once.z---by-origin must not be given more than once.z4At least one of --problems and --origins is requiredz2At most one of --problems and --origins is allowedz7At most one of --by-importer and --by-origin is allowedc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )
Definitionz(Information about a symbol's definition.c                 C   s   || _ || _|| _d S N)namevaluedefiner)selfr   r   r    r   F/usr/lib/python3/dist-packages/sympy/testing/tests/diagnose_imports.py__init__`   s   
zDefinition.__init__c                 C   s
   t | jS r   )hashr   r   r   r   r   __hash__d      
zDefinition.__hash__c                 C   s   | j |j ko| j|jkS r   )r   r   r   otherr   r   r   __eq__f   s   zDefinition.__eq__c                 C   s
   | |k S r   r   r   r   r   r   __ne__h   r   zDefinition.__ne__c                 C   s   dt | jt | jf S )NzDefinition(%s, ..., %s))reprr   r   r   r   r   r   __repr__j   s   zDefinition.__repr__N)	__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   ^   s    r   c                 C   s&   | |kp| dko|dko|  |d S )z,Is a the same module as or a submodule of b?N.)
startswith)abr   r   r   	in_moduleq   s   &r(   c                 C   s
   t | dS )zgIs module relevant for import checking?

        Only imports between relevant modules will be checked.sympy)r(   )moduler   r   r   relevantu   s   
r+   c                 G   s(   t jrt| |  d S t| |  d S r   )options
by_processprintsorted_messagesappend)msgargsr   r   r   r1   }   s   r1   c                 C   s  t td}|j}|d }||jkrd|t|jf }n|}t| ||||}	|	j}
t	|rt	|
r|	j
 D ]}t||	j
| |}|tvrK|
t|< q8t|	dratjr[td| | ntd||  |dkr|}d|v r|d	sz|d
sz|dr}g }n|	j
 }|D ]T}||	j
vrtjrtd|
|| qtd||
| qt||	j
| |}t| }||
krtjrtd|
|| qtd||
| qtjrtd|
|||| qtd||
||| q|	S )a  __import__ wrapper - does not change imports at all, but tracks them.

        Default order is implemented by doing output directly.
        All other orders are implemented by collecting output information into
        a sorted list that will be emitted after all imports are processed.

        Indirect imports can only occur after the requested symbol has been
        imported directly (because the indirect import would not have a module
        to pick the symbol up from).
        So this code detects indirect imports by checking whether the symbol in
        question was already imported.

        Keeps the semantics of __import__ unchanged.r   r    z
%s line %s__path__z'Error: %s (a package) is imported by %sz$Error: %s contains package import %sN*z__init__.pyz__init__.pycz__init__.pyoz<Error: %s.%s is not defined (yet), but %s tries to import itz>Error: %s tries to import %s.%s, which did not define it (yet)z&Error: %s.%s is imported again into %szError: %s imports %s.%s againzAError: %s.%s is imported by %s, which should import %s.%s insteadz7Error: %s imports %s.%s but should import %s.%s instead)inspectZgetframeinfosys	_getframefilenamestrlinenobuiltin_importr    r+   __dict__Ziterkeysr   symbol_definershasattrr,   	by_originr1   endswith)r*   globalslocalsfromlistlevelZcaller_frameZimporter_filenameZimporter_moduleZimporter_referenceresultZimportee_modulesymbolZ
definitionZsymbol_listZsymbol_definerr   r   r   tracking_import   s   







rH   r)   )/r#   typingr   r    r7   r6   builtinsZoptparseos.pathr   r   r   r   __file__Z	this_fileZ	sympy_dirpathinsertZOptionParserZoption_parserZOptionGroupZoption_groupZ
add_optionZadd_option_group
parse_argsr,   r2   errorargvZproblemsZoriginsZby_importerr@   r-   
__import__r<   r   r>   r(   r+   r/   r1   rB   rH   sortmessager.   r   r   r   r   <module>   s   











S  S