Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa61505

Browse files
committed
Use a counter instead of a Boolean to check for initialized; n calls
to Py_Initialize will be undone by n calls to Py_Uninitialize.
1 parent 558be28 commit aa61505

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

Demo/pysvr/pysvr.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ main_thread(int port)
168168
PyEval_AcquireThread(gtstate);
169169
gtstate = NULL;
170170
Py_Finalize();
171+
Py_Finalize();
171172
}
172173
exit(0);
173174
}
@@ -213,6 +214,7 @@ init_python()
213214
if (gtstate)
214215
return;
215216
Py_Initialize(); /* Initialize the interpreter */
217+
Py_Initialize(); /* Initialize the interpreter */
216218
PyEval_InitThreads(); /* Create (and acquire) the interpreter lock */
217219
gtstate = PyEval_SaveThread(); /* Release the thread state */
218220
}

Python/pythonrun.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ Py_Initialize()
9898
PyObject *bimod, *sysmod;
9999
char *p;
100100

101-
if (initialized)
102-
Py_FatalError("Py_Initialize: already initialized");
103-
initialized = 1;
101+
if (++initialized > 1)
102+
return;
104103

105104
if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
106105
Py_DebugFlag = 1;
@@ -166,9 +165,10 @@ Py_Finalize()
166165

167166
call_sys_exitfunc();
168167

169-
if (!initialized)
168+
if (--initialized > 0)
169+
return;
170+
if (initialized < 0)
170171
Py_FatalError("Py_Finalize: not initialized");
171-
initialized = 0;
172172

173173
tstate = PyThreadState_Get();
174174
interp = tstate->interp;

0 commit comments

Comments
 (0)