@@ -3556,24 +3556,11 @@ _PyPopen(char *cmdstring, int mode, int n, int bufsize)
35563556 * exit code as the result of the close() operation. This permits the
35573557 * files to be closed in any order - it is always the close() of the
35583558 * final handle that will return the exit code.
3559+ *
3560+ * NOTE: This function is currently called with the GIL released.
3561+ * hence we use the GILState API to manage our state.
35593562 */
35603563
3561- /* RED_FLAG 31-Aug-2000 Tim
3562- * This is always called (today!) between a pair of
3563- * Py_BEGIN_ALLOW_THREADS/ Py_END_ALLOW_THREADS
3564- * macros. So the thread running this has no valid thread state, as
3565- * far as Python is concerned. However, this calls some Python API
3566- * functions that cannot be called safely without a valid thread
3567- * state, in particular PyDict_GetItem.
3568- * As a temporary hack (although it may last for years ...), we
3569- * *rely* on not having a valid thread state in this function, in
3570- * order to create our own "from scratch".
3571- * This will deadlock if _PyPclose is ever called by a thread
3572- * holding the global lock.
3573- * (The OS/2 EMX thread support appears to cover the case where the
3574- * lock is already held - AIM Apr01)
3575- */
3576-
35773564static int _PyPclose (FILE * file )
35783565{
35793566 int result ;
@@ -3582,8 +3569,7 @@ static int _PyPclose(FILE *file)
35823569 PyObject * procObj , * pidObj , * intObj , * fileObj ;
35833570 int file_count ;
35843571#ifdef WITH_THREAD
3585- PyInterpreterState * pInterpreterState ;
3586- PyThreadState * pThreadState ;
3572+ PyGILState_STATE state ;
35873573#endif
35883574
35893575 /* Close the file handle first, to ensure it can't block the
@@ -3592,30 +3578,8 @@ static int _PyPclose(FILE *file)
35923578 result = fclose (file );
35933579
35943580#ifdef WITH_THREAD
3595- /* Bootstrap a valid thread state into existence. */
3596- pInterpreterState = PyInterpreterState_New ();
3597- if (!pInterpreterState ) {
3598- /* Well, we're hosed now! We don't have a thread
3599- * state, so can't call a nice error routine, or raise
3600- * an exception. Just die.
3601- */
3602- Py_FatalError ("unable to allocate interpreter state "
3603- "when closing popen object." );
3604- return -1 ; /* unreachable */
3605- }
3606- pThreadState = PyThreadState_New (pInterpreterState );
3607- if (!pThreadState ) {
3608- Py_FatalError ("unable to allocate thread state "
3609- "when closing popen object." );
3610- return -1 ; /* unreachable */
3611- }
3612- /* Grab the global lock. Note that this will deadlock if the
3613- * current thread already has the lock! (see RED_FLAG comments
3614- * before this function)
3615- */
3616- PyEval_RestoreThread (pThreadState );
3581+ state = PyGILState_Ensure ();
36173582#endif
3618-
36193583 if (_PyPopenProcs )
36203584 {
36213585 if ((fileObj = PyLong_FromVoidPtr (file )) != NULL &&
@@ -3678,17 +3642,8 @@ static int _PyPclose(FILE *file)
36783642 } /* if _PyPopenProcs */
36793643
36803644#ifdef WITH_THREAD
3681- /* Tear down the thread & interpreter states.
3682- * Note that interpreter state clear & delete functions automatically
3683- * call the thread clear & delete functions, and indeed insist on
3684- * doing that themselves. The lock must be held during the clear, but
3685- * need not be held during the delete.
3686- */
3687- PyInterpreterState_Clear (pInterpreterState );
3688- PyEval_ReleaseThread (pThreadState );
3689- PyInterpreterState_Delete (pInterpreterState );
3645+ PyGILState_Release (state );
36903646#endif
3691-
36923647 return result ;
36933648}
36943649
0 commit comments