| 1 |
|
| 2 |
from __future__ import absolute_import |
| 3 |
|
| 4 |
import types |
| 5 |
from rpython.annotator.model import ( |
| 6 |
SomeBool, SomeInteger, SomeString, SomeFloat, SomeList, SomeDict, s_None, |
| 7 |
SomeObject, SomeInstance, SomeTuple, unionof, SomeUnicodeString, SomeType, |
| 8 |
AnnotatorError) |
| 9 |
from rpython.annotator.listdef import ListDef |
| 10 |
from rpython.annotator.dictdef import DictDef |
| 11 |
from rpython.rtyper import extregistry |
| 12 |
|
| 13 |
_annotation_cache = {} |
| 14 |
|
| 15 |
def _annotation_key(t): |
| 16 |
from rpython.rtyper import extregistry |
| 17 |
if isinstance(t, list): |
| 18 |
assert len(t) == 1 |
| 19 |
return ('list', _annotation_key(t[0])) |
| 20 |
elif isinstance(t, dict): |
| 21 |
assert len(t.keys()) == 1 |
| 22 |
return ('dict', _annotation_key(t.items()[0])) |
| 23 |
elif isinstance(t, tuple): |
| 24 |
return tuple([_annotation_key(i) for i in t]) |
| 25 |
elif extregistry.is_registered(t): |
| 26 |
|
| 27 |
return t |
| 28 |
return t |
| 29 |
|
| 30 |
def annotation(t, bookkeeper=None): |
| 31 |
if bookkeeper is None: |
| 32 |
key = _annotation_key(t) |
| 33 |
try: |
| 34 |
return _annotation_cache[key] |
| 35 |
except KeyError: |
| 36 |
t = _compute_annotation(t, bookkeeper) |
| 37 |
_annotation_cache[key] = t |
| 38 |
return t |
| 39 |
return _compute_annotation(t, bookkeeper) |
| 40 |
|
| 41 |
|
| 42 |
def _validate_annotation_size(t): |
| 43 |
try: |
| 44 |
_ = iter(t) |
| 45 |
except TypeError: |
| 46 |
return t |
| 47 |
if isinstance(t, tuple): |
| 48 |
return t |
| 49 |
if len(t) > 1: |
| 50 |
raise TypeError("Cannot specify multiple types in a %s (try using tuple)", type(t)) |
| 51 |
|
| 52 |
|
| 53 |
def _compute_annotation(t, bookkeeper=None): |
| 54 |
from rpython.rtyper.lltypesystem import lltype |
| 55 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 56 |
_validate_annotation_size(t) |
| 57 |
if isinstance(t, SomeObject): |
| 58 |
return t |
| 59 |
elif isinstance(t, lltype.LowLevelType): |
| 60 |
return lltype_to_annotation(t) |
| 61 |
elif isinstance(t, list): |
| 62 |
return SomeList( |
| 63 |
ListDef(bookkeeper, annotation(t[0]), |
| 64 |
mutated=True, resized=True)) |
| 65 |
elif isinstance(t, tuple): |
| 66 |
return SomeTuple(tuple([annotation(i) for i in t])) |
| 67 |
elif isinstance(t, dict): |
| 68 |
return SomeDict( |
| 69 |
DictDef(bookkeeper, |
| 70 |
annotation(t.keys()[0]), annotation(t.values()[0]))) |
| 71 |
elif type(t) is types.NoneType: |
| 72 |
return s_None |
| 73 |
elif extregistry.is_registered(t): |
| 74 |
entry = extregistry.lookup(t) |
| 75 |
entry.bookkeeper = bookkeeper |
| 76 |
return entry.compute_result_annotation() |
| 77 |
else: |
| 78 |
return annotationoftype(t, bookkeeper) |
| 79 |
|
| 80 |
def annotationoftype(t, bookkeeper=False): |
| 81 |
from rpython.rtyper import extregistry |
| 82 |
|
| 83 |
"""The most precise SomeValue instance that contains all |
| 84 |
objects of type t.""" |
| 85 |
assert isinstance(t, (type, types.ClassType)) |
| 86 |
if t is bool: |
| 87 |
return SomeBool() |
| 88 |
elif t is int: |
| 89 |
return SomeInteger() |
| 90 |
elif t is float: |
| 91 |
return SomeFloat() |
| 92 |
elif issubclass(t, str): |
| 93 |
return SomeString() |
| 94 |
elif t is unicode: |
| 95 |
return SomeUnicodeString() |
| 96 |
elif t is types.NoneType: |
| 97 |
return s_None |
| 98 |
elif bookkeeper and extregistry.is_registered_type(t): |
| 99 |
return (extregistry.lookup_type(t) |
| 100 |
.compute_annotation_bk(bookkeeper)) |
| 101 |
elif t is type: |
| 102 |
return SomeType() |
| 103 |
elif bookkeeper and not hasattr(t, '_freeze_'): |
| 104 |
return SomeInstance(bookkeeper.getuniqueclassdef(t)) |
| 105 |
else: |
| 106 |
raise TypeError("Annotation of type %r not supported" % (t,)) |
| 107 |
|
| 108 |
class Sig(object): |
| 109 |
|
| 110 |
def __init__(self, *argtypes): |
| 111 |
self.argtypes = argtypes |
| 112 |
|
| 113 |
def __call__(self, funcdesc, inputcells): |
| 114 |
from rpython.rlib.objectmodel import NOT_CONSTANT |
| 115 |
from rpython.rtyper.lltypesystem import lltype |
| 116 |
args_s = [] |
| 117 |
from rpython.annotator import model as annmodel |
| 118 |
for i, argtype in enumerate(self.argtypes): |
| 119 |
if isinstance(argtype, (types.FunctionType, types.MethodType)): |
| 120 |
argtype = argtype(*inputcells) |
| 121 |
if argtype is lltype.Void: |
| 122 |
|
| 123 |
|
| 124 |
s_input = inputcells[i] |
| 125 |
assert isinstance(s_input, (annmodel.SomePBC, annmodel.SomeNone)) |
| 126 |
assert s_input.is_constant() |
| 127 |
args_s.append(s_input) |
| 128 |
elif argtype is None: |
| 129 |
args_s.append(inputcells[i]) |
| 130 |
elif argtype is NOT_CONSTANT: |
| 131 |
from rpython.annotator.model import not_const |
| 132 |
args_s.append(not_const(inputcells[i])) |
| 133 |
else: |
| 134 |
args_s.append(annotation(argtype, bookkeeper=funcdesc.bookkeeper)) |
| 135 |
if len(inputcells) != len(args_s): |
| 136 |
raise SignatureError("%r: expected %d args, got %d" % (funcdesc, |
| 137 |
len(args_s), |
| 138 |
len(inputcells))) |
| 139 |
for i, (s_arg, s_input) in enumerate(zip(args_s, inputcells)): |
| 140 |
s_input = unionof(s_input, s_arg) |
| 141 |
if not s_arg.contains(s_input): |
| 142 |
raise SignatureError("%r argument %d:\n" |
| 143 |
"expected %s,\n" |
| 144 |
" got %s" % (funcdesc, i+1, |
| 145 |
s_arg, |
| 146 |
s_input)) |
| 147 |
inputcells[:] = args_s |
| 148 |
|
| 149 |
class SignatureError(AnnotatorError): |
| 150 |
pass |
| 151 |
|
| 152 |
def finish_type(paramtype, bookkeeper, func): |
| 153 |
from rpython.rlib.types import SelfTypeMarker, AnyTypeMarker |
| 154 |
if isinstance(paramtype, SomeObject): |
| 155 |
return paramtype |
| 156 |
elif isinstance(paramtype, SelfTypeMarker): |
| 157 |
raise SignatureError("%r argument declared as annotation.types.self(); class needs decorator rlib.signature.finishsigs()" % (func,)) |
| 158 |
elif isinstance(paramtype, AnyTypeMarker): |
| 159 |
return None |
| 160 |
else: |
| 161 |
return paramtype(bookkeeper) |
| 162 |
|
| 163 |
def enforce_signature_args(funcdesc, paramtypes, actualtypes): |
| 164 |
assert len(paramtypes) == len(actualtypes) |
| 165 |
params_s = [finish_type(paramtype, funcdesc.bookkeeper, funcdesc.pyobj) for paramtype in paramtypes] |
| 166 |
for i, (s_param, s_actual) in enumerate(zip(params_s, actualtypes)): |
| 167 |
if s_param is None: |
| 168 |
continue |
| 169 |
if not s_param.contains(s_actual): |
| 170 |
raise SignatureError("%r argument %d:\n" |
| 171 |
"expected %s,\n" |
| 172 |
" got %s" % (funcdesc, i+1, s_param, s_actual)) |
| 173 |
for i, s_param in enumerate(params_s): |
| 174 |
if s_param is None: |
| 175 |
continue |
| 176 |
actualtypes[i] = s_param |
| 177 |
|
| 178 |
def enforce_signature_return(funcdesc, sigtype, inferredtype): |
| 179 |
s_sigret = finish_type(sigtype, funcdesc.bookkeeper, funcdesc.pyobj) |
| 180 |
if s_sigret is not None and not s_sigret.contains(inferredtype): |
| 181 |
raise SignatureError("%r return value:\n" |
| 182 |
"expected %s,\n" |
| 183 |
" got %s" % (funcdesc, s_sigret, inferredtype)) |
| 184 |
return s_sigret |