[libpeas] Bug 732155 - Fix initializing Python loader from Python application



commit a5bce456109604b0490551a6914e386741a32a05
Author: Garrett Regier <Garrett Regier riftio com>
Date:   Tue Jul 22 15:17:18 2014 -0400

    Bug 732155 - Fix initializing Python loader from Python application
    
    https://bugzilla.gnome.org/show_bug.cgi?id=732155

 loaders/python/peas-plugin-loader-python.c |   15 +++++++++++++--
 tests/libpeas/extension-py.c               |   27 +++++++++++++++++++++++----
 2 files changed, 36 insertions(+), 6 deletions(-)
---
diff --git a/loaders/python/peas-plugin-loader-python.c b/loaders/python/peas-plugin-loader-python.c
index ed018a3..91a3d41 100644
--- a/loaders/python/peas-plugin-loader-python.c
+++ b/loaders/python/peas-plugin-loader-python.c
@@ -375,6 +375,7 @@ static gboolean
 peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
 {
   PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
+  PyGILState_STATE state = 0;
   long hexversion;
   PyObject *gettext, *result;
   const gchar *prgname;
@@ -390,7 +391,11 @@ peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
   pyloader->priv->init_failed = TRUE;
 
   /* Python initialization */
-  if (!Py_IsInitialized ())
+  if (Py_IsInitialized ())
+    {
+      state = PyGILState_Ensure ();
+    }
+  else
     {
 #ifdef HAVE_SIGACTION
       struct sigaction sigint;
@@ -506,7 +511,10 @@ peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
   /* Python has been successfully initialized */
   pyloader->priv->init_failed = FALSE;
 
-  pyloader->priv->py_thread_state = PyEval_SaveThread ();
+  if (!pyloader->priv->must_finalize_python)
+    PyGILState_Release (state);
+  else
+    pyloader->priv->py_thread_state = PyEval_SaveThread ();
 
   return TRUE;
 
@@ -518,6 +526,9 @@ python_init_error:
   if (PyErr_Occurred ())
     PyErr_Clear ();
 
+  if (!pyloader->priv->must_finalize_python)
+    PyGILState_Release (state);
+
   return FALSE;
 }
 
diff --git a/tests/libpeas/extension-py.c b/tests/libpeas/extension-py.c
index a25b1fe..6b6a472 100644
--- a/tests/libpeas/extension-py.c
+++ b/tests/libpeas/extension-py.c
@@ -145,18 +145,37 @@ static void
 test_extension_py_already_initialized_subprocess (void)
 {
   PeasEngine *engine;
-  PeasPluginInfo *info;
+  PyObject *module, *dict, *pyengine, *result;
 
   /* Check that python has not been initialized yet */
   g_assert (!Py_IsInitialized ());
-
   Py_InitializeEx (FALSE);
 
+  /* Initialize PyGObject */
+  pygobject_init (PYGOBJECT_MAJOR_VERSION,
+                  PYGOBJECT_MINOR_VERSION,
+                  PYGOBJECT_MICRO_VERSION);
+  g_assert (!PyErr_Occurred ());
+
   engine = testing_engine_new ();
-  info = peas_engine_get_plugin_info (engine, "extension-" PY_LOADER_STR);
+  peas_engine_enable_loader (engine, PY_LOADER_STR);
+
+  module = PyImport_AddModule ("__main__");
+  dict = PyModule_GetDict (module);
+
+  pyengine = pygobject_new (G_OBJECT (engine));
+  g_assert (PyDict_SetItemString (dict, "engine", pyengine) == 0);
+  Py_DECREF (pyengine);
+
+  result = PyRun_String ("plugin_name = 'extension-" PY_LOADER_STR "'\n"
+                         "info = engine.get_plugin_info(plugin_name)\n"
+                         "assert engine.load_plugin(info)\n",
+                         Py_file_input, dict, dict);
+  Py_XDECREF (result);
 
-  g_assert (peas_engine_load_plugin (engine, info));
+  g_assert (!PyErr_Occurred ());
 
+  PyDict_Clear (dict);
   testing_engine_free (engine);
 
   peas_engine_shutdown ();


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