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