#local imports from .. import fixer_base from ..fixer_util import Name, Call, RParen, LParen, Dot from .. import patcomp class FixSocketSendRecvReupdated(fixer_base.BaseFix): BM_compatible = True explicit = True #The user must ask for this fixer PATTERN = """ power< obj=any+ trailer< '.' method=('send' | 'recv') > args=trailer< '(' innerArgs =[any] ')' > tail=any* > """ def transform(self, node, results): #print((node)) #changing the send method if(str(results["method"][0]) == "send"): #checks if the arguments have already been converted to bytes using the encode() method alreadyConverted = False if(len(results["innerArgs"]) > 0): for foo in ((results["innerArgs"][0].children)): if(str(foo) == ".encode"): alreadyConverted = True break if(str(foo) == "struct" and str(foo.next_sibling) == ".pack" ): alreadyConverted = True break if(str(foo) == "bytes"): alreadyConverted = True break if(not alreadyConverted): print(node.children) data = node.children[2].children[1] print("Data is " + str(data)) #check for the type again and convert if necessary... node.insert_child(2, LParen()) node.insert_child(3, data) node.insert_child(4, Name(" if type(")) node.insert_child(5, data) node.insert_child(6, RParen()) node.insert_child(7, Name(" == bytes ")) node.insert_child(8, Name(" else ")) node.insert_child(9, Name("str.encode")) #node.append_child(RParen()) node.insert_child(13, RParen()) node.changed() #changing the recv method else: if(str(node.children[-2]) == ".decode" ): return callingNode = node.parent.parent.parent if(str(callingNode.children[0]).strip() == "struct" and str(callingNode.children[1]).strip()==".unpack"): pass else: node.append_child(Dot()) node.append_child(Call(Name("decode"), [Name("'utf-8'")])) node.changed()