cython

Форк
0
/
reimport_from_subinterpreter.srctree 
79 строк · 2.3 Кб
1
# mode: run
2
# tag: pep489, subinterpreter
3

4
PYTHON setup.py build_ext --inplace
5
PYTHON -c "import subtest; subtest.run_main()"
6
PYTHON -c "import subtest; subtest.run_sub()"
7
PYTHON -c "import subtest; subtest.run_main(); subtest.run_sub()"
8

9
######## setup.py ########
10

11
from Cython.Build.Dependencies import cythonize
12
from distutils.core import setup
13

14
setup(
15
    ext_modules = cythonize("**/*.pyx"),
16
    )
17

18
######## subtest.pyx ########
19

20
cdef extern from *:
21
    """
22
    /* Copied from CPython's _testcapi.c module */
23
    static PyObject *run_in_subinterpreter(const char *code) {
24
        int r;
25
        PyThreadState *substate, *mainstate;
26

27
        mainstate = PyThreadState_Get();
28

29
        PyThreadState_Swap(NULL);
30

31
        substate = Py_NewInterpreter();
32
        if (substate == NULL) {
33
            /* Since no new thread state was created, there is no exception to
34
               propagate; raise a fresh one after swapping in the old thread
35
               state. */
36
            PyThreadState_Swap(mainstate);
37
            PyErr_SetString(PyExc_RuntimeError, "sub-interpreter creation failed");
38
            return NULL;
39
        }
40
        r = PyRun_SimpleString(code);
41
        Py_EndInterpreter(substate);
42

43
        PyThreadState_Swap(mainstate);
44
        return PyLong_FromLong(r);
45
    }
46
    """
47
    object run_in_subinterpreter(const char *code)
48

49

50
MAIN_HAS_IMPORTED = False
51

52
def run_main():
53
    global MAIN_HAS_IMPORTED
54
    MAIN_HAS_IMPORTED = True
55
    import package.subtest
56
    from package import subtest
57

58
def run_sub():
59
    assert 0 == run_in_subinterpreter(b'1+1')
60
    assert 0 == run_in_subinterpreter(b'2+2')
61

62
    # The subinterpreter does not add the current working directory to
63
    # sys.path, so we need to add it manually.
64
    pre = b'import sys; sys.path.insert(0, "."); '
65
    assert 0 == run_in_subinterpreter(pre + b'import package')
66
    assert 0 == run_in_subinterpreter(pre + b'import package')
67

68
    result = run_in_subinterpreter(pre + b'import package.subtest')
69
    if not MAIN_HAS_IMPORTED:
70
        assert result == 0, result  # imports only in subinterpreters are ok
71
    else:
72
        assert result == -1, result  # re-import in a different subinterpreter fails in Py3.5+ (with PEP-489)
73

74

75
######## package/__init__.py ########
76

77
######## package/subtest.pyx ########
78

79
print("Module loaded: %s" % __name__)
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.