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