Debian Codegraph @

extra_tests/hpy_tests/_vendored/test_importing.py

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