Commit 4caf76b4 authored by Stefan Behnel's avatar Stefan Behnel

Add test for Cython module usage in subinterpreters. In Py3.5, we should now...

Add test for Cython module usage in subinterpreters. In Py3.5, we should now raise an exception when trying to re-import a module into a different subinterpreter.
parent 4ce75427
# mode: run
# tag: pep489, subinterpreter
PYTHON setup.py build_ext --inplace
PYTHON -c 'import subtest; subtest.run_main()'
PYTHON -c 'import subtest; subtest.run_sub()'
PYTHON -c 'import subtest; subtest.run_main(); subtest.run_sub()'
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
setup(
ext_modules = cythonize("**/*.pyx"),
)
######## subtest.pyx ########
cdef extern from *:
"""
/* Copied from CPython's _testcapi.c module */
static PyObject *run_in_subinterpreter(const char *code) {
int r;
PyThreadState *substate, *mainstate;
mainstate = PyThreadState_Get();
PyThreadState_Swap(NULL);
substate = Py_NewInterpreter();
if (substate == NULL) {
/* Since no new thread state was created, there is no exception to
propagate; raise a fresh one after swapping in the old thread
state. */
PyThreadState_Swap(mainstate);
PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
return NULL;
}
r = PyRun_SimpleString(code);
Py_EndInterpreter(substate);
PyThreadState_Swap(mainstate);
return PyLong_FromLong(r);
}
"""
object run_in_subinterpreter(const char *code)
MAIN_HAS_IMPORTED = False
def run_main():
global MAIN_HAS_IMPORTED
MAIN_HAS_IMPORTED = True
import package.subtest
from package import subtest
def run_sub():
assert 0 == run_in_subinterpreter(b'1+1')
assert 0 == run_in_subinterpreter(b'2+2')
assert 0 == run_in_subinterpreter(b'import package')
assert 0 == run_in_subinterpreter(b'import package')
import sys
result = run_in_subinterpreter(b'import package.subtest')
if not MAIN_HAS_IMPORTED:
assert result == 0, result # imports only in subinterpreters are ok
elif sys.version_info >= (3, 5):
assert result == -1, result # re-import in a different subinterpreter fails in Py3.5+ (with PEP-489)
else:
assert result == 0, result # re-import in a different subinterpreter reuses the module in Py<3.5
######## package/__init__.py ########
######## package/subtest.pyx ########
print("Module loaded: %s" % __name__)
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