1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
import py
from rpython.rtyper.lltypesystem import lltype
from rpython.rtyper.test.test_llinterp import gengraph, interpret, interpret_raises
class BaseRtypingTest(object):
type_system = 'lltype'
FLOAT_PRECISION = 8
def gengraph(self, func, argtypes=[], viewbefore='auto', policy=None,
backendopt=False, config=None):
return gengraph(func, argtypes, viewbefore, policy,
backendopt=backendopt, config=config)
def interpret(self, fn, args, **kwds):
return interpret(fn, args, type_system=self.type_system, **kwds)
def interpret_raises(self, exc, fn, args, **kwds):
return interpret_raises(exc, fn, args, type_system=self.type_system, **kwds)
def float_eq(self, x, y):
return x == y
def float_eq_approx(self, x, y):
maxError = 10**-self.FLOAT_PRECISION
if abs(x-y) < maxError:
return True
if abs(y) > abs(x):
relativeError = abs((x - y) / y)
else:
relativeError = abs((x - y) / x)
return relativeError < maxError
def is_of_type(self, x, type_):
return type(x) is type_
def _skip_llinterpreter(self, reason):
py.test.skip("lltypesystem doesn't support %s, yet" % reason)
def ll_to_string(self, s):
if not s:
return None
return ''.join(s.chars)
def ll_to_unicode(self, s):
return u''.join(s.chars)
def string_to_ll(self, s):
from rpython.rtyper.module.support import LLSupport
return LLSupport.to_rstr(s)
def unicode_to_ll(self, s):
from rpython.rtyper.module.support import LLSupport
return LLSupport.to_runicode(s)
def ll_to_list(self, l):
r = []
items = l.ll_items()
for i in range(l.ll_length()):
r.append(items[i])
return r
def ll_unpack_tuple(self, t, length):
return tuple([getattr(t, 'item%d' % i) for i in range(length)])
def get_callable(self, fnptr):
return fnptr._obj._callable
def class_name(self, value):
return ''.join(value.super.typeptr.name.chars)
def read_attr(self, value, attr_name):
value = value._obj
while value is not None:
attr = getattr(value, "inst_" + attr_name, None)
if attr is None:
value = value._parentstructure()
else:
return attr
raise AttributeError()
def is_of_instance_type(self, val):
T = lltype.typeOf(val)
return isinstance(T, lltype.Ptr) and isinstance(T.TO, lltype.GcStruct)
|