<s> from os import walk , sep , pardir <EOL> from os . path import split , join , abspath , exists , isfile <EOL> from glob import glob <EOL> import re <EOL> import random <EOL> import ast <EOL> from sympy . core . compatibility import PY3 <EOL> sepd = { "<STR_LIT>" : sep } <EOL> SYMPY_PATH = abspath ( join ( split ( __file__ ) [ <NUM_LIT:0> ] , pardir , pardir ) ) <EOL> assert exists ( SYMPY_PATH ) <EOL> TOP_PATH = abspath ( join ( SYMPY_PATH , pardir ) ) <EOL> BIN_PATH = join ( TOP_PATH , "<STR_LIT>" ) <EOL> EXAMPLES_PATH = join ( TOP_PATH , "<STR_LIT>" ) <EOL> message_space = "<STR_LIT>" <EOL> message_implicit = "<STR_LIT>" <EOL> message_tabs = "<STR_LIT>" <EOL> message_carriage = "<STR_LIT>" <EOL> message_str_raise = "<STR_LIT>" <EOL> message_gen_raise = "<STR_LIT>" <EOL> message_old_raise = "<STR_LIT>" <EOL> message_eof = "<STR_LIT>" <EOL> message_multi_eof = "<STR_LIT>" <EOL> message_test_suite_def = "<STR_LIT>" <EOL> message_duplicate_test = "<STR_LIT>" <EOL> message_self_assignments = "<STR_LIT>" <EOL> implicit_test_re = re . compile ( r'<STR_LIT>' ) <EOL> str_raise_re = re . compile ( <EOL> r'<STR_LIT>' ) <EOL> gen_raise_re = re . compile ( <EOL> r'<STR_LIT>' ) <EOL> old_raise_re = re . compile ( r'<STR_LIT>' ) <EOL> test_suite_def_re = re . compile ( r'<STR_LIT>' ) <EOL> test_ok_def_re = re . compile ( r'<STR_LIT>' ) <EOL> test_file_re = re . compile ( r'<STR_LIT>' ) <EOL> def tab_in_leading ( s ) : <EOL> """<STR_LIT>""" <EOL> n = len ( s ) - len ( s . lstrip ( ) ) <EOL> if not s [ n : n + <NUM_LIT:3> ] in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> check = s [ : n ] <EOL> else : <EOL> smore = s [ n + <NUM_LIT:3> : ] <EOL> check = s [ : n ] + smore [ : len ( smore ) - len ( smore . lstrip ( ) ) ] <EOL> return not ( check . expandtabs ( ) == check ) <EOL> def find_self_assignments ( s ) : <EOL> """<STR_LIT>""" <EOL> t = [ n for n in ast . parse ( s ) . body if isinstance ( n , ast . ClassDef ) ] <EOL> bad = [ ] <EOL> for c in t : <EOL> for n in c . body : <EOL> if not isinstance ( n , ast . FunctionDef ) : <EOL> continue <EOL> if any ( d . id == '<STR_LIT>' <EOL> for d in n . decorator_list if isinstance ( d , ast . Name ) ) : <EOL> continue <EOL> if n . name == '<STR_LIT>' : <EOL> continue <EOL> if not n . args . args : <EOL> continue <EOL> if PY3 : <EOL> first_arg = n . args . args [ <NUM_LIT:0> ] . arg <EOL> else : <EOL> first_arg = n . args . args [ <NUM_LIT:0> ] . id <EOL> for m in ast . walk ( n ) : <EOL> if isinstance ( m , ast . Assign ) : <EOL> for a in m . targets : <EOL> if isinstance ( a , ast . Name ) and a . id == first_arg : <EOL> bad . append ( m ) <EOL> elif ( isinstance ( a , ast . Tuple ) and <EOL> any ( q . id == first_arg for q in a . elts <EOL> if isinstance ( q , ast . Name ) ) ) : <EOL> bad . append ( m ) <EOL> return bad <EOL> def check_directory_tree ( base_path , file_check , exclusions = set ( ) , pattern = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if not base_path : <EOL> return <EOL> for root , dirs , files in walk ( base_path ) : <EOL> check_files ( glob ( join ( root , pattern ) ) , file_check , exclusions ) <EOL> def check_files ( files , file_check , exclusions = set ( ) , pattern = None ) : <EOL> """<STR_LIT>""" <EOL> if not files : <EOL> return <EOL> for fname in files : <EOL> if not exists ( fname ) or not isfile ( fname ) : <EOL> continue <EOL> if any ( ex in fname for ex in exclusions ) : <EOL> continue <EOL> if pattern is None or re . match ( pattern , fname ) : <EOL> file_check ( fname ) <EOL> def test_files ( ) : <EOL> """<STR_LIT>""" <EOL> def test ( fname ) : <EOL> if PY3 : <EOL> with open ( fname , "<STR_LIT>" , encoding = "<STR_LIT:utf8>" ) as test_file : <EOL> test_this_file ( fname , test_file ) <EOL> else : <EOL> with open ( fname , "<STR_LIT>" ) as test_file : <EOL> test_this_file ( fname , test_file ) <EOL> with open ( fname , "<STR_LIT>" ) as test_file : <EOL> source = test_file . read ( ) <EOL> result = find_self_assignments ( source ) <EOL> if result : <EOL> assert False , message_self_assignments % ( fname , <EOL> result [ <NUM_LIT:0> ] . lineno ) <EOL> def test_this_file ( fname , test_file ) : <EOL> line = None <EOL> tests = <NUM_LIT:0> <EOL> test_set = set ( ) <EOL> for idx , line in enumerate ( test_file ) : <EOL> if test_file_re . match ( fname ) : <EOL> if test_suite_def_re . match ( line ) : <EOL> assert False , message_test_suite_def % ( fname , idx + <NUM_LIT:1> ) <EOL> if test_ok_def_re . match ( line ) : <EOL> tests += <NUM_LIT:1> <EOL> test_set . add ( line [ <NUM_LIT:3> : ] . split ( '<STR_LIT:(>' ) [ <NUM_LIT:0> ] . strip ( ) ) <EOL> if len ( test_set ) != tests : <EOL> assert False , message_duplicate_test % ( fname , idx + <NUM_LIT:1> ) <EOL> if line . endswith ( "<STR_LIT>" ) or line . endswith ( "<STR_LIT>" ) : <EOL> assert False , message_space % ( fname , idx + <NUM_LIT:1> ) <EOL> if line . endswith ( "<STR_LIT:\r\n>" ) : <EOL> assert False , message_carriage % ( fname , idx + <NUM_LIT:1> ) <EOL> if tab_in_leading ( line ) : <EOL> assert False , message_tabs % ( fname , idx + <NUM_LIT:1> ) <EOL> if str_raise_re . search ( line ) : <EOL> assert False , message_str_raise % ( fname , idx + <NUM_LIT:1> ) <EOL> if gen_raise_re . search ( line ) : <EOL> assert False , message_gen_raise % ( fname , idx + <NUM_LIT:1> ) <EOL> if ( implicit_test_re . search ( line ) and <EOL> not filter ( lambda ex : ex in fname , import_exclude ) ) : <EOL> assert False , message_implicit % ( fname , idx + <NUM_LIT:1> ) <EOL> result = old_raise_re . search ( line ) <EOL> if result is not None : <EOL> assert False , message_old_raise % ( <EOL> fname , idx + <NUM_LIT:1> , result . group ( <NUM_LIT:2> ) ) <EOL> if line is not None : <EOL> if line == '<STR_LIT:\n>' and idx > <NUM_LIT:0> : <EOL> assert False , message_multi_eof % ( fname , idx + <NUM_LIT:1> ) <EOL> elif not line . endswith ( '<STR_LIT:\n>' ) : <EOL> assert False , message_eof % ( fname , idx + <NUM_LIT:1> ) <EOL> top_level_files = [ join ( TOP_PATH , file ) for file in [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ] <EOL> exclude = set ( ) <EOL> import_exclude = set ( [ <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> "<STR_LIT>" % sepd , <EOL> ] ) <EOL> check_files ( top_level_files , test ) <EOL> check_directory_tree ( BIN_PATH , test , set ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) , "<STR_LIT:*>" ) <EOL> check_directory_tree ( SYMPY_PATH , test , exclude ) <EOL> check_directory_tree ( EXAMPLES_PATH , test , exclude ) <EOL> def _with_space ( c ) : <EOL> return random . randint ( <NUM_LIT:0> , <NUM_LIT:10> ) * '<STR_LIT:U+0020>' + c <EOL> def test_raise_statement_regular_expression ( ) : <EOL> candidates_ok = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> '''<STR_LIT>''' , <EOL> "<STR_LIT>" , <EOL> ] <EOL> str_candidates_fail = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" , <EOL> ] <EOL> gen_candidates_fail = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> old_candidates_fail = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> for c in candidates_ok : <EOL> assert str_raise_re . search ( _with_space ( c ) ) is None , c <EOL> assert gen_raise_re . search ( _with_space ( c ) ) is None , c <EOL> assert old_raise_re . search ( _with_space ( c ) ) is None , c <EOL> for c in str_candidates_fail : <EOL> assert str_raise_re . search ( _with_space ( c ) ) is not None , c <EOL> for c in gen_candidates_fail : <EOL> assert gen_raise_re . search ( _with_space ( c ) ) is not None , c <EOL> for c in old_candidates_fail : <EOL> assert old_raise_re . search ( _with_space ( c ) ) is not None , c <EOL> def test_implicit_imports_regular_expression ( ) : <EOL> candidates_ok = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> candidates_fail = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> for c in candidates_ok : <EOL> assert implicit_test_re . search ( _with_space ( c ) ) is None , c <EOL> for c in candidates_fail : <EOL> assert implicit_test_re . search ( _with_space ( c ) ) is not None , c <EOL> def test_test_suite_defs ( ) : <EOL> candidates_ok = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> candidates_fail = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> for c in candidates_ok : <EOL> assert test_suite_def_re . search ( c ) is None , c <EOL> for c in candidates_fail : <EOL> assert test_suite_def_re . search ( c ) is not None , c <EOL> def test_test_duplicate_defs ( ) : <EOL> candidates_ok = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> candidates_fail = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> ok = ( None , '<STR_LIT>' ) <EOL> def check ( file ) : <EOL> tests = <NUM_LIT:0> <EOL> test_set = set ( ) <EOL> for idx , line in enumerate ( file . splitlines ( ) ) : <EOL> if test_ok_def_re . match ( line ) : <EOL> tests += <NUM_LIT:1> <EOL> test_set . add ( line [ <NUM_LIT:3> : ] . split ( '<STR_LIT:(>' ) [ <NUM_LIT:0> ] . strip ( ) ) <EOL> if len ( test_set ) != tests : <EOL> return False , message_duplicate_test % ( '<STR_LIT>' , idx + <NUM_LIT:1> ) <EOL> return None , '<STR_LIT>' <EOL> for c in candidates_ok : <EOL> assert check ( c ) == ok <EOL> for c in candidates_fail : <EOL> assert check ( c ) != ok <EOL> def test_find_self_assignments ( ) : <EOL> candidates_ok = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> candidates_fail = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> for c in candidates_ok : <EOL> assert find_self_assignments ( c ) == [ ] <EOL> for c in candidates_fail : <EOL> assert find_self_assignments ( c ) != [ ] </s>
