PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/rlib/test/test_objectmodel.py

https://github.com/woodrow/pyoac
Python | 319 lines | 264 code | 51 blank | 4 comment | 25 complexity | bf130e344f1d80285203997053b9fcb1 MD5 | raw file
  1. import py
  2. from pypy.rlib.objectmodel import *
  3. from pypy.translator.translator import TranslationContext, graphof
  4. from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
  5. from pypy.conftest import option
  6. def strange_key_eq(key1, key2):
  7. return key1[0] == key2[0] # only the 1st character is relevant
  8. def strange_key_hash(key):
  9. return ord(key[0])
  10. def play_with_r_dict(d):
  11. d['hello'] = 41
  12. d['hello'] = 42
  13. assert d['hi there'] == 42
  14. try:
  15. unexpected = d["dumb"]
  16. except KeyError:
  17. pass
  18. else:
  19. assert False, "should have raised, got %s" % unexpected
  20. assert len(d) == 1
  21. assert 'oops' not in d
  22. count = 0
  23. for x in d:
  24. assert x == 'hello'
  25. count += 1
  26. assert count == 1
  27. assert d.get('hola', -1) == 42
  28. assert d.get('salut', -1) == -1
  29. d1 = d.copy()
  30. del d['hu!']
  31. assert len(d) == 0
  32. assert d1.keys() == ['hello']
  33. d.update(d1)
  34. assert d.values() == [42]
  35. lst = d.items()
  36. assert len(lst) == 1 and len(lst[0]) == 2
  37. assert lst[0][0] == 'hello' and lst[0][1] == 42
  38. count = 0
  39. for x in d.iterkeys():
  40. assert x == 'hello'
  41. count += 1
  42. assert count == 1
  43. count = 0
  44. for x in d.itervalues():
  45. assert x == 42
  46. count += 1
  47. assert count == 1
  48. count = 0
  49. for x in d.iteritems():
  50. assert len(x) == 2 and x[0] == 'hello' and x[1] == 42
  51. count += 1
  52. assert count == 1
  53. d.clear()
  54. assert d.keys() == []
  55. return True # for the tests below
  56. def test_recursive_r_dict_repr():
  57. import operator
  58. rdic = r_dict(operator.eq, hash)
  59. rdic['x'] = rdic
  60. assert str(rdic) == "r_dict({'x': r_dict({...})})"
  61. assert repr(rdic)== "r_dict({'x': r_dict({...})})"
  62. def test_r_dict():
  63. # NB. this test function is also annotated/rtyped by the next tests
  64. d = r_dict(strange_key_eq, strange_key_hash)
  65. return play_with_r_dict(d)
  66. class Strange:
  67. def key_eq(strange, key1, key2):
  68. return key1[0] == key2[0] # only the 1st character is relevant
  69. def key_hash(strange, key):
  70. return ord(key[0])
  71. def test_r_dict_bm():
  72. # NB. this test function is also annotated by the next tests
  73. strange = Strange()
  74. d = r_dict(strange.key_eq, strange.key_hash)
  75. return play_with_r_dict(d)
  76. def test_annotate_r_dict():
  77. t = TranslationContext()
  78. a = t.buildannotator()
  79. a.build_types(test_r_dict, [])
  80. #t.view()
  81. graph = graphof(t, strange_key_eq)
  82. assert a.binding(graph.getargs()[0]).knowntype == str
  83. assert a.binding(graph.getargs()[1]).knowntype == str
  84. graph = graphof(t, strange_key_hash)
  85. assert a.binding(graph.getargs()[0]).knowntype == str
  86. def test_annotate_r_dict_bm():
  87. t = TranslationContext()
  88. a = t.buildannotator()
  89. a.build_types(test_r_dict_bm, [])
  90. #t.view()
  91. strange_key_eq = Strange.key_eq.im_func
  92. strange_key_hash = Strange.key_hash.im_func
  93. Strange_def = a.bookkeeper.getuniqueclassdef(Strange)
  94. graph = graphof(t, strange_key_eq)
  95. assert a.binding(graph.getargs()[0]).knowntype == Strange_def
  96. assert a.binding(graph.getargs()[1]).knowntype == str
  97. assert a.binding(graph.getargs()[2]).knowntype == str
  98. graph = graphof(t, strange_key_hash)
  99. assert a.binding(graph.getargs()[0]).knowntype == Strange_def
  100. assert a.binding(graph.getargs()[1]).knowntype == str
  101. def test_unboxed_value():
  102. class Base(object):
  103. __slots__ = ()
  104. class C(Base, UnboxedValue):
  105. __slots__ = 'smallint'
  106. assert C(17).smallint == 17
  107. assert C(17).getvalue() == 17
  108. class A(UnboxedValue):
  109. __slots__ = ['value']
  110. assert A(12098).value == 12098
  111. assert A(12098).getvalue() == 12098
  112. def test_symbolic():
  113. py.test.skip("xxx no test here")
  114. def test_symbolic_raises():
  115. s1 = Symbolic()
  116. s2 = Symbolic()
  117. py.test.raises(TypeError, "s1 < s2")
  118. py.test.raises(TypeError, "hash(s1)")
  119. class BaseTestObjectModel(BaseRtypingTest):
  120. def test_we_are_translated(self):
  121. assert we_are_translated() == False
  122. def fn():
  123. return we_are_translated()
  124. res = self.interpret(fn, [])
  125. assert res is True
  126. def test_rtype_r_dict(self):
  127. res = self.interpret(test_r_dict, [])
  128. assert res is True
  129. def test_rtype_r_dict_bm(self):
  130. res = self.interpret(test_r_dict_bm, [])
  131. assert res is True
  132. def test_rtype_constant_r_dicts(self):
  133. d1 = r_dict(strange_key_eq, strange_key_hash)
  134. d1['hello'] = 666
  135. d2 = r_dict(strange_key_eq, strange_key_hash)
  136. d2['hello'] = 777
  137. d2['world'] = 888
  138. def fn(i):
  139. if i == 1:
  140. d = d1
  141. else:
  142. d = d2
  143. return len(d)
  144. res = self.interpret(fn, [1])
  145. assert res == 1
  146. res = self.interpret(fn, [2])
  147. assert res == 2
  148. def test_rtype_r_dict_singlefrozen_func(self):
  149. class FreezingClass(Strange):
  150. def _freeze_(self):
  151. return True
  152. obj = FreezingClass()
  153. def fn():
  154. d = r_dict(obj.key_eq, obj.key_hash)
  155. return play_with_r_dict(d)
  156. assert self.interpret(fn, []) is True
  157. def test_rtype_r_dict_singlefrozen_func_pbc(self):
  158. class FreezingClass(Strange):
  159. def _freeze_(self):
  160. return True
  161. obj = FreezingClass()
  162. pbc_d = r_dict(obj.key_eq, obj.key_hash)
  163. def fn():
  164. return play_with_r_dict(pbc_d)
  165. assert self.interpret(fn, []) is True
  166. def test_rtype_r_dict_exceptions(self):
  167. def raising_hash(obj):
  168. if obj.startswith("bla"):
  169. raise TypeError
  170. return 1
  171. def eq(obj1, obj2):
  172. return obj1 is obj2
  173. def f():
  174. d1 = r_dict(eq, raising_hash)
  175. d1['xxx'] = 1
  176. try:
  177. x = d1["blabla"]
  178. except Exception:
  179. return 42
  180. return x
  181. res = self.interpret(f, [])
  182. assert res == 42
  183. def f():
  184. d1 = r_dict(eq, raising_hash)
  185. d1['xxx'] = 1
  186. try:
  187. x = d1["blabla"]
  188. except TypeError:
  189. return 42
  190. return x
  191. res = self.interpret(f, [])
  192. assert res == 42
  193. def f():
  194. d1 = r_dict(eq, raising_hash)
  195. d1['xxx'] = 1
  196. try:
  197. d1["blabla"] = 2
  198. except TypeError:
  199. return 42
  200. return 0
  201. res = self.interpret(f, [])
  202. assert res == 42
  203. def test_access_in_try(self):
  204. h = lambda x: 1
  205. eq = lambda x,y: x==y
  206. def f(d):
  207. try:
  208. return d[2]
  209. except ZeroDivisionError:
  210. return 42
  211. return -1
  212. def g(n):
  213. d = r_dict(eq, h)
  214. d[1] = n
  215. d[2] = 2*n
  216. return f(d)
  217. res = self.interpret(g, [3])
  218. assert res == 6
  219. def test_access_in_try_set(self):
  220. h = lambda x: 1
  221. eq = lambda x,y: x==y
  222. def f(d):
  223. try:
  224. d[2] = 77
  225. except ZeroDivisionError:
  226. return 42
  227. return -1
  228. def g(n):
  229. d = r_dict(eq, h)
  230. d[1] = n
  231. f(d)
  232. return d[2]
  233. res = self.interpret(g, [3])
  234. assert res == 77
  235. class TestLLtype(BaseTestObjectModel, LLRtypeMixin):
  236. def test_rtype_keepalive(self):
  237. from pypy.rlib import objectmodel
  238. def f():
  239. x = [1]
  240. y = ['b']
  241. objectmodel.keepalive_until_here(x,y)
  242. return 1
  243. res = self.interpret(f, [])
  244. assert res == 1
  245. class TestOOtype(BaseTestObjectModel, OORtypeMixin):
  246. pass
  247. def test_specialize_decorator():
  248. def f():
  249. pass
  250. specialize.memo()(f)
  251. assert f._annspecialcase_ == 'specialize:memo'
  252. specialize.arg(0)(f)
  253. assert f._annspecialcase_ == 'specialize:arg(0)'
  254. specialize.arg(1)(f)
  255. assert f._annspecialcase_ == 'specialize:arg(1)'
  256. def getgraph(f, argtypes):
  257. from pypy.translator.translator import TranslationContext, graphof
  258. from pypy.translator.backendopt.all import backend_optimizations
  259. t = TranslationContext()
  260. a = t.buildannotator()
  261. typer = t.buildrtyper()
  262. a.build_types(f, argtypes)
  263. typer.specialize()
  264. backend_optimizations(t)
  265. graph = graphof(t, f)
  266. if option.view:
  267. graph.show()
  268. return graph