All pastes #406747 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-22 17:41 UTC
rendered paste body
        PyObject *pModules, *pModule, *pReload, *pDict;

        _init_python_support ();

        pModules = PySys_GetObject ("modules");

        g_assert (pModules != NULL);

        pModule = PyDict_GetItemString (pModules, self->filename);

        if (!pModule) {
            pModule = PyImport_ImportModule (self->filename);

            if (!pModule) {
                PyErr_Print ();
                PyErr_Clear ();

                CNL_WARNING ("opening python module %s failed", self->filename);

                return FALSE;
            }
            else
                CNL_DEBUG ("opening python module %s succeeded", self->filename);
        }
        else {
            pReload = PyImport_ReloadModule (pModule);

            if (!pReload) {
                PyErr_Print ();
                PyErr_Clear ();

                CNL_WARNING ("could not reload python module %s", self->filename);

                return FALSE;
            }
            else {
                Py_DECREF (pReload);
            }
        }

        if (pModule) {
            PyObject *pKey, *pObject;
            gint i = 0;

            if (!_PyGObject_Type) {
                PyObject *gobject_module;

                if ((gobject_module = PyImport_ImportModule ("gobject")) != NULL) {
                    _PyGObject_Type = (PyTypeObject *) PyObject_GetAttrString (gobject_module, "GObjectMeta");
                    if (!_PyGObject_Type) {
                        return FALSE;
                    }
                }
                else {
                    return FALSE;
                }
            }

            pDict = PyModule_GetDict (pModule);

            while (PyDict_Next (pDict, &i, &pKey, &pObject)) {
                if (!PyType_Check (pObject))
                    continue;

                if (pObject && PyObject_TypeCheck (pObject, &PyGObject_Type)) {
                    GType g_type = pyg_type_from_object ((PyObject *) pObject);

                    if (g_type_is_a (g_type, CNL_TYPE_OBJECT)) {
                        cnl_plugin_register (self, g_type, 0);
                    }
                }
            }

            Py_DECREF (pDict);
        }