| 1 | |
| 2 | """ Test of non-constant constant. |
| 3 | """ |
| 4 | |
| 5 | from rpython.rlib.nonconst import NonConstant |
| 6 | |
| 7 | from rpython.annotator.annrpython import RPythonAnnotator |
| 8 | from rpython.conftest import option |
| 9 | from rpython.annotator.model import SomeInstance |
| 10 | from rpython.rtyper.test.test_llinterp import interpret |
| 11 | |
| 12 | def test_nonconst(): |
| 13 | def nonconst_f(): |
| 14 | a = NonConstant(3) |
| 15 | return a |
| 16 | |
| 17 | a = RPythonAnnotator() |
| 18 | s = a.build_types(nonconst_f, []) |
| 19 | assert s.knowntype is int |
| 20 | assert not hasattr(s, 'const') |
| 21 | |
| 22 | def test_nonconst_list(): |
| 23 | def nonconst_l(): |
| 24 | a = NonConstant([1, 2, 3]) |
| 25 | return a[0] |
| 26 | |
| 27 | a = RPythonAnnotator() |
| 28 | s = a.build_types(nonconst_l, []) |
| 29 | assert s.knowntype is int |
| 30 | assert not hasattr(s, 'const') |
| 31 | |
| 32 | def test_nonconst_instance(): |
| 33 | class A: |
| 34 | pass |
| 35 | a = A() |
| 36 | |
| 37 | def nonconst_i(): |
| 38 | return NonConstant(a) |
| 39 | |
| 40 | a = RPythonAnnotator() |
| 41 | s = a.build_types(nonconst_i, []) |
| 42 | if option.view: |
| 43 | a.translator.view() |
| 44 | assert isinstance(s, SomeInstance) |
| 45 | |
| 46 | def test_bool_nonconst(): |
| 47 | def fn(): |
| 48 | return bool(NonConstant(False)) |
| 49 | |
| 50 | assert not fn() |
| 51 | |
| 52 | a = RPythonAnnotator() |
| 53 | s = a.build_types(fn, []) |
| 54 | assert s.knowntype is bool |
| 55 | assert not hasattr(s, 'const') |
| 56 | |
| 57 | if option.view: |
| 58 | a.translator.view() |
| 59 | |
| 60 | def test_already_not_const(): |
| 61 | def fn(x): |
| 62 | return NonConstant(x) |
| 63 | assert interpret(fn, [5]) == 5 |