[pygobject] Deprecate GLib and GObject threads_init



commit 48e52210ece0e144b4c959e773ea542a912358e7
Author: Simon Feltman <sfeltman src gnome org>
Date:   Thu Mar 7 00:26:37 2013 -0800

    Deprecate GLib and GObject threads_init
    
    Remove a handful of Python threading related helper functions
    from pyglib and pygobject. The binding internals required
    GLib.threads_init to be called for PyGObject to work with
    Python threads. This was removed as it should not be a requirement.
    Using the Python threading module already initializes threading
    for us (PyEval_InitThreads).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=686914

 gi/_glib/glibmodule.c       |   16 -------
 gi/_glib/pyglib.c           |   94 -------------------------------------------
 gi/_glib/pyglib.h           |   28 ++++++-------
 gi/_gobject/gobjectmodule.c |   31 +++-----------
 gi/overrides/GLib.py        |    8 +++-
 gi/overrides/GObject.py     |    2 +-
 6 files changed, 28 insertions(+), 151 deletions(-)
---
diff --git a/gi/_glib/glibmodule.c b/gi/_glib/glibmodule.c
index e8e0f4f..d08d4fb 100644
--- a/gi/_glib/glibmodule.c
+++ b/gi/_glib/glibmodule.c
@@ -35,23 +35,7 @@
 
 /* ---------------- glib module functions -------------------- */
 
-static PyObject *
-pyglib_threads_init(PyObject *unused, PyObject *args, PyObject *kwargs)
-{
-    if (!pyglib_enable_threads())
-        return NULL;
-
-    Py_INCREF(Py_None);
-    return Py_None;
-}
-
 static PyMethodDef _glib_functions[] = {
-    { "threads_init",
-      (PyCFunction) pyglib_threads_init, METH_NOARGS,
-      "threads_init()\n"
-      "Initialize GLib for use from multiple threads. If you also use GTK+\n"
-      "itself (i.e. GUI, not just PyGObject), use gtk.gdk.threads_init()\n"
-      "instead." },
     { "spawn_async",
       (PyCFunction)pyglib_spawn_async, METH_VARARGS|METH_KEYWORDS,
       "spawn_async(argv, envp=None, working_directory=None,\n"
diff --git a/gi/_glib/pyglib.c b/gi/_glib/pyglib.c
index ef8a02e..54b2d90 100644
--- a/gi/_glib/pyglib.c
+++ b/gi/_glib/pyglib.c
@@ -31,7 +31,6 @@
 #include "pygoptiongroup.h"
 
 static struct _PyGLib_Functions *_PyGLib_API;
-static int pyglib_thread_state_tls_key;
 static PyObject *exception_table = NULL;
 
 void
@@ -77,99 +76,6 @@ pyglib_init_internal(PyObject *api)
     _PyGLib_API = (struct _PyGLib_Functions *) PYGLIB_CPointer_GetPointer(api, "gi._glib._PyGLib_API");
 }
 
-gboolean
-pyglib_threads_enabled(void)
-{
-    g_return_val_if_fail (_PyGLib_API != NULL, FALSE);
-
-    return _PyGLib_API->threads_enabled;
-}
-
-PyGILState_STATE
-pyglib_gil_state_ensure(void)
-{
-    g_return_val_if_fail (_PyGLib_API != NULL, PyGILState_LOCKED);
-
-    if (!_PyGLib_API->threads_enabled)
-       return PyGILState_LOCKED;
-
-#ifdef DISABLE_THREADING
-    return PyGILState_LOCKED;
-#else
-    return PyGILState_Ensure();
-#endif
-}
-
-void
-pyglib_gil_state_release(PyGILState_STATE state)
-{
-    g_return_if_fail (_PyGLib_API != NULL);
-
-    if (!_PyGLib_API->threads_enabled)
-       return;
-
-#ifndef DISABLE_THREADING
-    PyGILState_Release(state);
-#endif
-}
-
-/**
- * pyglib_enable_threads:
- *
- * Returns: TRUE if threading is enabled, FALSE otherwise.
- *
- */
-#ifdef DISABLE_THREADING
-gboolean
-pyglib_enable_threads(void)
-{
-    PyErr_SetString(PyExc_RuntimeError,
-                   "pyglib threading disabled at compile time");
-    return FALSE;
-}
-
-void
-_pyglib_notify_on_enabling_threads(PyGLibThreadsEnabledFunc callback)
-{
-    /* Ignore, threads cannot be enabled. */
-}
-
-#else
-
-static GSList *thread_enabling_callbacks = NULL;
-
-/* Enable threading; note that the GIL must be held by the current
- * thread when this function is called
- */
-gboolean
-pyglib_enable_threads(void)
-{
-    GSList *callback;
-
-    g_return_val_if_fail (_PyGLib_API != NULL, FALSE);
-
-    if (_PyGLib_API->threads_enabled)
-       return TRUE;
-  
-    PyEval_InitThreads();
-    _PyGLib_API->threads_enabled = TRUE;
-    pyglib_thread_state_tls_key = PyThread_create_key();
-
-    for (callback = thread_enabling_callbacks; callback; callback = callback->next)
-       ((PyGLibThreadsEnabledFunc) callback->data) ();
-
-    g_slist_free(thread_enabling_callbacks);
-    return TRUE;
-}
-
-void
-_pyglib_notify_on_enabling_threads(PyGLibThreadsEnabledFunc callback)
-{
-    if (callback && !pyglib_threads_enabled())
-       thread_enabling_callbacks = g_slist_append(thread_enabling_callbacks, callback);
-}
-#endif
-
 /**
  * pyglib_block_threads:
  *
diff --git a/gi/_glib/pyglib.h b/gi/_glib/pyglib.h
index bc80d6d..0b587b8 100644
--- a/gi/_glib/pyglib.h
+++ b/gi/_glib/pyglib.h
@@ -33,15 +33,24 @@ typedef void (*PyGLibThreadBlockFunc) (void);
 
 void pyglib_init(void);
 void pyglib_init_internal(PyObject *api);
-PyGILState_STATE pyglib_gil_state_ensure(void);
-void pyglib_gil_state_release(PyGILState_STATE state);
-int pyglib_enable_threads(void);
+
+#ifdef DISABLE_THREADING
+#    define pyglib_gil_state_ensure()        PyGILState_LOCKED
+#    define pyglib_gil_state_release(state)  state
+#    define pyglib_begin_allow_threads       G_STMT_START {
+#    define pyglib_end_allow_threads         } G_STMT_END
+#else
+#    define pyglib_gil_state_ensure          PyGILState_Ensure
+#    define pyglib_gil_state_release         PyGILState_Release
+#    define pyglib_begin_allow_threads       Py_BEGIN_ALLOW_THREADS
+#    define pyglib_end_allow_threads         Py_END_ALLOW_THREADS
+#endif
+
 gboolean pyglib_error_check(GError **error);
 PyObject *pyglib_error_marshal (GError **error);
 gboolean pyglib_gerror_exception_check(GError **error);
 PyObject *pyglib_register_exception_for_domain(gchar *name,
                                               gint error_domain);
-gboolean pyglib_threads_enabled(void);
 void pyglib_set_thread_block_funcs(PyGLibThreadBlockFunc block_threads_func,
                                   PyGLibThreadBlockFunc unblock_threads_func);
 void pyglib_block_threads(void);
@@ -51,20 +60,9 @@ PyObject * pyglib_option_group_new(GOptionGroup *group);
 GOptionGroup * pyglib_option_group_transfer_group(PyObject *self);
 
 /* Private: for gobject <-> glib interaction only. */
-void _pyglib_notify_on_enabling_threads(PyGLibThreadsEnabledFunc callback);
 PyObject* _pyglib_generic_ptr_richcompare(void* a, void *b, int op);
 PyObject* _pyglib_generic_long_richcompare(long a, long b, int op);
 
-#define pyglib_begin_allow_threads             \
-    G_STMT_START {                              \
-        PyThreadState *_save = NULL;            \
-        if (pyglib_threads_enabled())          \
-            _save = PyEval_SaveThread();
-
-#define pyglib_end_allow_threads                \
-        if (pyglib_threads_enabled())           \
-            PyEval_RestoreThread(_save);        \
-    } G_STMT_END
 
 #define PYGLIB_REGISTER_TYPE(d, type, name)            \
     if (!type.tp_alloc)                                 \
diff --git a/gi/_gobject/gobjectmodule.c b/gi/_gobject/gobjectmodule.c
index 359c2c7..ec91072 100644
--- a/gi/_gobject/gobjectmodule.c
+++ b/gi/_gobject/gobjectmodule.c
@@ -1525,31 +1525,13 @@ pygobject_gil_state_release (int flag)
     pyglib_gil_state_release(flag);
 }
 
-static PyObject *
-pyg_threads_init (PyObject *unused, PyObject *args, PyObject *kwargs)
-{
-    if (!pyglib_enable_threads())
-        return NULL;
-
-    Py_INCREF(Py_None);
-    return Py_None;
-}
-
 /* Only for backwards compatibility */
 static int
 pygobject_enable_threads(void)
 {
-    if (!pyglib_enable_threads())
-       return -1;
     return 0;
 }
 
-static void
-pyg_note_threads_enabled(void)
-{
-    pygobject_api_functions.threads_enabled = TRUE;
-}
-
 static PyObject *
 pyg_signal_accumulator_true_handled(PyObject *unused, PyObject *args)
 {
@@ -1690,8 +1672,6 @@ static PyMethodDef _gobject_functions[] = {
       pyg_object_class_list_properties, METH_VARARGS },
     { "new",
       (PyCFunction)pyg_object_new, METH_VARARGS|METH_KEYWORDS },
-    { "threads_init",
-      (PyCFunction)pyg_threads_init, METH_VARARGS|METH_KEYWORDS },
     { "signal_accumulator_true_handled",
       (PyCFunction)pyg_signal_accumulator_true_handled, METH_VARARGS },
     { "add_emission_hook",
@@ -2068,7 +2048,13 @@ struct _PyGObject_Functions pygobject_api_functions = {
   pyg_flags_add,
   pyg_flags_from_gtype,
 
-  FALSE, /* threads_enabled */
+  /* threads_enabled */
+#ifdef DISABLE_THREADING
+  FALSE,
+#else
+  TRUE,
+#endif
+
   pygobject_enable_threads,
   pygobject_gil_state_ensure,
   pygobject_gil_state_release,
@@ -2194,8 +2180,5 @@ PYGLIB_MODULE_START(_gobject, "_gobject")
     pygobject_pointer_register_types(d);
     pygobject_enum_register_types(d);
     pygobject_flags_register_types(d);
-
-    pygobject_api_functions.threads_enabled = pyglib_threads_enabled();
-    _pyglib_notify_on_enabling_threads(pyg_note_threads_enabled);
 }
 PYGLIB_MODULE_END
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index 0273d40..7c512ca 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -45,7 +45,13 @@ OptionContext = _glib.OptionContext
 OptionGroup = _glib.OptionGroup
 Pid = _glib.Pid
 spawn_async = _glib.spawn_async
-threads_init = _glib.threads_init
+
+
+def threads_init():
+    warnings.warn('threads_init longer needs to be called. '
+                  'See: https://bugzilla.gnome.org/show_bug.cgi?id=686914',
+                  PyGIDeprecationWarning)
+
 
 __all__ += ['GError', 'OptionContext', 'OptionGroup', 'Pid',
             'spawn_async', 'threads_init']
diff --git a/gi/overrides/GObject.py b/gi/overrides/GObject.py
index eea8928..42c97a2 100644
--- a/gi/overrides/GObject.py
+++ b/gi/overrides/GObject.py
@@ -198,7 +198,7 @@ features = _gobject.features
 list_properties = _gobject.list_properties
 new = _gobject.new
 pygobject_version = _gobject.pygobject_version
-threads_init = _gobject.threads_init
+threads_init = GLib.threads_init
 type_register = _gobject.type_register
 __all__ += ['features', 'list_properties', 'new',
             'pygobject_version', 'threads_init', 'type_register']


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]