python: Remove deprecated use of PyEval_ThreadsInitialized.

The correct/modern way to do this is:

https://docs.python.org/3/c-api/init.html#releasing-the-gil-from-extension-code

PyEval_ThreadsInitialized was deprecated in 3.9 and will be removed
completely in 3.11, so we shouldn't use that function.
This commit is contained in:
Richard W.M. Jones
2020-07-06 12:13:07 +01:00
parent 403d17a876
commit 75def0ef18
2 changed files with 8 additions and 29 deletions

View File

@@ -268,9 +268,6 @@ and generate_python_actions actions () =
pr "guestfs_int_py_%s (PyObject *self, PyObject *args)\n" name;
pr "{\n";
if blocking then
pr " PyThreadState *py_save = NULL;\n";
pr " PyObject *py_g;\n";
pr " guestfs_h *g;\n";
pr " PyObject *py_r = NULL;\n";
@@ -413,27 +410,15 @@ and generate_python_actions actions () =
pr "\n"
);
if blocking then (
(* Release Python GIL while running. This code is from
* libvirt/python/typewrappers.h. Thanks to Dan Berrange for
* showing us how to do this properly.
*)
pr " if (PyEval_ThreadsInitialized ())\n";
pr " py_save = PyEval_SaveThread ();\n";
pr "\n"
);
if blocking then
pr " Py_BEGIN_ALLOW_THREADS\n";
pr " r = %s " c_function;
generate_c_call_args ~handle:"g" style;
pr ";\n";
if blocking then
pr " Py_END_ALLOW_THREADS\n";
pr "\n";
if blocking then (
pr " if (PyEval_ThreadsInitialized ())\n";
pr " PyEval_RestoreThread (py_save);\n";
pr "\n"
);
(match errcode_of_ret ret with
| `CannotReturnError -> ()
| `ErrorIsMinusOne ->

View File

@@ -62,7 +62,6 @@ guestfs_int_py_create (PyObject *self, PyObject *args)
PyObject *
guestfs_int_py_close (PyObject *self, PyObject *args)
{
PyThreadState *py_save = NULL;
PyObject *py_g;
guestfs_h *g;
size_t len;
@@ -85,11 +84,9 @@ guestfs_int_py_close (PyObject *self, PyObject *args)
if (len != 0 && callbacks == NULL)
return NULL;
if (PyEval_ThreadsInitialized ())
py_save = PyEval_SaveThread ();
Py_BEGIN_ALLOW_THREADS;
guestfs_close (g);
if (PyEval_ThreadsInitialized ())
PyEval_RestoreThread (py_save);
Py_END_ALLOW_THREADS;
if (len > 0) {
size_t i;
@@ -119,15 +116,13 @@ guestfs_int_py_event_callback_wrapper (guestfs_h *g,
PyObject *a;
size_t i;
PyObject *py_r;
int threads_initialized = PyEval_ThreadsInitialized ();
#ifdef HAVE_PY_ISFINALIZING
if (_Py_IsFinalizing ())
return;
#endif
if (threads_initialized)
py_save = PyGILState_Ensure ();
py_save = PyGILState_Ensure ();
py_array = PyList_New (array_len);
for (i = 0; i < array_len; ++i) {
@@ -151,8 +146,7 @@ guestfs_int_py_event_callback_wrapper (guestfs_h *g,
/* Callback threw an exception: print it. */
PyErr_PrintEx (0);
if (threads_initialized)
PyGILState_Release (py_save);
PyGILState_Release (py_save);
}
PyObject *