PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/rlib/test/test__jit_vref.py

https://bitbucket.org/pypy/pypy/
Python | 147 lines | 128 code | 19 blank | 0 comment | 10 complexity | 40d3794aef4ce9bc97f7bd9e44553633 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import py
  2. from rpython.rlib.jit import virtual_ref, virtual_ref_finish
  3. from rpython.rlib.jit import vref_None, non_virtual_ref, InvalidVirtualRef
  4. from rpython.rlib._jit_vref import SomeVRef
  5. from rpython.annotator import model as annmodel
  6. from rpython.annotator.annrpython import RPythonAnnotator
  7. from rpython.rtyper.rclass import OBJECTPTR
  8. from rpython.rtyper.lltypesystem import lltype
  9. from rpython.rtyper.test.tool import BaseRtypingTest
  10. class X(object):
  11. pass
  12. class Y(X):
  13. pass
  14. class Z(X):
  15. pass
  16. def test_direct_forced():
  17. x1 = X()
  18. vref = virtual_ref(x1)
  19. assert vref._state == 'non-forced'
  20. assert vref.virtual is True
  21. assert vref() is x1
  22. assert vref._state == 'forced'
  23. assert vref.virtual is False
  24. virtual_ref_finish(vref, x1)
  25. assert vref._state == 'forced'
  26. assert vref.virtual is False
  27. assert vref() is x1
  28. def test_direct_invalid():
  29. x1 = X()
  30. vref = virtual_ref(x1)
  31. assert vref._state == 'non-forced'
  32. virtual_ref_finish(vref, x1)
  33. assert vref._state == 'invalid'
  34. py.test.raises(InvalidVirtualRef, "vref()")
  35. def test_annotate_1():
  36. def f():
  37. return virtual_ref(X())
  38. a = RPythonAnnotator()
  39. s = a.build_types(f, [])
  40. assert isinstance(s, SomeVRef)
  41. assert isinstance(s.s_instance, annmodel.SomeInstance)
  42. assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X)
  43. def test_annotate_2():
  44. def f():
  45. x1 = X()
  46. vref = virtual_ref(x1)
  47. x2 = vref()
  48. virtual_ref_finish(vref, x1)
  49. return x2
  50. a = RPythonAnnotator()
  51. s = a.build_types(f, [])
  52. assert isinstance(s, annmodel.SomeInstance)
  53. assert s.classdef == a.bookkeeper.getuniqueclassdef(X)
  54. def test_annotate_3():
  55. def f(n):
  56. if n > 0:
  57. return virtual_ref(Y())
  58. else:
  59. return non_virtual_ref(Z())
  60. a = RPythonAnnotator()
  61. s = a.build_types(f, [int])
  62. assert isinstance(s, SomeVRef)
  63. assert isinstance(s.s_instance, annmodel.SomeInstance)
  64. assert not s.s_instance.can_be_None
  65. assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X)
  66. def test_annotate_4():
  67. def f(n):
  68. if n > 0:
  69. return virtual_ref(X())
  70. else:
  71. return vref_None
  72. a = RPythonAnnotator()
  73. s = a.build_types(f, [int])
  74. assert isinstance(s, SomeVRef)
  75. assert isinstance(s.s_instance, annmodel.SomeInstance)
  76. assert s.s_instance.can_be_None
  77. assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X)
  78. class TestVRef(BaseRtypingTest):
  79. OBJECTTYPE = OBJECTPTR
  80. def castable(self, TO, var):
  81. return lltype.castable(TO, lltype.typeOf(var)) > 0
  82. def test_rtype_1(self):
  83. def f():
  84. return virtual_ref(X())
  85. x = self.interpret(f, [])
  86. assert lltype.typeOf(x) == self.OBJECTTYPE
  87. def test_rtype_2(self):
  88. def f():
  89. x1 = X()
  90. vref = virtual_ref(x1)
  91. x2 = vref()
  92. virtual_ref_finish(vref, x2)
  93. return x2
  94. x = self.interpret(f, [])
  95. assert self.castable(self.OBJECTTYPE, x)
  96. def test_rtype_3(self):
  97. def f(n):
  98. if n > 0:
  99. return virtual_ref(Y())
  100. else:
  101. return non_virtual_ref(Z())
  102. x = self.interpret(f, [-5])
  103. assert lltype.typeOf(x) == self.OBJECTTYPE
  104. def test_rtype_4(self):
  105. def f(n):
  106. if n > 0:
  107. return virtual_ref(X())
  108. else:
  109. return vref_None
  110. x = self.interpret(f, [-5])
  111. assert lltype.typeOf(x) == self.OBJECTTYPE
  112. assert not x
  113. def test_rtype_5(self):
  114. def f():
  115. vref = virtual_ref(X())
  116. try:
  117. vref()
  118. return 42
  119. except InvalidVirtualRef:
  120. return -1
  121. x = self.interpret(f, [])
  122. assert x == 42
  123. def test_rtype_virtualattr(self):
  124. def f():
  125. vref = virtual_ref(X())
  126. return vref.virtual
  127. x = self.interpret(f, [])
  128. assert x is False