a
    lhK                     @   s  d dl mZmZmZ d dlmZmZmZmZm	Z	 d dl
m
Z
 d dlZd dlZd dlZd dlmZ d dlmZ deiZeeeed  eeZeesJ eeeeZeedZeed	Zd
ZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%e&dZ'e&dZ(e&dZ)e&dZ*e&dZ+e&dZ,e&dZ-e&dZ.d d! Z/d"d# Z0e1 d$fd%d&Z2e1 dfd'd(Z3G d)d* d*ej4Z5e5 Z6d+d, Z7d-d. Z8d/d0 Z9d1d2 Z:d3d4 Z;d5d6 Z<d7d8 Z=d9d: Z>d;d< Z?dS )=    )walkseppardir)splitjoinabspathexistsisfile)globN)raises_test_this_file_encodingr   binZexamplesz/File contains trailing whitespace: %s, line %s.z.File contains an implicit import: %s, line %s.z2File contains tabs instead of spaces: %s, line %s.z:File contains carriage returns at end of line: %s, line %sz+File contains string exception: %s, line %sz,File contains generic exception: %s, line %sz:File contains old-style raise statement: %s, line %s, "%s"z-File does not end with a newline: %s, line %sz/File ends with more than 1 newline: %s, line %sz6Function should start with 'test_' or '_': %s, line %sz.This is a duplicate test function: %s, line %sz3File contains assignments to self/cls: %s, line %s.z&File contains '.func is': %s, line %s.z+File contains bare expression: %s, line %s.z(^\s*(>>> )?(\.\.\. )?from .* import .*\*z9^\s*(>>> )?(\.\.\. )?raise(\s+(\'|\")|\s*(\(\s*)+(\'|\"))z=^\s*(>>> )?(\.\.\. )?raise(\s+Exception|\s*(\(\s*)+Exception)z1^\s*(>>> )?(\.\.\. )?raise((\s*\(\s*)|\s+)\w+\s*,z$^def\s+(?!(_|test))[^(]*\(\s*\)\s*:$z^def\s+test_.*:$z.*[/\\]test_.*\.py$z\.\s*func\s+isc                 C   s|   t | t |   }| ||d  dvr6| d| }n8| |d d }| d| |dt |t |    }| |k S )z{Returns True if there are tabs in the leading whitespace of a line,
    including the whitespace of docstring code samples.   )z...z>>>N)lenlstrip
expandtabs)sncheckZsmore r   S/var/www/auris/lib/python3.9/site-packages/sympy/testing/tests/test_code_quality.pytab_in_leading7   s    (r   c                    s   dd t | jD }g }|D ]}|jD ]}t|t js:q(tdd |jD rPq(|jdkr\q(|jjsfq(|jjd j	 t 
|D ]l}t|t jr~|jD ]T}t|t jr|j kr|| qt|t jrt fdd|jD r|| qq~q(q|S )zReturns a list of "bad" assignments: if there are instances
    of assigning to the first argument of the class method (except
    for staticmethod's).
    c                 S   s   g | ]}t |tjr|qS r   )
isinstanceastClassDef).0r   r   r   r   
<listcomp>H       z)find_self_assignments.<locals>.<listcomp>c                 s   s$   | ]}t |tjr|jd kV  qdS )staticmethodNr   r   Nameid)r   dr   r   r   	<genexpr>O   s   z(find_self_assignments.<locals>.<genexpr>__new__r   c                 3   s$   | ]}t |tjr|j kV  qd S Nr    )r   qZ	first_argr   r   r$   ^   s   )r   parsebodyr   FunctionDefanydecorator_listnameargsargr   Assigntargetsr!   r"   appendTupleelts)r   tbadcr   mar   r(   r   find_self_assignmentsC   s2    


r;   z*.pyc                 C   s6   | sdS t | D ] \}}}ttt|||| qdS )z
    Checks all files in the directory tree (with base_path as starting point)
    with the file_check function provided, skipping files that contain
    any of the strings in the set provided by exclusions.
    N)r   check_filesr
   r   )	base_path
file_check
exclusionspatternrootdirsfilesr   r   r   check_directory_treee   s    rD   c                    s\   | sdS | D ]J t  rt s"qt fdd|D r:q|du sNt| r|  qdS )z
    Checks all files with the file_check function provided, skipping files
    that contain any of the strings in the set provided by exclusions.
    Nc                 3   s   | ]}| v V  qd S r&   r   )r   exfnamer   r   r$   {   r   zcheck_files.<locals>.<genexpr>)r   r	   r,   rematch)rC   r>   r?   r@   r   rF   r   r<   q   s    r<   c                   @   s    e Zd ZdZdd Zdd ZdS )_Visita  return the line number corresponding to the
    line on which a bare expression appears if it is a binary op
    or a comparison that is not in a with block.

    EXAMPLES
    ========

    >>> import ast
    >>> class _Visit(ast.NodeVisitor):
    ...     def visit_Expr(self, node):
    ...         if isinstance(node.value, (ast.BinOp, ast.Compare)):
    ...             print(node.lineno)
    ...     def visit_With(self, node):
    ...         pass  # no checking there
    ...
    >>> code='''x = 1    # line 1
    ... for i in range(3):
    ...     x == 2       # <-- 3
    ... if x == 2:
    ...     x == 3       # <-- 5
    ...     x + 1        # <-- 6
    ...     x = 1
    ...     if x == 1:
    ...         print(1)
    ... while x != 1:
    ...     x == 1       # <-- 11
    ... with raises(TypeError):
    ...     c == 1
    ...     raise TypeError
    ... assert x == 1
    ... '''
    >>> _Visit().visit(ast.parse(code))
    3
    5
    6
    11
    c                 C   s.   t |jtjtjfr*d s*J td|jf d S )N )r   valuer   BinOpComparemessage_bare_exprlinenoselfnoder   r   r   
visit_Expr   s    z_Visit.visit_Exprc                 C   s   d S r&   r   rQ   r   r   r   
visit_With   s    z_Visit.visit_WithN)__name__
__module____qualname____doc__rT   rU   r   r   r   r   rJ      s   %rJ   c              
   C   s   t | }zt| W nl ty } zT|js2J |jd }|tddd sVJ t	|
ddd dW  Y d}~S d}~0 0 dS )z]return None or else 0-based line number of code on which
    a bare expression appeared.
    r   :    .N)r   r)   BareExprvisitAssertionErrorr/   
startswithrO   r   intrsplitrstrip)codetreemsgr   r   r   line_with_bare_expr   s    


rh   c                     s   fdd}  fdddd dD }dt  d	t  d
t  dt  dt  h}dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  dt  h t||  tt| h d d! tt| | tt| | d"S )#a  
    This test tests all files in SymPy and checks that:
      o no lines contains a trailing whitespace
      o no lines end with 

      o no line uses tabs instead of spaces
      o that the file ends with a single newline
      o there are no general or string exceptions
      o there are no old style raise statements
      o name of arg-less test suite functions start with _ or test_
      o no duplicate function names that start with test_
      o no assignments to self variable in class methods
      o no lines contain ".func is" except in the test suite
      o there is no do-nothing expression like `a == b` or `x + 1`
    c                    sp   t | dd} | | W d    n1 s,0    Y  t | dd}t| | W d    n1 sb0    Y  d S )Nutf8)encoding)openr   )rG   	test_file)test_this_filer   r   test   s    (ztest_files.<locals>.testc           	         s  d }|  }|d t vr" n tdd }|drDt|}|d urddsdJ t |d f d }d}t }t|D ]\}}t	
 rt
|rdsJ t |d f t
|r|d7 }||dd  dd   t||krdsJ t |d f |dr4ds4J t |d f |d	rZdsZJ t |d f t|r~ds~J t |d f t|rdsJ t |d f t|rdsJ t |d f t|rtt fd
dsdsJ t  |d f t!|r:t	 s:ds:J t" |d f t#|}|d urzdszJ t$ |d |%df qz|d ur|dkr|dkrdsJ t& |d f n&|dsdsJ t' |d f d S )Nr   r[   Ztest_Fr   ()z 
z	
z
c                    s   |  v S r&   r   )rE   rF   r   r   <lambda>   r   z4test_files.<locals>.test_this_file.<locals>.<lambda>   
)(readseekr   rc   ra   rh   rO   set	enumeratetest_file_rerI   test_suite_def_remessage_test_suite_deftest_ok_def_readdr   stripr   message_duplicate_testendswithmessage_spacemessage_carriager   message_tabsstr_raise_researchmessage_str_raisegen_raise_remessage_gen_raiseimplicit_test_relistfiltermessage_implicit
func_is_remessage_func_isold_raise_remessage_old_raisegroupmessage_multi_eofmessage_eof)	rG   rl   idxre   pylineteststest_setresult)import_excluderF   r   rm      s\    


 



z"test_files.<locals>.test_this_filec                 S   s   g | ]}t t|qS r   )r   TOP_PATH)r   filer   r   r   r   
  r   ztest_files.<locals>.<listcomp>)z	isympy.pyzbuild.pyzsetup.pyzL%(sep)ssympy%(sep)sparsing%(sep)sautolev%(sep)s_antlr%(sep)sautolevparser.pyzK%(sep)ssympy%(sep)sparsing%(sep)sautolev%(sep)s_antlr%(sep)sautolevlexer.pyzN%(sep)ssympy%(sep)sparsing%(sep)sautolev%(sep)s_antlr%(sep)sautolevlistener.pyzH%(sep)ssympy%(sep)sparsing%(sep)slatex%(sep)s_antlr%(sep)slatexparser.pyzG%(sep)ssympy%(sep)sparsing%(sep)slatex%(sep)s_antlr%(sep)slatexlexer.pyz%(sep)ssympy%(sep)s__init__.pyz%(sep)svector%(sep)s__init__.pyz"%(sep)smechanics%(sep)s__init__.pyz %(sep)squantum%(sep)s__init__.pyz%(sep)spolys%(sep)s__init__.pyz,%(sep)spolys%(sep)sdomains%(sep)s__init__.pyz#%(sep)sinteractive%(sep)ssession.pyz%(sep)sisympy.pyz%(sep)sbin%(sep)ssympy_time.pyz$%(sep)sbin%(sep)ssympy_time_cache.pyz&%(sep)sparsing%(sep)ssympy_tokenize.pyz'%(sep)splotting%(sep)spygletplot%(sep)sz)%(sep)sbin%(sep)stest_external_imports.pyz*%(sep)sbin%(sep)stest_submodule_imports.pyz"%(sep)sutilities%(sep)sruntests.pyz %(sep)sutilities%(sep)spytest.pyz"%(sep)sutilities%(sep)srandtest.pyz"%(sep)sutilities%(sep)stmpfiles.pyz)%(sep)sutilities%(sep)squality_unicode.py>   z.pycz.sh~*N)sepdr<   rD   BIN_PATH
SYMPY_PATHEXAMPLES_PATH)rn   Ztop_level_filesexcluder   )r   rm   r   
test_files   sB    5

r   c                 C   s   t ddd |  S )Nr   
   r\   )randomrandint)r8   r   r   r   _with_space=  s    r   c                  C   s   g d} g d}g d}g d}| D ]R}t t|d u sBJ |tt|d u s\J |tt|d u s$J |q$|D ]}t t|d us|J |q||D ]}tt|d usJ |q|D ]}tt|d usJ |qd S )N)z#some text # raise Exception, 'text'z2raise ValueError('text') # raise Exception, 'text'raise ValueError('text')zraise ValueErrorr   zraise ValueError('text') #,zB'"""This function will raise ValueError, except when it doesn't"""zraise (ValueError('text'))zraise 'exception'zraise 'Exception'zraise "exception"zraise "Exception"zraise 'ValueError')z1raise Exception('text') # raise Exception, 'text'raise Exception('text')zraise Exceptionr   zraise Exception('text') #,raise Exception, 'text'1raise Exception, 'text' # raise Exception('text')1raise Exception, 'text' # raise Exception, 'text'>>> raise Exception, 'text'5>>> raise Exception, 'text' # raise Exception('text')5>>> raise Exception, 'text' # raise Exception, 'text')r   r   r   r   r   r   zraise ValueError, 'text'z2raise ValueError, 'text' # raise Exception('text')z2raise ValueError, 'text' # raise Exception, 'text'z>>> raise ValueError, 'text'z6>>> raise ValueError, 'text' # raise Exception('text')z6>>> raise ValueError, 'text' # raise Exception, 'text'zraise(ValueError,zraise (ValueError,zraise( ValueError,zraise ( ValueError,zraise(ValueError ,zraise (ValueError ,zraise( ValueError ,zraise ( ValueError ,)r   r   r   r   r   )candidates_okZstr_candidates_failZgen_candidates_failZold_candidates_failr8   r   r   r   'test_raise_statement_regular_expressionB  s    r   c                  C   s\   g d} g d}| D ]}t t|d u sJ |q|D ]}t t|d us8J |q8d S )N)zfrom sympy import somethingz>>> from sympy import somethingz%from sympy.somewhere import somethingz)>>> from sympy.somewhere import somethingzimport sympyz>>> import sympyz import sympy.something.somethingz... import sympyz$... import sympy.something.somethingz... from sympy import somethingz)... from sympy.somewhere import somethingz>> from sympy import *z# from sympy import *zsome text # from sympy import *)zfrom sympy import *z>>> from sympy import *zfrom sympy.somewhere import *z!>>> from sympy.somewhere import *z... from sympy import *z!... from sympy.somewhere import *)r   r   r   r   candidates_failr8   r   r   r   (test_implicit_imports_regular_expression  s    r   c                  C   sT   g d} g d}| D ]}t |d u sJ |q|D ]}t |d us4J |q4d S )N)z    def foo():
zdef foo(arg):
zdef _foo():
zdef test_foo():
)zdef foo():
zdef foo() :
zdef foo( ):
zdef  foo():
)ry   r   r   r   r   r   test_test_suite_defs  s    r   c                  C   sT   g d} ddg}d}dd }| D ]}|||ks J q |D ]}|||ks:J q:d S )N)zdef foo():
def foo():
zdef test():
def test_():
zdef test_():
def test__():
zdef test_():
def test_ ():
zdef test_1():
def  test_1():
Nr   c                 S   sz   d}t  }t|  D ]^\}}t|r|d7 }||dd  dd   t||krdt	d|d f f  S qdS )Nr   r[   r   rp   Fr   r   )
rv   rw   
splitlinesr{   rI   r|   r   r}   r   r~   )r   r   r   r   r   r   r   r   r     s    
 z'test_test_duplicate_defs.<locals>.checkr   )r   r   okr   r8   r   r   r   test_test_duplicate_defs  s    
r   c                  C   sH   g d} g d}| D ]}t |g ksJ q|D ]}t |g ks.J q.d S )N)z4class A(object):
    def foo(self, arg): arg = self
z9class A(object):
    def foo(self, arg): self.prop = arg
z?class A(object):
    def foo(self, arg): obj, obj2 = arg, self
zCclass A(object):
    @classmethod
    def bar(cls, arg): arg = cls
z2class A(object):
    def foo(var, arg): arg = var
)z4class A(object):
    def foo(self, arg): self = arg
z>class A(object):
    def foo(self, arg): obj, self = arg, arg
zCclass A(object):
    def foo(self, arg):
        if arg: self = argzCclass A(object):
    @classmethod
    def foo(cls, arg): cls = arg
z2class A(object):
    def foo(var, arg): var = arg
)r;   r   r   r   r   test_find_self_assignments  s    r   c                      s   dgdgd dgt t fdd d dgt  d dgt t fdd d dgt  d S )NZfoobarabcu   αc                      s   t  S r&   r   r   rG   rl   Zunicode_strict_whitelistZunicode_whitelistr   r   rq     s   z,test_test_unicode_encoding.<locals>.<lambda>c                      s   t  S r&   r   r   r   r   r   rq     s   )r   r`   r   r   r   r   r   test_test_unicode_encoding  s$    r   )@osr   r   r   os.pathr   r   r   r   r	   r
   rH   r   r   Zsympy.testing.pytestr   Zsympy.testing.quality_unicoder   r   __file__r   r   r   r   r   r   r   r   r   r   r   r   r   rz   r~   Zmessage_self_assignmentsr   rO   compiler   r   r   r   ry   r{   rx   r   r   r;   rv   rD   r<   NodeVisitorrJ   r^   rh   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sl   







"-~C