# functions to query information out of the translator and annotator from the debug prompt of translateimporttypesimportreimportpypy.annotation.modelasannmodelimportpypy.objspace.flow.modelasflowmodel# query used for sanity checks by translatedefshort_binding(annotator,var):try:binding=annotator.binding(var)exceptKeyError:return"?"ifbinding.is_constant():return'const %s'%binding.__class__.__name__else:returnbinding.__class__.__name__defgraph_sig(t,g):ann=t.annotatorhbinding=lambdav:short_binding(ann,v)return"%s -> %s"%(', '.join(map(hbinding,g.getargs())),hbinding(g.getreturnvar()))classFound(Exception):passdefpolluted_qgen(translator):"""list functions with still real SomeObject variables"""annotator=translator.annotatorforgintranslator.graphs:try:forblocking.iterblocks():forvinblock.getvariables():s=annotator.binding(v,None)ifsands.__class__==annmodel.SomeObjectands.knowntype!=type:raiseFoundexceptFound:line="%s: %s"%(g,graph_sig(translator,g))yieldlinedefcheck_exceptblocks_qgen(translator):annotator=translator.annotatorforgraphintranslator.graphs:et,ev=graph.exceptblock.inputargss_et=annotator.binding(et,None)s_ev=annotator.binding(ev,None)ifs_et:ifs_et.knowntype==type:ifs_et.__class__==annmodel.SomeType:ifhasattr(s_et,'is_type_of')ands_et.is_type_of==[ev]:continueelse:ifs_et.__class__==annmodel.SomePBC:continueyield"%s exceptblock is not completely sane"%graph.namedefcheck_methods_qgen(translator):frompypy.annotation.descriptionimportFunctionDesc,MethodDescdefismeth(s_val):ifnotisinstance(s_val,annmodel.SomePBC):returnFalseifs_val.isNone():returnFalsereturns_val.getKind()isMethodDescbk=translator.annotator.bookkeeperclassdefs=bk.classdefswithmeths=[]forclsdefinclassdefs:meths=[]forattrinclsdef.attrs.values():ifismeth(attr.s_value):meths.append(attr)ifmeths:withmeths.append((clsdef,meths))forclsdef,methsinwithmeths:n=0subclasses=[]forclsdef1inclassdefs:ifclsdef1.issubclass(clsdef):subclasses.append(clsdef1)formethinmeths:name=meth.namefuncs=dict.fromkeys([desc.funcdescfordescinmeth.s_value.descriptions])forsubclsinsubclasses:ifnotsubcls.classdesc.find_source_for(name):continuec=subcls.classdesc.read_attribute(name)ifisinstance(c,flowmodel.Constant):ifnotisinstance(c.value,(types.FunctionType,types.MethodType)):continuec=bk.getdesc(c.value)ifisinstance(c,FunctionDesc):ifcnotinfuncs:yield"lost method: %s%s%s%s"%(name,subcls.name,clsdef.name,subcls.attrs.keys())defqoutput(queryg,write=None):ifwriteisNone:defwrite(s):printsc=0forbitinqueryg:write(bit)c+=1returncdefpolluted(translator):c=qoutput(polluted_qgen(translator))printcdefsanity_check_methods(translator):lost=qoutput(check_methods_qgen(translator))printlost