@@ -129,8 +129,9 @@ def fake_attribute_from_tokens(root,tokens,**kwargs):
129129
130130def is_store (root ):
131131 return match_node (root ,ast .Store ) or (match_node (root ,ast .Name ) and match_node (root .ctx ,ast .Store ))
132+ 132133def single (root ):
133- return match_parent (node ,(),ast .Attribute )
134+ return match_parent (root ,(),ast .Attribute )
134135
135136def name (root ):
136137 return match_node (root ,ast .Name )
@@ -207,8 +208,6 @@ def get_weak_header(root,atok):
207208 root .items if match_node (root ,(ast .With )) else
208209 root .type if match_node (root ,(ast .ExceptHandler )) else None
209210 )
210- def get_body (root ):
211- return root .body if match_node (root ,(ast .IfExp , ast .If ,ast .For ,ast .While , ast .Try )) else None
212211
213212
214213
@@ -255,12 +254,12 @@ def get_return_value(root):
255254# need to revisit
256255def get_elements (root ):
257256 return (
258- root .elts if hasattr (root ,elts ) else None
257+ root .elts if hasattr (root ," elts" ) else None
259258 )
260259
261260def get_context (root ):
262261 return (
263- root .ctx if hasattr (root ,ctx ) else None
262+ root .ctx if hasattr (root ," ctx" ) else None
264263 )
265264
266265def get_key_value (root ):
@@ -321,6 +320,17 @@ def get_container_check(root):
321320def get_membership (root ):
322321 return root if match_node (root ,ast .Compare ) and all ([match_node (x ,(ast .In ,ast .NotIn )) for x in root .ops ]) else None
323322
323+ 324+ # Extracting Identity Left And Right
325+ def get_identity_check_left (root ):
326+ return root .left if match_node (root ,ast .Compare ) and all ([match_node (x ,(ast .Is ,ast .IsNot )) for x in root .ops ]) else None
327+ 328+ def get_identity_check_right (root ):
329+ return root .comparators [- 1 ] if match_node (root ,ast .Compare ) and all ([match_node (x ,(ast .Is ,ast .IsNot )) for x in root .ops ]) else None
330+ 331+ def get_identity_check (root ):
332+ return root if match_node (root ,ast .Compare ) and all ([match_node (x ,(ast .Is ,ast .IsNot )) for x in root .ops ]) else None
333+ 324334# Extract Left Middle And Right from numerical comparisons
325335def get_comparison_left_side (root ):
326336 return root .left if match_node (root ,ast .Compare ) else None
@@ -667,28 +677,29 @@ def get_subparts_of_string(root,name_mode = False):
667677 start_position = 1
668678 if not check_fake (root ):
669679 x = root .first_token .string
670- # print("String:\n",x)
671680 y1 = x .find ("'" )
672681 y2 = x .find ("\" " )
673682 if y1 >= 0 and y2 >= 0 :
674- z = mean (y1 ,y2 )
683+ z = min (y1 ,y2 )
675684 elif y1 >= 0 :
676685 z = y1
677686 elif y2 >= 0 :
678687 z = y2
679688 else :
680689 raise Exception ("problem with splitting a string , there is no beginning!" )
690+ try :
691+ if x [z ]== x [z + 1 ]== x [z + 2 ]:
692+ z = z + 2
693+ except :
694+ pass
681695 start_position += z
682696 start_position += root .first_token .startpos
683- # print("Start Position:\n",start_position)
684- # start_position = root.first_token.startpos + ( 1+(len(root.first_token.string) if root.first_token.type==tokenize.NAME else 0) if not name_mode else 0)
685697 original = root .s if not name_mode else root .id
686698 try :
687699 splitted = split_string (root .s if not name_mode else root .id ,even_letters = False if name_mode else True )
688700 except :
689701 print (" exceptions were thrown" )
690702 index = 0
691- print ("splitted " ,splitted )
692703 for s in splitted :
693704 if not s :
694705 continue
@@ -881,12 +892,11 @@ def get_raw(root):
881892
882893def correspond_to_index_in_call (root , index ,field ,field_index ):
883894 x = get_argument_from_call (root ,index )
884- print ("entering index taking \n " ,ast .dump (x ))
885895 if not x :
886896 return False
887897 if x .parent_field == "value" :
888898 x = x .parent
889- print ("inside checking for index " ,(x .parent_field ,x .parent_field_index ),(field ,field_index ))
899+ # print("inside checking for index ",(x.parent_field,x.parent_field_index),(field,field_index))
890900 return (field , field_index )== (x .parent_field ,x .parent_field_index ) if x else False
891901
892902
@@ -1016,6 +1026,9 @@ def fix_alias(root,atok):
10161026def fix_argument (root ,atok ,token = None ):
10171027 if already_fixed (root ):
10181028 return token
1029+ # the following check was introduced to work around issue #17
1030+ if not match_node (root .parent .parent ,ast .FunctionDef ):
1031+ return None
10191032 if token is None :
10201033 fix_definition (root .parent .parent ,atok )
10211034 if not already_fixed (root ):
@@ -1037,15 +1050,14 @@ def fix_argument(root,atok,token = None):
10371050def fix_argument_list (root ,atok ):
10381051 if not match_node (root ,ast .arguments ):
10391052 return False
1040- if already_fixed (root ) or fix_definition (root .parent ,atok ):
1053+ if already_fixed (root ) or match_node ( root . parent ,( ast . FunctionDef )) and fix_definition (root .parent ,atok ):
10411054 return True
10421055 return False
10431056
10441057
10451058def fix_definition (root ,atok ):
10461059 if already_fixed (root ):
10471060 return True
1048- 10491061 # there is a discrepancy between the 3.3 and 3.4 versions of the abstract syntax tree
10501062 # in 3.3 the variable arguments and the variable keyboard arguments are stored in a little bit differently
10511063 x = root .args
@@ -1116,12 +1128,13 @@ def fix_exception_handler(root,atok):
11161128 if not root .type or not root .name :
11171129 mark_fixed (root )
11181130 return True
1119- print ( "Exception Handler: \n " ,[ root . first_token , root . last_token ])
1131+ 11201132 token = root .type .last_token
11211133 token = atok .find_token (next_token (atok ,token ),tokenize .NAME , root .name )
11221134 f = root .type .first_token
11231135 f = atok .find_token (previous_token (atok ,f ),tokenize .NAME , "except" ,reverse = True )
1124- fake_name_node = create_fake (root ,ast .Name ,real_tokens = token ,id = token .string ,ctx = ast .Load ())
1136+ fake_name_node = create_fake (root ,ast .Name ,real_tokens = token ,id = token .string ,ctx = ast .Load (),
1137+ parent = root ,parent_field = "name" )
11251138 set_fake (root ,"name" ,fake_name_node )
11261139 # root.first_token=root.type.first_token
11271140 # root.last_token = token
0 commit comments