Staging
v0.5.0
https://github.com/python/cpython
Raw File
Tip revision: 4cf9b2047d0438b69f66b0c2fd48e6d7576eb1f1 authored by Georg Brandl on 11 November 2013, 05:13:54 UTC
Bump to 3.3.3rc2.
Tip revision: 4cf9b20
_testembed.c
#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;
#ifdef WITH_THREAD
    PyGILState_STATE gilstate;
#endif
    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();

#ifdef WITH_THREAD
        PyEval_InitThreads();
        PyEval_ReleaseThread(mainstate);

        gilstate = PyGILState_Ensure();
#endif
        print_subinterp();
        PyThreadState_Swap(NULL);

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

        PyThreadState_Swap(mainstate);
        print_subinterp();
#ifdef WITH_THREAD
        PyGILState_Release(gilstate);
#endif

        PyEval_RestoreThread(mainstate);
        Py_Finalize();
    }
    return 0;
}
back to top