_testembed.c 1.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <Python.h>
#include <stdio.h>

void print_subinterp(void)
{
    /* Just output some debug stuff */
    PyThreadState *ts = PyThreadState_Get();
    printf("interp %p, thread state %p: ", ts->interp, ts);
    fflush(stdout);
    PyRun_SimpleString(
        "import sys;"
        "print('id(modules) =', id(sys.modules));"
        "sys.stdout.flush()"
    );
}

int main(int argc, char *argv[])
{
    PyThreadState *mainstate, *substate;
20
#ifdef WITH_THREAD
21
    PyGILState_STATE gilstate;
22
#endif
23 24 25 26 27 28 29 30 31 32
    int i, j;

    for (i=0; i<3; i++) {
        printf("--- Pass %d ---\n", i);
        /* HACK: the "./" at front avoids a search along the PATH in
           Modules/getpath.c */
        Py_SetProgramName(L"./_testembed");
        Py_Initialize();
        mainstate = PyThreadState_Get();

33
#ifdef WITH_THREAD
34 35 36 37
        PyEval_InitThreads();
        PyEval_ReleaseThread(mainstate);

        gilstate = PyGILState_Ensure();
38
#endif
39 40 41 42 43 44 45 46 47 48 49
        print_subinterp();
        PyThreadState_Swap(NULL);

        for (j=0; j<3; j++) {
            substate = Py_NewInterpreter();
            print_subinterp();
            Py_EndInterpreter(substate);
        }

        PyThreadState_Swap(mainstate);
        print_subinterp();
50
#ifdef WITH_THREAD
51
        PyGILState_Release(gilstate);
52
#endif
53 54 55 56 57 58

        PyEval_RestoreThread(mainstate);
        Py_Finalize();
    }
    return 0;
}