PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/translator/test/test_unsimplify.py

https://github.com/thepian/pypy
Python | 90 lines | 84 code | 5 blank | 1 comment | 13 complexity | 80871d7f4f5dd574dfbf29d33541715b MD5 | raw file
  1. import os
  2. from pypy.translator.translator import TranslationContext, graphof
  3. from pypy.translator.unsimplify import split_block, call_final_function
  4. from pypy.rpython.llinterp import LLInterpreter
  5. from pypy.objspace.flow.model import checkgraph
  6. from pypy.rlib.objectmodel import we_are_translated
  7. from pypy.tool.udir import udir
  8. def translate(func, argtypes, type_system="lltype"):
  9. t = TranslationContext()
  10. t.buildannotator().build_types(func, argtypes)
  11. t.entry_point_graph = graphof(t, func)
  12. t.buildrtyper(type_system=type_system).specialize()
  13. return graphof(t, func), t
  14. def test_split_blocks_simple():
  15. for i in range(4):
  16. def f(x, y):
  17. z = x + y
  18. w = x * y
  19. return z + w
  20. graph, t = translate(f, [int, int])
  21. split_block(t.annotator, graph.startblock, i)
  22. checkgraph(graph)
  23. interp = LLInterpreter(t.rtyper)
  24. result = interp.eval_graph(graph, [1, 2])
  25. assert result == 5
  26. def test_split_blocks_conditional():
  27. for i in range(3):
  28. def f(x, y):
  29. if x + 12:
  30. return y + 1
  31. else:
  32. return y + 2
  33. graph, t = translate(f, [int, int])
  34. split_block(t.annotator, graph.startblock, i)
  35. checkgraph(graph)
  36. interp = LLInterpreter(t.rtyper)
  37. result = interp.eval_graph(graph, [-12, 2])
  38. assert result == 4
  39. result = interp.eval_graph(graph, [0, 2])
  40. assert result == 3
  41. def test_split_block_exceptions():
  42. for i in range(2):
  43. def raises(x):
  44. if x == 1:
  45. raise ValueError
  46. elif x == 2:
  47. raise KeyError
  48. return x
  49. def catches(x):
  50. try:
  51. y = x + 1
  52. raises(y)
  53. except ValueError:
  54. return 0
  55. except KeyError:
  56. return 1
  57. return x
  58. graph, t = translate(catches, [int])
  59. split_block(t.annotator, graph.startblock, i)
  60. checkgraph(graph)
  61. interp = LLInterpreter(t.rtyper)
  62. result = interp.eval_graph(graph, [0])
  63. assert result == 0
  64. result = interp.eval_graph(graph, [1])
  65. assert result == 1
  66. result = interp.eval_graph(graph, [2])
  67. assert result == 2
  68. def test_call_final_function():
  69. tmpfile = str(udir.join('test_call_final_function'))
  70. for type_system in ['lltype', 'ootype']:
  71. def f(x):
  72. return x * 6
  73. def goodbye_world():
  74. if we_are_translated():
  75. fd = os.open(tmpfile, os.O_WRONLY | os.O_CREAT, 0)
  76. os.close(fd)
  77. graph, t = translate(f, [int], type_system)
  78. call_final_function(t, goodbye_world)
  79. #
  80. if os.path.exists(tmpfile):
  81. os.unlink(tmpfile)
  82. interp = LLInterpreter(t.rtyper)
  83. result = interp.eval_graph(graph, [7])
  84. assert result == 42
  85. assert os.path.isfile(tmpfile)