Commit e10bd48a authored by Stefan Behnel's avatar Stefan Behnel

Automatically add stubs for "cython.cimports.*" in the test runner to make...

Automatically add stubs for "cython.cimports.*" in the test runner to make them importable in Python (although not necessarily runnable).
parent 48325b43
from cython.cimports.libc import math
def use_libc_math():
return math.ceil(5.5)
......@@ -218,11 +218,7 @@ libraries become available to Python code. It only means that you can
tell Cython what cimports you want to use, without requiring special
syntax. Running such code in plain Python will fail.
::
from cython.cimports.libc import math
print(math.ceil(5.5))
.. literalinclude:: ../../examples/tutorial/pure/py_cimport.py
Since such code must necessarily refer to the non-existing
``cython.cimports`` 'package', the plain cimport form
......
......@@ -980,6 +980,15 @@ class CythonCompileTestCase(unittest.TestCase):
if self.workdir not in sys.path:
sys.path.insert(0, self.workdir)
if self.add_cython_import:
with open(self.module_path, 'rb') as f:
source = f.read()
if b'cython.cimports.' in source:
from Cython.Shadow import CythonCImports
for name in set(re.findall(br"(cython\.cimports(?:\.\w+)+)", source)):
name = name.decode()
sys.modules[name] = CythonCImports(name)
def tearDown(self):
from Cython.Compiler import Options
for name, value in self._saved_options:
......@@ -995,6 +1004,13 @@ class CythonCompileTestCase(unittest.TestCase):
del sys.modules[self.module]
except KeyError:
pass
# remove any stubs of cimported modules in pure Python mode
if self.add_cython_import:
for name in list(sys.modules):
if name.startswith('cython.cimports.'):
del sys.modules[name]
cleanup = self.cleanup_failures or self.success
cleanup_c_files = WITH_CYTHON and self.cleanup_workdir and cleanup
cleanup_lib_files = self.cleanup_sharedlibs and cleanup
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment