2013/01/31 Thu 5:56:41 UTC+9 Bram Moolenaar wrote:

> That doesn't look right, it would access pygilstate before setting it.

Hmm, but the same can be said of if_python.c.  pygilstate would be
accessed in Python_SaveThread() because PY_CAN_RECURSE is normally defined.

Using PyEval_SaveThread() to release the GIL seems good.
I checked the attached patch with Python 2.7 and 3.3 on Win7.
Both background threads and system trace seem to work well.

Thanks,
Ken Takata

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


# HG changeset patch
# Parent 8c5f1372df3ad08f0a049a50dc152d3978eb141a

diff --git a/src/if_python.c b/src/if_python.c
--- a/src/if_python.c
+++ b/src/if_python.c
@@ -741,7 +741,7 @@
 	PyMac_Initialize();
 #endif
 	/* Initialise threads, and below save the state using
-	 * PyGILState_Ensure.  Without the call to PyGILState_Ensure, thread
+	 * PyEval_SaveThread.  Without the call to PyEval_SaveThread, thread
 	 * specific state (such as the system trace hook), will be lost
 	 * between invocations of Python code. */
 	PyEval_InitThreads();
@@ -755,10 +755,6 @@
 	if (PythonMod_Init())
 	    goto fail;
 
-	/* The first python thread is vim's, release the lock. */
-	Python_SaveThread();
-	pygilstate = PyGILState_Ensure();
-
 	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
 
 	/* Remove the element from sys.path that was added because of our
@@ -767,7 +763,14 @@
 	 * the current directory in sys.path. */
 	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
 
-	PyGILState_Release(pygilstate);
+	/* lock is created and acquired in PyEval_InitThreads() and thread
+	 * state is created in Py_Initialize()
+	 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
+	 * (python must have threads enabled!)
+	 * so the following does both: unlock GIL and save thread state in TLS
+	 * without deleting thread state
+	 */
+	PyEval_SaveThread();
 
 	initialised = 1;
     }
diff --git a/src/if_python3.c b/src/if_python3.c
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -729,13 +729,11 @@
 #else
 	PyMac_Initialize();
 #endif
-	/* Initialise threads, and save the state using PyGILState_Ensure.
-	 * Without the call to PyGILState_Ensure, thread specific state (such
-	 * as the system trace hook), will be lost between invocations of
-	 * Python code. */
+	/* Initialise threads, and below save the state using
+	 * PyEval_SaveThread.  Without the call to PyEval_SaveThread, thread
+	 * specific state (such as the system trace hook), will be lost
+	 * between invocations of Python code. */
 	PyEval_InitThreads();
-	pygilstate = PyGILState_Ensure();
-
 #ifdef DYNAMIC_PYTHON3
 	get_py3_exceptions();
 #endif
@@ -754,13 +752,14 @@
 	 */
 	PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
 
-	// lock is created and acquired in PyEval_InitThreads() and thread
-	// state is created in Py_Initialize()
-	// there _PyGILState_NoteThreadState() also sets gilcounter to 1
-	// (python must have threads enabled!)
-	// so the following does both: unlock GIL and save thread state in TLS
-	// without deleting thread state
-	PyGILState_Release(pygilstate);
+	/* lock is created and acquired in PyEval_InitThreads() and thread
+	 * state is created in Py_Initialize()
+	 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
+	 * (python must have threads enabled!)
+	 * so the following does both: unlock GIL and save thread state in TLS
+	 * without deleting thread state
+	 */
+	PyEval_SaveThread();
 
 	py3initialised = 1;
     }

Raspunde prin e-mail lui