Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 1 | #include <Python.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | void 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 | |
| 17 | int main(int argc, char *argv[]) |
| 18 | { |
| 19 | PyThreadState *mainstate, *substate; |
Victor Stinner | 66299a4 | 2011-04-26 23:37:02 +0200 | [diff] [blame] | 20 | #ifdef WITH_THREAD |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 21 | PyGILState_STATE gilstate; |
Victor Stinner | 66299a4 | 2011-04-26 23:37:02 +0200 | [diff] [blame] | 22 | #endif |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 23 | 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 | |
Victor Stinner | 66299a4 | 2011-04-26 23:37:02 +0200 | [diff] [blame] | 33 | #ifdef WITH_THREAD |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 34 | PyEval_InitThreads(); |
| 35 | PyEval_ReleaseThread(mainstate); |
| 36 | |
| 37 | gilstate = PyGILState_Ensure(); |
Victor Stinner | 66299a4 | 2011-04-26 23:37:02 +0200 | [diff] [blame] | 38 | #endif |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 39 | 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(); |
Victor Stinner | 66299a4 | 2011-04-26 23:37:02 +0200 | [diff] [blame] | 50 | #ifdef WITH_THREAD |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 51 | PyGILState_Release(gilstate); |
Victor Stinner | 66299a4 | 2011-04-26 23:37:02 +0200 | [diff] [blame] | 52 | #endif |
Antoine Pitrou | 8e60577 | 2011-04-25 21:21:07 +0200 | [diff] [blame] | 53 | |
| 54 | PyEval_RestoreThread(mainstate); |
| 55 | Py_Finalize(); |
| 56 | } |
| 57 | return 0; |
| 58 | } |