From efc0d94afc48a03b07955e91315e7e67945cd079 Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Sun, 11 Oct 2020 18:05:02 +0200
Subject: [PATCH] patch 8.2.1834: PyEval_InitThreads() is deprecated in Python
 3.9

Problem:    PyEval_InitThreads() is deprecated in Python 3.9.
Solution:   Do not call PyEval_InitThreads in Python 3.9 and later. (Ken
Takata, closes #7113)  Avoid warnings for functions.
---
 src/if_py_both.h |  8 ++++----
 src/if_python3.c | 21 +++++++++++----------
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/src/if_py_both.h b/src/if_py_both.h
index 0763c65..63f4329 100644
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -306,7 +306,7 @@ ObjectDir(PyObject *self, char **attributes)
 // Output buffer management
 
 // Function to write a line, points to either msg() or emsg().
-typedef void (*writefn)(char_u *);
+typedef int (*writefn)(char *);
 
 static PyTypeObject OutputType;
 
@@ -358,8 +358,8 @@ PythonIO_Flush(void)
 {
     if (old_fn != NULL && io_ga.ga_len > 0)
     {
-	((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
-	old_fn((char_u *)io_ga.ga_data);
+	((char *)io_ga.ga_data)[io_ga.ga_len] = NUL;
+	old_fn((char *)io_ga.ga_data);
     }
     io_ga.ga_len = 0;
 }
@@ -389,7 +389,7 @@ writer(writefn fn, char_u *str, PyInt n)
 
 	mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
 	((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
-	fn((char_u *)io_ga.ga_data);
+	fn((char *)io_ga.ga_data);
 	str = ptr + 1;
 	n -= len + 1;
 	io_ga.ga_len = 0;
diff --git a/src/if_python3.c b/src/if_python3.c
index 45dc308..7c9e140 100644
--- a/src/if_python3.c
+++ b/src/if_python3.c
@@ -958,11 +958,10 @@ Python3_Init(void)
 
 	Py_Initialize();
 
-	// 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.
+#if PY_VERSION_HEX < 0x03090000
+	// Initialise threads.  This is deprecated since Python 3.9.
 	PyEval_InitThreads();
+#endif
 #ifdef DYNAMIC_PYTHON3
 	get_py3_exceptions();
 #endif
@@ -980,12 +979,14 @@ Python3_Init(void)
 	// sys.path.
 	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
+	// Without the call to PyEval_SaveThread, thread specific state (such
+	// as the system trace hook), will be lost between invocations of
+	// Python code.
+	// GIL may have been 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;
-- 
1.8.3.1

