Debian Codegraph @

pypy/module/pypyjit/test_pypy_c/test_import.py

raw symbols SCIP
1 import py
2 from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC
3
4 class TestImport(BaseTestPyPyC):
5
6 def test_import_in_function(self):
7 def main(n):
8 i = 0
9 while i < n:
10 from sys import version # ID: import
11 i += 1
12 return i
13 #
14 log = self.run(main, [500])
15 assert log.result == 500
16 loop, = log.loops_by_id('import')
17 assert loop.match_by_id('import', """
18 guard_not_invalidated(descr=...)
19 """)
20
21 def test_import_fast_path(self, tmpdir):
22 pkg = tmpdir.join('mypkg').ensure(dir=True)
23 pkg.join('__init__.py').write("")
24 pkg.join('mod.py').write(str(py.code.Source("""
25 def do_the_import():
26 import sys
27 """)))
28 def main(path, n):
29 import sys
30 sys.path.append(path)
31 from mypkg.mod import do_the_import
32 for i in range(n):
33 do_the_import()
34 #
35 log = self.run(main, [str(tmpdir), 300])
36 loop, = log.loops_by_filename(self.filepath)
37 # this is a check for a slow-down that introduced a
38 # call_may_force(absolute_import_with_lock).
39 for opname in log.opnames(loop.allops(opcode="IMPORT_NAME")):
40 assert 'call' not in opname # no call-like opcode
41
42 def test_import_fast_path_package(self, tmpdir):
43 print tmpdir
44 pkg = tmpdir.join('mypkg').ensure(dir=True)
45 subdir = pkg.join("sub").ensure(dir=True)
46 pkg.join('__init__.py').write("")
47 subdir.join('__init__.py').write("")
48 subdir.join('mod.py').write(str(py.code.Source("""
49 def do_the_import():
50 import sys
51 """)))
52 def main(path, n):
53 def do_the_import():
54 from mypkg.sub import mod
55 import sys
56 sys.path.append(path)
57 for i in range(n):
58 do_the_import()
59 #
60 log = self.run(main, [str(tmpdir), 300])
61 loop, = log.loops_by_filename(self.filepath)
62 # check that no string compares and other calls are there
63 for opname in log.opnames(loop.allops(opcode="IMPORT_NAME")):
64 assert 'call' not in opname # no call-like opcode
65
66 def test___import___has_fast_path(self):
67 def main(n):
68 i = 0
69 while i < n:
70 __import__('sys') # ID: import
71 i += 1
72 return i
73 log = self.run(main, [500])
74 assert log.result == 500
75 loop, = log.loops_by_id('import')
76 assert loop.match_by_id('import', """
77 guard_not_invalidated(descr=...)
78 """)