| 1 |
|
| 2 |
import types |
| 3 |
|
| 4 |
import rpython.annotator.model as annmodel |
| 5 |
import rpython.flowspace.model as flowmodel |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
def short_binding(annotator, var): |
| 10 |
try: |
| 11 |
binding = annotator.binding(var) |
| 12 |
except KeyError: |
| 13 |
return "?" |
| 14 |
if binding.is_constant(): |
| 15 |
return 'const %s' % binding.__class__.__name__ |
| 16 |
else: |
| 17 |
return binding.__class__.__name__ |
| 18 |
|
| 19 |
def graph_sig(t, g): |
| 20 |
ann = t.annotator |
| 21 |
hbinding = lambda v: short_binding(ann, v) |
| 22 |
return "%s -> %s" % ( |
| 23 |
', '.join(map(hbinding, g.getargs())), |
| 24 |
hbinding(g.getreturnvar())) |
| 25 |
|
| 26 |
class Found(Exception): |
| 27 |
pass |
| 28 |
|
| 29 |
def polluted_qgen(translator): |
| 30 |
"""list functions with still real SomeObject variables""" |
| 31 |
annotator = translator.annotator |
| 32 |
for g in translator.graphs: |
| 33 |
try: |
| 34 |
for block in g.iterblocks(): |
| 35 |
for v in block.getvariables(): |
| 36 |
s = annotator.annotation(v) |
| 37 |
if s and s.__class__ == annmodel.SomeObject and s.knowntype != type: |
| 38 |
raise Found |
| 39 |
except Found: |
| 40 |
line = "%s: %s" % (g, graph_sig(translator, g)) |
| 41 |
yield line |
| 42 |
|
| 43 |
def check_exceptblocks_qgen(translator): |
| 44 |
annotator = translator.annotator |
| 45 |
for graph in translator.graphs: |
| 46 |
et, ev = graph.exceptblock.inputargs |
| 47 |
s_et = annotator.annotation(et) |
| 48 |
s_ev = annotator.annotation(ev) |
| 49 |
if s_et: |
| 50 |
if s_et.knowntype == type: |
| 51 |
if s_et.__class__ == annmodel.SomeTypeOf: |
| 52 |
if hasattr(s_et, 'is_type_of') and s_et.is_type_of == [ev]: |
| 53 |
continue |
| 54 |
else: |
| 55 |
if s_et.__class__ == annmodel.SomePBC: |
| 56 |
continue |
| 57 |
yield "%s exceptblock is not completely sane" % graph.name |
| 58 |
|
| 59 |
def check_methods_qgen(translator): |
| 60 |
from rpython.annotator.description import FunctionDesc, MethodDesc |
| 61 |
def ismeth(s_val): |
| 62 |
if not isinstance(s_val, annmodel.SomePBC): |
| 63 |
return False |
| 64 |
if isinstance(s_val, annmodel.SomeNone): |
| 65 |
return False |
| 66 |
return s_val.getKind() is MethodDesc |
| 67 |
bk = translator.annotator.bookkeeper |
| 68 |
classdefs = bk.classdefs |
| 69 |
withmeths = [] |
| 70 |
for clsdef in classdefs: |
| 71 |
meths = [] |
| 72 |
for attr in clsdef.attrs.values(): |
| 73 |
if ismeth(attr.s_value): |
| 74 |
meths.append(attr) |
| 75 |
if meths: |
| 76 |
withmeths.append((clsdef, meths)) |
| 77 |
for clsdef, meths in withmeths: |
| 78 |
n = 0 |
| 79 |
subclasses = [] |
| 80 |
for clsdef1 in classdefs: |
| 81 |
if clsdef1.issubclass(clsdef): |
| 82 |
subclasses.append(clsdef1) |
| 83 |
for meth in meths: |
| 84 |
name = meth.name |
| 85 |
funcs = dict.fromkeys([desc.funcdesc |
| 86 |
for desc in meth.s_value.descriptions]) |
| 87 |
for subcls in subclasses: |
| 88 |
if not subcls.classdesc.find_source_for(name): |
| 89 |
continue |
| 90 |
c = subcls.classdesc.read_attribute(name) |
| 91 |
if isinstance(c, flowmodel.Constant): |
| 92 |
if not isinstance(c.value, (types.FunctionType, |
| 93 |
types.MethodType)): |
| 94 |
continue |
| 95 |
c = bk.getdesc(c.value) |
| 96 |
if isinstance(c, FunctionDesc): |
| 97 |
if c not in funcs: |
| 98 |
yield "lost method: %s %s %s %s" % (name, subcls.name, clsdef.name, subcls.attrs.keys() ) |
| 99 |
|
| 100 |
def qoutput(queryg, write=None): |
| 101 |
if write is None: |
| 102 |
def write(s): |
| 103 |
print s |
| 104 |
c = 0 |
| 105 |
for bit in queryg: |
| 106 |
write(bit) |
| 107 |
c += 1 |
| 108 |
return c |
| 109 |
|
| 110 |
def polluted(translator): |
| 111 |
c = qoutput(polluted_qgen(translator)) |
| 112 |
print c |
| 113 |
|
| 114 |
def sanity_check_methods(translator): |
| 115 |
lost = qoutput(check_methods_qgen(translator)) |
| 116 |
print lost |