blob: 51b439f4804e65af0e44c30872aab23b16224a4e [file] [log] [blame]
Antoine Pitrou8e605772011-04-25 21:21:07 +02001#include <Python.h>
2#include <stdio.h>
3
4void print_subinterp(void)
5{
6 /* Just output some debug stuff */
7 PyThreadState *ts = PyThreadState_Get();
8 printf("interp %p, thread state %p: ", ts->interp, ts);
9 fflush(stdout);
10 PyRun_SimpleString(
11 "import sys;"
12 "print('id(modules) =', id(sys.modules));"
13 "sys.stdout.flush()"
14 );
15}
16
17int main(int argc, char *argv[])
18{
19 PyThreadState *mainstate, *substate;
Stefan Krahf2c6db52011-05-26 00:37:45 +020020#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020021 PyGILState_STATE gilstate;
Stefan Krahf2c6db52011-05-26 00:37:45 +020022#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020023 int i, j;
24
25 for (i=0; i<3; i++) {
26 printf("--- Pass %d ---\n", i);
27 /* HACK: the "./" at front avoids a search along the PATH in
28 Modules/getpath.c */
29 Py_SetProgramName(L"./_testembed");
30 Py_Initialize();
31 mainstate = PyThreadState_Get();
32
Stefan Krahf2c6db52011-05-26 00:37:45 +020033#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020034 PyEval_InitThreads();
35 PyEval_ReleaseThread(mainstate);
36
37 gilstate = PyGILState_Ensure();
Stefan Krahf2c6db52011-05-26 00:37:45 +020038#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020039 print_subinterp();
40 PyThreadState_Swap(NULL);
41
42 for (j=0; j<3; j++) {
43 substate = Py_NewInterpreter();
44 print_subinterp();
45 Py_EndInterpreter(substate);
46 }
47
48 PyThreadState_Swap(mainstate);
49 print_subinterp();
Stefan Krahf2c6db52011-05-26 00:37:45 +020050#ifdef WITH_THREAD
Antoine Pitrou8e605772011-04-25 21:21:07 +020051 PyGILState_Release(gilstate);
Stefan Krahf2c6db52011-05-26 00:37:45 +020052#endif
Antoine Pitrou8e605772011-04-25 21:21:07 +020053
54 PyEval_RestoreThread(mainstate);
55 Py_Finalize();
56 }
57 return 0;
58}