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
|
""" Test of non-constant constant.
"""
from rpython.rlib.nonconst import NonConstant
from rpython.annotator.annrpython import RPythonAnnotator
from rpython.conftest import option
from rpython.annotator.model import SomeInstance
from rpython.rtyper.test.test_llinterp import interpret
def test_nonconst():
def nonconst_f():
a = NonConstant(3)
return a
a = RPythonAnnotator()
s = a.build_types(nonconst_f, [])
assert s.knowntype is int
assert not hasattr(s, 'const')
def test_nonconst_list():
def nonconst_l():
a = NonConstant([1, 2, 3])
return a[0]
a = RPythonAnnotator()
s = a.build_types(nonconst_l, [])
assert s.knowntype is int
assert not hasattr(s, 'const')
def test_nonconst_instance():
class A:
pass
a = A()
def nonconst_i():
return NonConstant(a)
a = RPythonAnnotator()
s = a.build_types(nonconst_i, [])
if option.view:
a.translator.view()
assert isinstance(s, SomeInstance)
def test_bool_nonconst():
def fn():
return bool(NonConstant(False))
assert not fn()
a = RPythonAnnotator()
s = a.build_types(fn, [])
assert s.knowntype is bool
assert not hasattr(s, 'const')
if option.view:
a.translator.view()
def test_already_not_const():
def fn(x):
return NonConstant(x)
assert interpret(fn, [5]) == 5
|