<s> from __future__ import print_function , absolute_import <EOL> import os <EOL> import sys <EOL> import bz2 <EOL> import ast <EOL> import json <EOL> import time <EOL> from flexx . util . testing import run_tests_if_main , raises , skipif <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . dirname ( os . path . dirname ( __file__ ) ) ) <EOL> import commonast <EOL> dirname = os . path . dirname ( __file__ ) <EOL> filename1 = os . path . join ( dirname , '<STR_LIT>' ) <EOL> filename2 = os . path . join ( dirname , '<STR_LIT>' ) <EOL> filename3 = os . path . join ( dirname , '<STR_LIT>' ) <EOL> def _export_python_sample_ast ( ) : <EOL> if sys . version_info > ( <NUM_LIT:3> , ) : <EOL> filenames = filename1 , filename3 <EOL> else : <EOL> filenames = filename2 , <EOL> for filename in filenames : <EOL> filename_bz2 = filename [ : - <NUM_LIT:2> ] + '<STR_LIT>' <EOL> code = open ( filename , '<STR_LIT:rb>' ) . read ( ) . decode ( ) <EOL> root = commonast . parse ( code ) <EOL> ast_bin = bz2 . compress ( root . tojson ( indent = None ) . encode ( ) ) <EOL> with open ( filename_bz2 , '<STR_LIT:wb>' ) as f : <EOL> n = f . write ( ast_bin ) or '<STR_LIT>' <EOL> print ( '<STR_LIT>' % ( n , filename_bz2 ) ) <EOL> def _get_ref_json ( filename ) : <EOL> filename_bz2 = filename [ : - <NUM_LIT:2> ] + '<STR_LIT>' <EOL> js_ref = bz2 . decompress ( open ( filename_bz2 , '<STR_LIT:rb>' ) . read ( ) ) . decode ( ) <EOL> return json . dumps ( json . loads ( js_ref ) , indent = <NUM_LIT:2> , sort_keys = True ) <EOL> def timeit ( what , func , * args , ** kwargs ) : <EOL> t0 = time . perf_counter ( ) <EOL> func ( * args , ** kwargs ) <EOL> t1 = time . perf_counter ( ) <EOL> print ( '<STR_LIT>' % ( what , ( t1 - t0 ) ) ) <EOL> def _performance_runner ( ) : <EOL> """<STR_LIT>""" <EOL> code = open ( filename1 , '<STR_LIT:rb>' ) . read ( ) . decode ( ) * <NUM_LIT:100> <EOL> timeit ( '<STR_LIT>' , ast . parse , code ) <EOL> timeit ( '<STR_LIT>' , commonast . parse , code ) <EOL> timeit ( '<STR_LIT>' , commonast . parse , code , comments = True ) <EOL> code = """<STR_LIT>""" <EOL> code = ( code . strip ( ) + '<STR_LIT>' ) * <NUM_LIT> <EOL> print ( ) <EOL> timeit ( '<STR_LIT>' , ast . parse , code ) <EOL> timeit ( '<STR_LIT>' , commonast . parse , code ) <EOL> timeit ( '<STR_LIT>' , commonast . parse , code , comments = True ) <EOL> def test_Node_creation ( ) : <EOL> Node = commonast . Node <EOL> class MyNodeWithoutSlots ( Node ) : <EOL> pass <EOL> class MyStubNode ( Node ) : <EOL> __slots__ = ( ) <EOL> class MyNode ( Node ) : <EOL> __slots__ = '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:bar>' <EOL> stubnode = MyStubNode ( ) <EOL> stubnodes = [ MyStubNode ( ) , MyStubNode ( ) , MyStubNode ( ) ] <EOL> raises ( AssertionError , Node ) <EOL> raises ( AssertionError , MyNodeWithoutSlots ) <EOL> raises ( AssertionError , MyNode , ) <EOL> raises ( AssertionError , MyStubNode , <NUM_LIT:1> ) <EOL> raises ( AssertionError , MyNode , '<STR_LIT:a>' , '<STR_LIT>' , stubnode , stubnodes , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> node = MyNode ( '<STR_LIT:a>' , '<STR_LIT>' , stubnode , stubnodes , <NUM_LIT:1> ) <EOL> node = MyNode ( '<STR_LIT:a>' , '<STR_LIT>' , None , stubnodes , <NUM_LIT:1> ) <EOL> node = MyNode ( '<STR_LIT:a>' , '<STR_LIT>' , stubnode , [ ] , <NUM_LIT:1> ) <EOL> node = MyNode ( '<STR_LIT:a>' , '<STR_LIT>' , stubnode , stubnodes , '<STR_LIT>' ) <EOL> node = MyNode ( '<STR_LIT:a>' , '<STR_LIT>' , stubnode , stubnodes , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> node = MyNode ( '<STR_LIT:a>' , '<STR_LIT>' , stubnode , stubnodes , <NUM_LIT:1> ) <EOL> node = MyNode ( '<STR_LIT>' , '<STR_LIT>' , stubnode , stubnodes , <NUM_LIT:1> ) <EOL> raises ( AssertionError , MyNode , <NUM_LIT:1> , '<STR_LIT>' , stubnode , stubnodes , <NUM_LIT:1> ) <EOL> raises ( AssertionError , MyNode , '<STR_LIT:a>' , '<STR_LIT>' , stubnode , stubnodes , <NUM_LIT:1> ) <EOL> raises ( AssertionError , MyNode , '<STR_LIT:a>' , '<STR_LIT>' , <NUM_LIT:1> , stubnodes , <NUM_LIT:1> ) <EOL> raises ( AssertionError , MyNode , '<STR_LIT:a>' , '<STR_LIT>' , '<STR_LIT:x>' , stubnodes , <NUM_LIT:1> ) <EOL> raises ( AssertionError , MyNode , '<STR_LIT:a>' , '<STR_LIT>' , stubnode , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> raises ( AssertionError , MyNode , '<STR_LIT:a>' , '<STR_LIT>' , stubnode , [ <NUM_LIT:1> , <NUM_LIT:2> ] , <NUM_LIT:1> ) <EOL> raises ( AssertionError , MyNode , '<STR_LIT:a>' , '<STR_LIT>' , stubnode , stubnodes , stubnode ) <EOL> raises ( AssertionError , MyNode , '<STR_LIT:a>' , '<STR_LIT>' , stubnode , stubnodes , stubnodes ) <EOL> def test_json_conversion ( ) : <EOL> from commonast import Node , Assign , Name , BinOp , Bytes , Num <EOL> roota = Assign ( [ Name ( '<STR_LIT:foo>' ) ] , BinOp ( '<STR_LIT>' , Name ( '<STR_LIT:a>' ) , Num ( <NUM_LIT:3> ) ) ) <EOL> rootb = Assign ( [ Name ( '<STR_LIT:foo>' ) ] , BinOp ( '<STR_LIT>' , None , Num ( <NUM_LIT> ) ) ) <EOL> rootc = Assign ( [ Name ( '<STR_LIT:foo>' ) ] , BinOp ( '<STR_LIT>' , Bytes ( b'<STR_LIT>' ) , Num ( <NUM_LIT> ) ) ) <EOL> for node1 in ( roota , rootb , rootc ) : <EOL> js = node1 . tojson ( ) <EOL> node2 = Node . fromjson ( js ) <EOL> assert js . count ( '<STR_LIT>' ) == <NUM_LIT:1> <EOL> assert js . count ( '<STR_LIT>' ) == <NUM_LIT:1> <EOL> assert node2 . target_nodes [ <NUM_LIT:0> ] . name == node1 . target_nodes [ <NUM_LIT:0> ] . name <EOL> assert node2 . value_node . op == node1 . value_node . op <EOL> assert node2 . value_node . left_node == node1 . value_node . left_node <EOL> assert node2 . value_node . right_node . value == node1 . value_node . right_node . value <EOL> node1 == node2 <EOL> assert roota != rootb <EOL> assert roota != rootc <EOL> with raises ( ValueError ) : <EOL> roota == <NUM_LIT:5> <EOL> assert str ( roota ) == roota . tojson ( ) <EOL> assert len ( repr ( roota ) ) < <NUM_LIT> <EOL> def test_comments ( ) : <EOL> code = """<STR_LIT>""" <EOL> code = '<STR_LIT:\n>' . join ( x [ <NUM_LIT:4> : ] for x in code . splitlines ( ) ) <EOL> root = commonast . parse ( code , comments = False ) <EOL> assert '<STR_LIT>' not in root . tojson ( ) <EOL> root = commonast . parse ( code , comments = True ) <EOL> for i in range ( <NUM_LIT> ) : <EOL> assert ( '<STR_LIT>' % i ) in root . tojson ( ) <EOL> def _compare_large_strings ( text1 , text2 ) : <EOL> lines1 , lines2 = text1 . splitlines ( ) , text2 . splitlines ( ) <EOL> for i in range ( <NUM_LIT:0> , max ( len ( lines1 ) , len ( lines2 ) ) , <NUM_LIT:4> ) : <EOL> prefix = '<STR_LIT>' % i <EOL> sec1 = prefix + '<STR_LIT:\n>' . join ( lines1 [ i : i + <NUM_LIT:8> ] ) <EOL> sec2 = prefix + '<STR_LIT:\n>' . join ( lines2 [ i : i + <NUM_LIT:8> ] ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert sec1 == sec2 <EOL> def test_compare_print ( ) : <EOL> ast = commonast . parse ( '<STR_LIT>' ) <EOL> n = ast . body_nodes [ <NUM_LIT:0> ] . value_node <EOL> assert isinstance ( n , commonast . Call ) <EOL> assert n . func_node . name == '<STR_LIT>' <EOL> assert len ( n . arg_nodes ) == <NUM_LIT:2> <EOL> assert n . arg_nodes [ <NUM_LIT:0> ] . name == '<STR_LIT:foo>' <EOL> assert n . arg_nodes [ <NUM_LIT:1> ] . name == '<STR_LIT:bar>' <EOL> def test_consistent_ast ( ) : <EOL> code = open ( filename1 , '<STR_LIT:rb>' ) . read ( ) . decode ( ) <EOL> root = commonast . parse ( code ) <EOL> js = root . tojson ( ) <EOL> _compare_large_strings ( _get_ref_json ( filename1 ) , js ) <EOL> @ skipif ( sys . version_info > ( <NUM_LIT:3> , ) , reason = '<STR_LIT>' ) <EOL> def test_consistent_ast2 ( ) : <EOL> code = open ( filename2 , '<STR_LIT:rb>' ) . read ( ) . decode ( ) <EOL> root = commonast . parse ( code ) <EOL> js = root . tojson ( ) <EOL> _compare_large_strings ( _get_ref_json ( filename2 ) , js ) <EOL> @ skipif ( sys . version_info < ( <NUM_LIT:3> , ) , reason = '<STR_LIT>' ) <EOL> def test_consistent_ast3 ( ) : <EOL> code = open ( filename3 , '<STR_LIT:rb>' ) . read ( ) . decode ( ) <EOL> root = commonast . parse ( code ) <EOL> js = root . tojson ( ) <EOL> _compare_large_strings ( _get_ref_json ( filename3 ) , js ) <EOL> @ skipif ( sys . version_info < ( <NUM_LIT:3> , ) , reason = '<STR_LIT>' ) <EOL> def test_functiondef_some_more ( ) : <EOL> code = """<STR_LIT>""" <EOL> code = '<STR_LIT:\n>' . join ( x [ <NUM_LIT:4> : ] for x in code . splitlines ( ) ) <EOL> root = commonast . parse ( code ) <EOL> assert len ( root . body_nodes ) == <NUM_LIT:2> <EOL> f1 , f2 = root . body_nodes <EOL> assert isinstance ( f1 , commonast . FunctionDef ) <EOL> assert isinstance ( f2 , commonast . FunctionDef ) <EOL> assert len ( f1 . arg_nodes ) == <NUM_LIT:2> <EOL> assert len ( f1 . kwarg_nodes ) == <NUM_LIT:2> <EOL> for arg , name in zip ( f1 . arg_nodes + f1 . kwarg_nodes , '<STR_LIT>' ) : <EOL> assert isinstance ( arg , commonast . Arg ) <EOL> assert arg . name == name <EOL> assert arg . annotation_node is None <EOL> assert f1 . arg_nodes [ <NUM_LIT:0> ] . value_node is None <EOL> assert f1 . arg_nodes [ <NUM_LIT:1> ] . value_node . value == <NUM_LIT:3> <EOL> assert f1 . kwarg_nodes [ <NUM_LIT:0> ] . value_node . value == <NUM_LIT:4> <EOL> assert f1 . kwarg_nodes [ <NUM_LIT:1> ] . value_node is None <EOL> assert len ( f2 . arg_nodes ) == <NUM_LIT:2> <EOL> assert len ( f2 . kwarg_nodes ) == <NUM_LIT:0> <EOL> for arg , name in zip ( f2 . arg_nodes + [ f2 . args_node , f2 . kwargs_node ] , '<STR_LIT>' ) : <EOL> assert isinstance ( arg , commonast . Arg ) <EOL> assert arg . name == name <EOL> assert f2 . args_node . value_node is None <EOL> assert f2 . kwargs_node . value_node is None <EOL> assert isinstance ( f2 . arg_nodes [ <NUM_LIT:0> ] . annotation_node , commonast . List ) <EOL> assert isinstance ( f2 . arg_nodes [ <NUM_LIT:1> ] . annotation_node , commonast . Tuple ) <EOL> assert isinstance ( f2 . args_node . annotation_node , commonast . Str ) <EOL> assert isinstance ( f2 . kwargs_node . annotation_node , commonast . Str ) <EOL> def test_call_some_more ( ) : <EOL> from commonast import Name , Num , Starred , Keyword <EOL> code = "<STR_LIT>" <EOL> node = commonast . parse ( code ) . body_nodes [ <NUM_LIT:0> ] . value_node <EOL> assert isinstance ( node , commonast . Call ) <EOL> assert len ( node . arg_nodes ) == <NUM_LIT:3> <EOL> assert len ( node . kwarg_nodes ) == <NUM_LIT:2> <EOL> for arg , cls in zip ( node . arg_nodes + node . kwarg_nodes , <EOL> [ Num , Name , Starred , Num , None . __class__ ] ) : <EOL> isinstance ( arg , cls ) <EOL> assert node . arg_nodes [ <NUM_LIT:2> ] . value_node . name == '<STR_LIT:b>' <EOL> assert node . kwarg_nodes [ <NUM_LIT:1> ] . name is None <EOL> assert node . kwarg_nodes [ <NUM_LIT:1> ] . value_node . name == '<STR_LIT:d>' <EOL> @ skipif ( sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:5> ) , reason = '<STR_LIT>' ) <EOL> def test_call_even_some_more ( ) : <EOL> from commonast import Name , Num , Starred , Keyword <EOL> code = "<STR_LIT>" <EOL> node = commonast . parse ( code ) . body_nodes [ <NUM_LIT:0> ] . value_node <EOL> assert len ( node . arg_nodes ) == <NUM_LIT:4> <EOL> assert len ( node . kwarg_nodes ) == <NUM_LIT:2> <EOL> for arg , cls in zip ( node . arg_nodes + node . kwarg_nodes , <EOL> [ Name , Starred , Name , Starred , Keyword , Keyword ] ) : <EOL> isinstance ( arg , cls ) <EOL> @ skipif ( sys . version_info < ( <NUM_LIT:3> , ) , reason = '<STR_LIT>' ) <EOL> def test_classdef_some_more ( ) : <EOL> code = "<STR_LIT>" <EOL> node = commonast . parse ( code ) . body_nodes [ <NUM_LIT:0> ] <EOL> assert isinstance ( node , commonast . ClassDef ) <EOL> assert len ( node . arg_nodes ) == <NUM_LIT:2> <EOL> assert len ( node . kwarg_nodes ) == <NUM_LIT:2> <EOL> assert node . arg_nodes [ <NUM_LIT:0> ] . name == '<STR_LIT>' <EOL> assert isinstance ( node . arg_nodes [ <NUM_LIT:1> ] , commonast . Starred ) <EOL> assert node . arg_nodes [ <NUM_LIT:1> ] . value_node . name == '<STR_LIT>' <EOL> assert node . kwarg_nodes [ <NUM_LIT:0> ] . name == '<STR_LIT>' <EOL> assert node . kwarg_nodes [ <NUM_LIT:0> ] . value_node . name == '<STR_LIT:X>' <EOL> assert node . kwarg_nodes [ <NUM_LIT:1> ] . name is None <EOL> assert node . kwarg_nodes [ <NUM_LIT:1> ] . value_node . name == '<STR_LIT>' <EOL> @ skipif ( sys . version_info > ( <NUM_LIT:3> , ) , reason = '<STR_LIT>' ) <EOL> def test_python2_old_syntax ( ) : <EOL> with raises ( RuntimeError ) : <EOL> commonast . parse ( '<STR_LIT>' ) <EOL> assert commonast . parse ( '<STR_LIT>' ) == commonast . parse ( '<STR_LIT>' ) <EOL> assert commonast . parse ( '<STR_LIT>' ) == commonast . parse ( '<STR_LIT>' ) <EOL> assert commonast . parse ( '<STR_LIT>' ) == commonast . parse ( '<STR_LIT>' ) <EOL> @ skipif ( sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:3> ) , reason = '<STR_LIT>' ) <EOL> def test_python_33_plus ( ) : <EOL> code = "<STR_LIT>" <EOL> root = commonast . parse ( code ) . body_nodes [ <NUM_LIT:0> ] <EOL> node = root . body_nodes [ <NUM_LIT:0> ] . value_node <EOL> assert isinstance ( node , commonast . YieldFrom ) <EOL> run_tests_if_main ( ) </s>
