@@ -129,8 +129,9 @@ def fake_attribute_from_tokens(root,tokens,**kwargs):
129
129
130
130
def is_store (root ):
131
131
return match_node (root ,ast .Store ) or (match_node (root ,ast .Name ) and match_node (root .ctx ,ast .Store ))
132
+
132
133
def single (root ):
133
- return match_parent (node ,(),ast .Attribute )
134
+ return match_parent (root ,(),ast .Attribute )
134
135
135
136
def name (root ):
136
137
return match_node (root ,ast .Name )
@@ -207,8 +208,6 @@ def get_weak_header(root,atok):
207
208
root .items if match_node (root ,(ast .With )) else
208
209
root .type if match_node (root ,(ast .ExceptHandler )) else None
209
210
)
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
212
211
213
212
214
213
@@ -255,12 +254,12 @@ def get_return_value(root):
255
254
# need to revisit
256
255
def get_elements (root ):
257
256
return (
258
- root .elts if hasattr (root ,elts ) else None
257
+ root .elts if hasattr (root ," elts" ) else None
259
258
)
260
259
261
260
def get_context (root ):
262
261
return (
263
- root .ctx if hasattr (root ,ctx ) else None
262
+ root .ctx if hasattr (root ," ctx" ) else None
264
263
)
265
264
266
265
def get_key_value (root ):
@@ -321,6 +320,17 @@ def get_container_check(root):
321
320
def get_membership (root ):
322
321
return root if match_node (root ,ast .Compare ) and all ([match_node (x ,(ast .In ,ast .NotIn )) for x in root .ops ]) else None
323
322
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
+
324
334
# Extract Left Middle And Right from numerical comparisons
325
335
def get_comparison_left_side (root ):
326
336
return root .left if match_node (root ,ast .Compare ) else None
@@ -667,28 +677,29 @@ def get_subparts_of_string(root,name_mode = False):
667
677
start_position = 1
668
678
if not check_fake (root ):
669
679
x = root .first_token .string
670
- # print("String:\n",x)
671
680
y1 = x .find ("'" )
672
681
y2 = x .find ("\" " )
673
682
if y1 >= 0 and y2 >= 0 :
674
- z = mean (y1 ,y2 )
683
+ z = min (y1 ,y2 )
675
684
elif y1 >= 0 :
676
685
z = y1
677
686
elif y2 >= 0 :
678
687
z = y2
679
688
else :
680
689
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
681
695
start_position += z
682
696
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)
685
697
original = root .s if not name_mode else root .id
686
698
try :
687
699
splitted = split_string (root .s if not name_mode else root .id ,even_letters = False if name_mode else True )
688
700
except :
689
701
print (" exceptions were thrown" )
690
702
index = 0
691
- print ("splitted " ,splitted )
692
703
for s in splitted :
693
704
if not s :
694
705
continue
@@ -881,12 +892,11 @@ def get_raw(root):
881
892
882
893
def correspond_to_index_in_call (root , index ,field ,field_index ):
883
894
x = get_argument_from_call (root ,index )
884
- print ("entering index taking \n " ,ast .dump (x ))
885
895
if not x :
886
896
return False
887
897
if x .parent_field == "value" :
888
898
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))
890
900
return (field , field_index )== (x .parent_field ,x .parent_field_index ) if x else False
891
901
892
902
@@ -1016,6 +1026,9 @@ def fix_alias(root,atok):
1016
1026
def fix_argument (root ,atok ,token = None ):
1017
1027
if already_fixed (root ):
1018
1028
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
1019
1032
if token is None :
1020
1033
fix_definition (root .parent .parent ,atok )
1021
1034
if not already_fixed (root ):
@@ -1037,15 +1050,14 @@ def fix_argument(root,atok,token = None):
1037
1050
def fix_argument_list (root ,atok ):
1038
1051
if not match_node (root ,ast .arguments ):
1039
1052
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 ):
1041
1054
return True
1042
1055
return False
1043
1056
1044
1057
1045
1058
def fix_definition (root ,atok ):
1046
1059
if already_fixed (root ):
1047
1060
return True
1048
-
1049
1061
# there is a discrepancy between the 3.3 and 3.4 versions of the abstract syntax tree
1050
1062
# in 3.3 the variable arguments and the variable keyboard arguments are stored in a little bit differently
1051
1063
x = root .args
@@ -1116,12 +1128,13 @@ def fix_exception_handler(root,atok):
1116
1128
if not root .type or not root .name :
1117
1129
mark_fixed (root )
1118
1130
return True
1119
- print ( "Exception Handler: \n " ,[ root . first_token , root . last_token ])
1131
+
1120
1132
token = root .type .last_token
1121
1133
token = atok .find_token (next_token (atok ,token ),tokenize .NAME , root .name )
1122
1134
f = root .type .first_token
1123
1135
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" )
1125
1138
set_fake (root ,"name" ,fake_name_node )
1126
1139
# root.first_token=root.type.first_token
1127
1140
# root.last_token = token
0 commit comments