Debian Codegraph @

pypy/module/_hpy_universal/test/_vendored/test_importing.py

raw symbols SCIP
1 import sys
2 import pytest
3 from .support import HPyTest
4
5 # PyPy tests are always run with python2
6 ISPYPY = sys.version_info[0] < 3
7
8 if not ISPYPY:
9 @pytest.fixture(params=['cpython', 'universal', 'hybrid', 'debug'])
10 def hpy_abi(request):
11 abi = request.param
12 yield abi
13
14
15 class TestImporting(HPyTest):
16
17 def full_import(self, name, mod_filename):
18 import importlib
19 import sys
20 import os
21 if name in sys.modules:
22 raise ValueError(
23 "Test module {!r} already present in sys.modules".format(name))
24 importlib.invalidate_caches()
25 mod_dir = os.path.dirname(mod_filename)
26 sys.path.insert(0, mod_dir)
27 try:
28 module = importlib.import_module(name)
29 assert sys.modules[name] is module
30 finally:
31 # assert that the module import didn't change the sys.path entry
32 # that was added above, then remove the entry.
33 assert sys.path[0] == mod_dir
34 del sys.path[0]
35 if name in sys.modules:
36 del sys.modules[name]
37 return module
38
39 def test_importing_attributes(self):
40 import pytest
41 if not self.supports_ordinary_make_module_imports():
42 pytest.skip()
43 from hpy.devel.abitag import get_hpy_ext_suffix
44 mod = self.make_module("""
45 @INIT
46 """, name='mytest')
47 mod = self.full_import(mod.__name__, mod.__file__)
48 assert mod.__name__ == 'mytest'
49 assert mod.__package__ == ''
50 assert mod.__doc__ == 'some test for hpy'
51 assert mod.__loader__.name == 'mytest'
52 assert mod.__spec__.loader is mod.__loader__
53 assert mod.__spec__.name == 'mytest'
54 assert mod.__file__
55
56 hpy_abi = self.compiler.hpy_abi
57 if hpy_abi == 'debug':
58 hpy_abi = 'universal'
59 ext_suffix = get_hpy_ext_suffix(hpy_abi)
60 assert repr(mod) == '<module \'mytest\' from {}>'.format(
61 repr(str(self.compiler.tmpdir.join('mytest' + ext_suffix))))