Karsten Hopp ab8882
To: vim_dev@googlegroups.com
Karsten Hopp ab8882
Subject: Patch 7.3.808
Karsten Hopp ab8882
Fcc: outbox
Karsten Hopp ab8882
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp ab8882
Mime-Version: 1.0
Karsten Hopp ab8882
Content-Type: text/plain; charset=UTF-8
Karsten Hopp ab8882
Content-Transfer-Encoding: 8bit
Karsten Hopp ab8882
------------
Karsten Hopp ab8882
Karsten Hopp ab8882
Patch 7.3.808
Karsten Hopp ab8882
Problem:    Python threads still do not work properly.
Karsten Hopp ab8882
Solution:   Fix both Python 2 and 3.  Add tests. (Ken Takata)
Karsten Hopp ab8882
Files:	    src/if_python.c, src/if_python3.c, src/testdir/test86.in,
Karsten Hopp ab8882
	    src/testdir/test86.ok, src/testdir/test87.in,
Karsten Hopp ab8882
	    src/testdir/test87.ok
Karsten Hopp ab8882
Karsten Hopp ab8882
Karsten Hopp ab8882
*** ../vim-7.3.807/src/if_python.c	2013-01-30 11:44:33.000000000 +0100
Karsten Hopp ab8882
--- src/if_python.c	2013-02-13 14:07:28.000000000 +0100
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 741,747 ****
Karsten Hopp ab8882
  	PyMac_Initialize();
Karsten Hopp ab8882
  #endif
Karsten Hopp ab8882
  	/* Initialise threads, and below save the state using
Karsten Hopp ab8882
! 	 * PyGILState_Ensure.  Without the call to PyGILState_Ensure, thread
Karsten Hopp ab8882
  	 * specific state (such as the system trace hook), will be lost
Karsten Hopp ab8882
  	 * between invocations of Python code. */
Karsten Hopp ab8882
  	PyEval_InitThreads();
Karsten Hopp ab8882
--- 741,747 ----
Karsten Hopp ab8882
  	PyMac_Initialize();
Karsten Hopp ab8882
  #endif
Karsten Hopp ab8882
  	/* Initialise threads, and below save the state using
Karsten Hopp ab8882
! 	 * PyEval_SaveThread.  Without the call to PyEval_SaveThread, thread
Karsten Hopp ab8882
  	 * specific state (such as the system trace hook), will be lost
Karsten Hopp ab8882
  	 * between invocations of Python code. */
Karsten Hopp ab8882
  	PyEval_InitThreads();
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 755,764 ****
Karsten Hopp ab8882
  	if (PythonMod_Init())
Karsten Hopp ab8882
  	    goto fail;
Karsten Hopp ab8882
  
Karsten Hopp ab8882
- 	/* The first python thread is vim's, release the lock. */
Karsten Hopp ab8882
- 	Python_SaveThread();
Karsten Hopp ab8882
- 	pygilstate = PyGILState_Ensure();
Karsten Hopp ab8882
- 
Karsten Hopp ab8882
  	globals = PyModule_GetDict(PyImport_AddModule("__main__"));
Karsten Hopp ab8882
  
Karsten Hopp ab8882
  	/* Remove the element from sys.path that was added because of our
Karsten Hopp ab8882
--- 755,760 ----
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 767,773 ****
Karsten Hopp ab8882
  	 * the current directory in sys.path. */
Karsten Hopp ab8882
  	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
Karsten Hopp ab8882
  
Karsten Hopp ab8882
! 	PyGILState_Release(pygilstate);
Karsten Hopp ab8882
  
Karsten Hopp ab8882
  	initialised = 1;
Karsten Hopp ab8882
      }
Karsten Hopp ab8882
--- 763,776 ----
Karsten Hopp ab8882
  	 * the current directory in sys.path. */
Karsten Hopp ab8882
  	PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
Karsten Hopp ab8882
  
Karsten Hopp ab8882
! 	/* lock is created and acquired in PyEval_InitThreads() and thread
Karsten Hopp ab8882
! 	 * state is created in Py_Initialize()
Karsten Hopp ab8882
! 	 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
Karsten Hopp ab8882
! 	 * (python must have threads enabled!)
Karsten Hopp ab8882
! 	 * so the following does both: unlock GIL and save thread state in TLS
Karsten Hopp ab8882
! 	 * without deleting thread state
Karsten Hopp ab8882
! 	 */
Karsten Hopp ab8882
! 	PyEval_SaveThread();
Karsten Hopp ab8882
  
Karsten Hopp ab8882
  	initialised = 1;
Karsten Hopp ab8882
      }
Karsten Hopp ab8882
*** ../vim-7.3.807/src/if_python3.c	2012-11-28 15:33:10.000000000 +0100
Karsten Hopp ab8882
--- src/if_python3.c	2013-02-13 14:07:28.000000000 +0100
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 729,741 ****
Karsten Hopp ab8882
  #else
Karsten Hopp ab8882
  	PyMac_Initialize();
Karsten Hopp ab8882
  #endif
Karsten Hopp ab8882
! 	/* Initialise threads, and save the state using PyGILState_Ensure.
Karsten Hopp ab8882
! 	 * Without the call to PyGILState_Ensure, thread specific state (such
Karsten Hopp ab8882
! 	 * as the system trace hook), will be lost between invocations of
Karsten Hopp ab8882
! 	 * Python code. */
Karsten Hopp ab8882
  	PyEval_InitThreads();
Karsten Hopp ab8882
- 	pygilstate = PyGILState_Ensure();
Karsten Hopp ab8882
- 
Karsten Hopp ab8882
  #ifdef DYNAMIC_PYTHON3
Karsten Hopp ab8882
  	get_py3_exceptions();
Karsten Hopp ab8882
  #endif
Karsten Hopp ab8882
--- 729,739 ----
Karsten Hopp ab8882
  #else
Karsten Hopp ab8882
  	PyMac_Initialize();
Karsten Hopp ab8882
  #endif
Karsten Hopp ab8882
! 	/* Initialise threads, and below save the state using
Karsten Hopp ab8882
! 	 * PyEval_SaveThread.  Without the call to PyEval_SaveThread, thread
Karsten Hopp ab8882
! 	 * specific state (such as the system trace hook), will be lost
Karsten Hopp ab8882
! 	 * between invocations of Python code. */
Karsten Hopp ab8882
  	PyEval_InitThreads();
Karsten Hopp ab8882
  #ifdef DYNAMIC_PYTHON3
Karsten Hopp ab8882
  	get_py3_exceptions();
Karsten Hopp ab8882
  #endif
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 754,766 ****
Karsten Hopp ab8882
  	 */
Karsten Hopp ab8882
  	PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
Karsten Hopp ab8882
  
Karsten Hopp ab8882
! 	// lock is created and acquired in PyEval_InitThreads() and thread
Karsten Hopp ab8882
! 	// state is created in Py_Initialize()
Karsten Hopp ab8882
! 	// there _PyGILState_NoteThreadState() also sets gilcounter to 1
Karsten Hopp ab8882
! 	// (python must have threads enabled!)
Karsten Hopp ab8882
! 	// so the following does both: unlock GIL and save thread state in TLS
Karsten Hopp ab8882
! 	// without deleting thread state
Karsten Hopp ab8882
! 	PyGILState_Release(pygilstate);
Karsten Hopp ab8882
  
Karsten Hopp ab8882
  	py3initialised = 1;
Karsten Hopp ab8882
      }
Karsten Hopp ab8882
--- 752,765 ----
Karsten Hopp ab8882
  	 */
Karsten Hopp ab8882
  	PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
Karsten Hopp ab8882
  
Karsten Hopp ab8882
! 	/* lock is created and acquired in PyEval_InitThreads() and thread
Karsten Hopp ab8882
! 	 * state is created in Py_Initialize()
Karsten Hopp ab8882
! 	 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
Karsten Hopp ab8882
! 	 * (python must have threads enabled!)
Karsten Hopp ab8882
! 	 * so the following does both: unlock GIL and save thread state in TLS
Karsten Hopp ab8882
! 	 * without deleting thread state
Karsten Hopp ab8882
! 	 */
Karsten Hopp ab8882
! 	PyEval_SaveThread();
Karsten Hopp ab8882
  
Karsten Hopp ab8882
  	py3initialised = 1;
Karsten Hopp ab8882
      }
Karsten Hopp ab8882
*** ../vim-7.3.807/src/testdir/test86.in	2012-09-21 14:00:05.000000000 +0200
Karsten Hopp ab8882
--- src/testdir/test86.in	2013-02-13 13:58:25.000000000 +0100
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 267,272 ****
Karsten Hopp ab8882
--- 267,320 ----
Karsten Hopp ab8882
  :      $put =toput
Karsten Hopp ab8882
  :   endtry
Karsten Hopp ab8882
  :endfor
Karsten Hopp ab8882
+ :"
Karsten Hopp ab8882
+ :" threading
Karsten Hopp ab8882
+ :let l = [0]
Karsten Hopp ab8882
+ :py l=vim.bindeval('l')
Karsten Hopp ab8882
+ :py <
Karsten Hopp ab8882
+ import threading
Karsten Hopp ab8882
+ import time
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+ class T(threading.Thread):
Karsten Hopp ab8882
+     def __init__(self):
Karsten Hopp ab8882
+         threading.Thread.__init__(self)
Karsten Hopp ab8882
+         self.t = 0
Karsten Hopp ab8882
+         self.running = True
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+     def run(self):
Karsten Hopp ab8882
+         while self.running:
Karsten Hopp ab8882
+             self.t += 1
Karsten Hopp ab8882
+             time.sleep(0.1)
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+ t = T()
Karsten Hopp ab8882
+ t.start()
Karsten Hopp ab8882
+ EOF
Karsten Hopp ab8882
+ :sleep 1
Karsten Hopp ab8882
+ :py t.running = False
Karsten Hopp ab8882
+ :py t.join()
Karsten Hopp ab8882
+ :py l[0] = t.t > 8  # check if the background thread is working
Karsten Hopp ab8882
+ :$put =string(l)
Karsten Hopp ab8882
+ :"
Karsten Hopp ab8882
+ :" settrace
Karsten Hopp ab8882
+ :let l = []
Karsten Hopp ab8882
+ :py l=vim.bindeval('l')
Karsten Hopp ab8882
+ :py <
Karsten Hopp ab8882
+ import sys
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+ def traceit(frame, event, arg):
Karsten Hopp ab8882
+     global l
Karsten Hopp ab8882
+     if event == "line":
Karsten Hopp ab8882
+         l.extend([frame.f_lineno])
Karsten Hopp ab8882
+     return traceit
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+ def trace_main():
Karsten Hopp ab8882
+     for i in range(5):
Karsten Hopp ab8882
+         pass
Karsten Hopp ab8882
+ EOF
Karsten Hopp ab8882
+ :py sys.settrace(traceit)
Karsten Hopp ab8882
+ :py trace_main()
Karsten Hopp ab8882
+ :py sys.settrace(None)
Karsten Hopp ab8882
+ :$put =string(l)
Karsten Hopp ab8882
  :endfun
Karsten Hopp ab8882
  :"
Karsten Hopp ab8882
  :call Test()
Karsten Hopp ab8882
*** ../vim-7.3.807/src/testdir/test86.ok	2012-09-21 14:00:05.000000000 +0200
Karsten Hopp ab8882
--- src/testdir/test86.ok	2013-02-13 13:58:25.000000000 +0100
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 63,65 ****
Karsten Hopp ab8882
--- 63,67 ----
Karsten Hopp ab8882
  {"\0": 1}:	Vim(let):E859:
Karsten Hopp ab8882
  undefined_name:	Vim(let):E858:
Karsten Hopp ab8882
  vim:	Vim(let):E859:
Karsten Hopp ab8882
+ [1]
Karsten Hopp ab8882
+ [1, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 1]
Karsten Hopp ab8882
*** ../vim-7.3.807/src/testdir/test87.in	2012-09-21 14:00:05.000000000 +0200
Karsten Hopp ab8882
--- src/testdir/test87.in	2013-02-13 13:58:25.000000000 +0100
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 267,272 ****
Karsten Hopp ab8882
--- 267,320 ----
Karsten Hopp ab8882
  :      $put =toput
Karsten Hopp ab8882
  :   endtry
Karsten Hopp ab8882
  :endfor
Karsten Hopp ab8882
+ :"
Karsten Hopp ab8882
+ :" threading
Karsten Hopp ab8882
+ :let l = [0]
Karsten Hopp ab8882
+ :py3 l=vim.bindeval('l')
Karsten Hopp ab8882
+ :py3 <
Karsten Hopp ab8882
+ import threading
Karsten Hopp ab8882
+ import time
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+ class T(threading.Thread):
Karsten Hopp ab8882
+     def __init__(self):
Karsten Hopp ab8882
+         threading.Thread.__init__(self)
Karsten Hopp ab8882
+         self.t = 0
Karsten Hopp ab8882
+         self.running = True
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+     def run(self):
Karsten Hopp ab8882
+         while self.running:
Karsten Hopp ab8882
+             self.t += 1
Karsten Hopp ab8882
+             time.sleep(0.1)
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+ t = T()
Karsten Hopp ab8882
+ t.start()
Karsten Hopp ab8882
+ EOF
Karsten Hopp ab8882
+ :sleep 1
Karsten Hopp ab8882
+ :py3 t.running = False
Karsten Hopp ab8882
+ :py3 t.join()
Karsten Hopp ab8882
+ :py3 l[0] = t.t > 8  # check if the background thread is working
Karsten Hopp ab8882
+ :$put =string(l)
Karsten Hopp ab8882
+ :"
Karsten Hopp ab8882
+ :" settrace
Karsten Hopp ab8882
+ :let l = []
Karsten Hopp ab8882
+ :py3 l=vim.bindeval('l')
Karsten Hopp ab8882
+ :py3 <
Karsten Hopp ab8882
+ import sys
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+ def traceit(frame, event, arg):
Karsten Hopp ab8882
+     global l
Karsten Hopp ab8882
+     if event == "line":
Karsten Hopp ab8882
+         l += [frame.f_lineno]
Karsten Hopp ab8882
+     return traceit
Karsten Hopp ab8882
+ 
Karsten Hopp ab8882
+ def trace_main():
Karsten Hopp ab8882
+     for i in range(5):
Karsten Hopp ab8882
+         pass
Karsten Hopp ab8882
+ EOF
Karsten Hopp ab8882
+ :py3 sys.settrace(traceit)
Karsten Hopp ab8882
+ :py3 trace_main()
Karsten Hopp ab8882
+ :py3 sys.settrace(None)
Karsten Hopp ab8882
+ :$put =string(l)
Karsten Hopp ab8882
  :endfun
Karsten Hopp ab8882
  :"
Karsten Hopp ab8882
  :call Test()
Karsten Hopp ab8882
*** ../vim-7.3.807/src/testdir/test87.ok	2012-09-21 14:00:05.000000000 +0200
Karsten Hopp ab8882
--- src/testdir/test87.ok	2013-02-13 13:58:25.000000000 +0100
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 63,65 ****
Karsten Hopp ab8882
--- 63,67 ----
Karsten Hopp ab8882
  {"\0": 1}:	Vim(let):E861:
Karsten Hopp ab8882
  undefined_name:	Vim(let):E860:
Karsten Hopp ab8882
  vim:	Vim(let):E861:
Karsten Hopp ab8882
+ [1]
Karsten Hopp ab8882
+ [1, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 1]
Karsten Hopp ab8882
*** ../vim-7.3.807/src/version.c	2013-02-13 12:15:59.000000000 +0100
Karsten Hopp ab8882
--- src/version.c	2013-02-13 14:10:53.000000000 +0100
Karsten Hopp ab8882
***************
Karsten Hopp ab8882
*** 727,728 ****
Karsten Hopp ab8882
--- 727,730 ----
Karsten Hopp ab8882
  {   /* Add new patch number below this line */
Karsten Hopp ab8882
+ /**/
Karsten Hopp ab8882
+     808,
Karsten Hopp ab8882
  /**/
Karsten Hopp ab8882
Karsten Hopp ab8882
-- 
Karsten Hopp ab8882
Lawmakers made it obligatory for everybody to take at least one bath
Karsten Hopp ab8882
each week -- on Saturday night.
Karsten Hopp ab8882
		[real standing law in Vermont, United States of America]
Karsten Hopp ab8882
Karsten Hopp ab8882
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp ab8882
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp ab8882
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp ab8882
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///