To: vim_dev@googlegroups.com
Subject: Patch 7.3.808
Fcc: outbox
From: Bram Moolenaar <Bram@moolenaar.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
------------

Patch 7.3.808
Problem:    Python threads still do not work properly.
Solution:   Fix both Python 2 and 3.  Add tests. (Ken Takata)
Files:	    src/if_python.c, src/if_python3.c, src/testdir/test86.in,
	    src/testdir/test86.ok, src/testdir/test87.in,
	    src/testdir/test87.ok


*** ../vim-7.3.807/src/if_python.c	2013-01-30 11:44:33.000000000 +0100
--- src/if_python.c	2013-02-13 14:07:28.000000000 +0100
***************
*** 741,747 ****
  	PyMac_Initialize();
  #endif
  	/* Initialise threads, and below 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. */
  	PyEval_InitThreads();
--- 741,747 ----
  	PyMac_Initialize();
  #endif
  	/* 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();
***************
*** 755,764 ****
  	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
--- 755,760 ----
***************
*** 767,773 ****
  	 * the current directory in sys.path. */
  	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
  
! 	PyGILState_Release(pygilstate);
  
  	initialised = 1;
      }
--- 763,776 ----
  	 * the current directory in sys.path. */
  	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/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
! 	 */
! 	PyEval_SaveThread();
  
  	initialised = 1;
      }
*** ../vim-7.3.807/src/if_python3.c	2012-11-28 15:33:10.000000000 +0100
--- src/if_python3.c	2013-02-13 14:07:28.000000000 +0100
***************
*** 729,741 ****
  #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. */
  	PyEval_InitThreads();
- 	pygilstate = PyGILState_Ensure();
- 
  #ifdef DYNAMIC_PYTHON3
  	get_py3_exceptions();
  #endif
--- 729,739 ----
  #else
  	PyMac_Initialize();
  #endif
! 	/* 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();
  #ifdef DYNAMIC_PYTHON3
  	get_py3_exceptions();
  #endif
***************
*** 754,766 ****
  	 */
  	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);
  
  	py3initialised = 1;
      }
--- 752,765 ----
  	 */
  	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
! 	 */
! 	PyEval_SaveThread();
  
  	py3initialised = 1;
      }
*** ../vim-7.3.807/src/testdir/test86.in	2012-09-21 14:00:05.000000000 +0200
--- src/testdir/test86.in	2013-02-13 13:58:25.000000000 +0100
***************
*** 267,272 ****
--- 267,320 ----
  :      $put =toput
  :   endtry
  :endfor
+ :"
+ :" threading
+ :let l = [0]
+ :py l=vim.bindeval('l')
+ :py <<EOF
+ import threading
+ import time
+ 
+ class T(threading.Thread):
+     def __init__(self):
+         threading.Thread.__init__(self)
+         self.t = 0
+         self.running = True
+ 
+     def run(self):
+         while self.running:
+             self.t += 1
+             time.sleep(0.1)
+ 
+ t = T()
+ t.start()
+ EOF
+ :sleep 1
+ :py t.running = False
+ :py t.join()
+ :py l[0] = t.t > 8  # check if the background thread is working
+ :$put =string(l)
+ :"
+ :" settrace
+ :let l = []
+ :py l=vim.bindeval('l')
+ :py <<EOF
+ import sys
+ 
+ def traceit(frame, event, arg):
+     global l
+     if event == "line":
+         l.extend([frame.f_lineno])
+     return traceit
+ 
+ def trace_main():
+     for i in range(5):
+         pass
+ EOF
+ :py sys.settrace(traceit)
+ :py trace_main()
+ :py sys.settrace(None)
+ :$put =string(l)
  :endfun
  :"
  :call Test()
*** ../vim-7.3.807/src/testdir/test86.ok	2012-09-21 14:00:05.000000000 +0200
--- src/testdir/test86.ok	2013-02-13 13:58:25.000000000 +0100
***************
*** 63,65 ****
--- 63,67 ----
  {"\0": 1}:	Vim(let):E859:
  undefined_name:	Vim(let):E858:
  vim:	Vim(let):E859:
+ [1]
+ [1, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 1]
*** ../vim-7.3.807/src/testdir/test87.in	2012-09-21 14:00:05.000000000 +0200
--- src/testdir/test87.in	2013-02-13 13:58:25.000000000 +0100
***************
*** 267,272 ****
--- 267,320 ----
  :      $put =toput
  :   endtry
  :endfor
+ :"
+ :" threading
+ :let l = [0]
+ :py3 l=vim.bindeval('l')
+ :py3 <<EOF
+ import threading
+ import time
+ 
+ class T(threading.Thread):
+     def __init__(self):
+         threading.Thread.__init__(self)
+         self.t = 0
+         self.running = True
+ 
+     def run(self):
+         while self.running:
+             self.t += 1
+             time.sleep(0.1)
+ 
+ t = T()
+ t.start()
+ EOF
+ :sleep 1
+ :py3 t.running = False
+ :py3 t.join()
+ :py3 l[0] = t.t > 8  # check if the background thread is working
+ :$put =string(l)
+ :"
+ :" settrace
+ :let l = []
+ :py3 l=vim.bindeval('l')
+ :py3 <<EOF
+ import sys
+ 
+ def traceit(frame, event, arg):
+     global l
+     if event == "line":
+         l += [frame.f_lineno]
+     return traceit
+ 
+ def trace_main():
+     for i in range(5):
+         pass
+ EOF
+ :py3 sys.settrace(traceit)
+ :py3 trace_main()
+ :py3 sys.settrace(None)
+ :$put =string(l)
  :endfun
  :"
  :call Test()
*** ../vim-7.3.807/src/testdir/test87.ok	2012-09-21 14:00:05.000000000 +0200
--- src/testdir/test87.ok	2013-02-13 13:58:25.000000000 +0100
***************
*** 63,65 ****
--- 63,67 ----
  {"\0": 1}:	Vim(let):E861:
  undefined_name:	Vim(let):E860:
  vim:	Vim(let):E861:
+ [1]
+ [1, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 1]
*** ../vim-7.3.807/src/version.c	2013-02-13 12:15:59.000000000 +0100
--- src/version.c	2013-02-13 14:10:53.000000000 +0100
***************
*** 727,728 ****
--- 727,730 ----
  {   /* Add new patch number below this line */
+ /**/
+     808,
  /**/

-- 
Lawmakers made it obligatory for everybody to take at least one bath
each week -- on Saturday night.
		[real standing law in Vermont, United States of America]

 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///
