[pygobject/foreign-cairo-fix: 2/2] foreign-cairo: Fix cairo marshalling not using the foreign converters in some cases. Fixes #260



commit bc3d7b06eab55e16d7ad51c870c96233760addd0
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Mon Sep 17 19:38:02 2018 +0200

    foreign-cairo: Fix cairo marshalling not using the foreign converters in some cases. Fixes #260
    
    In case the first cairo marshall happens through the GType converters we wont have loaded the
    cairo foreign module yet and wont have registered the right converters.
    This results in the non-foreign version getting passed on which isn't accepted by all
    other marshalling paths which use the foreign module.
    
    Try importing the cairo foreign module at gi import type instead so the gtype converters
    are registered.

 gi/__init__.py    |  2 ++
 gi/gimodule.c     |  1 +
 gi/pygi-foreign.c | 16 ++++++++++++++++
 gi/pygi-foreign.h |  2 ++
 4 files changed, 21 insertions(+)
---
diff --git a/gi/__init__.py b/gi/__init__.py
index 212136e5..bdfd5a99 100644
--- a/gi/__init__.py
+++ b/gi/__init__.py
@@ -62,6 +62,8 @@ _gobject.pygobject_version = _gi.pygobject_version
 version_info = _gi.pygobject_version[:]
 __version__ = "{0}.{1}.{2}".format(*version_info)
 
+_gi.register_foreign()
+
 
 class _DummyStaticModule(types.ModuleType):
     __path__ = None
diff --git a/gi/gimodule.c b/gi/gimodule.c
index e46af02c..3a616165 100644
--- a/gi/gimodule.c
+++ b/gi/gimodule.c
@@ -2279,6 +2279,7 @@ static PyMethodDef _gi_functions[] = {
     { "source_set_callback", (PyCFunction) pygi_source_set_callback, METH_VARARGS },
     { "io_channel_read", (PyCFunction) pyg_channel_read, METH_VARARGS },
     { "require_foreign", (PyCFunction) pygi_require_foreign, METH_VARARGS | METH_KEYWORDS },
+    { "register_foreign", (PyCFunction) pygi_register_foreign, METH_NOARGS },
     { "spawn_async",
       (PyCFunction)pyglib_spawn_async, METH_VARARGS|METH_KEYWORDS,
       "spawn_async(argv, envp=None, working_directory=None,\n"
diff --git a/gi/pygi-foreign.c b/gi/pygi-foreign.c
index 7db28fd7..80a19f7c 100644
--- a/gi/pygi-foreign.c
+++ b/gi/pygi-foreign.c
@@ -217,3 +217,19 @@ pygi_foreign_init (void)
 
     return 0;
 }
+
+
+PyObject *pygi_register_foreign (PyObject *self, PyObject *args)
+{
+    /* We need to try loading the foreign modules upfront so the GType
+     * converters are registered:
+     * https://gitlab.gnome.org/GNOME/pygobject/issues/260
+     */
+    PyObject *mod = pygi_struct_foreign_load_module ("cairo");
+    if (mod == NULL)
+        PyErr_Clear ();
+    else
+        Py_DECREF (mod);
+
+    Py_RETURN_NONE;
+}
diff --git a/gi/pygi-foreign.h b/gi/pygi-foreign.h
index c155e4fc..d4c50dde 100644
--- a/gi/pygi-foreign.h
+++ b/gi/pygi-foreign.h
@@ -50,4 +50,6 @@ PyObject *pygi_require_foreign    (PyObject *self,
 
 int pygi_foreign_init (void);
 
+PyObject *pygi_register_foreign (PyObject *self, PyObject *args);
+
 #endif /* __PYGI_FOREIGN_H__ */


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