From 76d711c3b5397b749a67d229150d3c1ff3f33add Mon Sep 17 00:00:00 2001 From: Bram Moolenaar Date: Wed, 13 Feb 2013 14:17:08 +0100 Subject: updated for version 7.3.808 Problem: Python threads still do not work properly. Solution: Fix both Python 2 and 3. Add tests. (Ken Takata) --- src/if_python.c | 15 +++++++++------ src/if_python3.c | 25 ++++++++++++------------- src/testdir/test86.in | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/testdir/test86.ok | 2 ++ src/testdir/test87.in | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/testdir/test87.ok | 2 ++ src/version.c | 2 ++ 7 files changed, 123 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/if_python.c b/src/if_python.c index 1bf737fc4..d0314e2f5 100644 --- a/src/if_python.c +++ b/src/if_python.c @@ -741,7 +741,7 @@ Python_Init(void) 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 @@ Python_Init(void) 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 @@ Python_Init(void) * 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 index 4067517ac..5311483dd 100644 --- a/src/if_python3.c +++ b/src/if_python3.c @@ -729,13 +729,11 @@ Python3_Init(void) #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 @@ Python3_Init(void) */ 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; } diff --git a/src/testdir/test86.in b/src/testdir/test86.in index 1309643d8..71635bda2 100644 --- a/src/testdir/test86.in +++ b/src/testdir/test86.in @@ -267,6 +267,54 @@ EOF : $put =toput : endtry :endfor +:" +:" threading +:let l = [0] +:py l=vim.bindeval('l') +:py < 8 # check if the background thread is working +:$put =string(l) +:" +:" settrace +:let l = [] +:py l=vim.bindeval('l') +:py < 8 # check if the background thread is working +:$put =string(l) +:" +:" settrace +:let l = [] +:py3 l=vim.bindeval('l') +:py3 <