don't crash if missing modules

parent 03b6a4c5
2006-02-18 Gustavo J. A. M. Carneiro <gjc@gnome.org>
* src/nautilus-python.c (np_init_pygnomevfs): Copy-paste of
init_pygnomevfs from pygnomevfs.h, but changing error handling to
avoid Py_FatalError, so we don't crash nautilus if gnomevfs module
is missing.
(np_init_pygnomevfs): Convert macro to inline function, make it
return TRUE/FALSE on success/failure. Update rest of code to
reflect these changes.
(np_init_pygtk): idem.
(np_init_pygobject): idem.
(nautilus_python_init_python): Call np_init_pygnomevfs instead of
init_pygnomevfs.
* src/nautilus-python.h: Moved np_init_pygobject and
np_init_pygtk to src/nautilus-python.c.
* src/nautilus-python.c: Refactor to initialize nautilus-python on
demand: python/pygtk/pygnomevfs are not initialized if no python
extension is actually found.
......
......@@ -41,7 +41,70 @@ static gboolean nautilus_python_init_python(void);
static GArray *all_types = NULL;
#define ENTRY_POINT "nautilus_extension_types"
static inline gboolean np_init_pygobject(void)
{
PyObject *gobject = PyImport_ImportModule("gobject");
if (gobject != NULL) {
PyObject *mdict = PyModule_GetDict(gobject);
PyObject *cobject = PyDict_GetItemString(mdict, "_PyGObject_API");
if (PyCObject_Check(cobject))
_PyGObject_API = (struct _PyGObject_Functions *)PyCObject_AsVoidPtr(cobject);
else {
PyErr_SetString(PyExc_RuntimeError,
"could not find _PyGObject_API object");
return FALSE;
}
} else {
PyErr_Print();
g_warning("could not import gobject");
return FALSE;
}
return TRUE;
}
static inline gboolean np_init_pygtk(void)
{
PyObject *pygtk = PyImport_ImportModule("gtk._gtk");
if (pygtk != NULL) {
PyObject *module_dict = PyModule_GetDict(pygtk);
PyObject *cobject = PyDict_GetItemString(module_dict, "_PyGtk_API");
if (PyCObject_Check(cobject))
_PyGtk_API = (struct _PyGtk_FunctionStruct*)
PyCObject_AsVoidPtr(cobject);
else {
PyErr_SetString(PyExc_RuntimeError,
"could not find _PyGtk_API object");
return FALSE;
}
} else {
PyErr_Print();
g_warning("could not import gtk._gtk");
return FALSE;
}
return TRUE;
}
static inline gboolean np_init_pygnomevfs(void)
{
PyObject *module = PyImport_ImportModule("gnomevfs");
if (module != NULL) {
PyObject *mdict = PyModule_GetDict(module);
PyObject *cobject = PyDict_GetItemString(mdict, "_PyGnomeVFS_API");
if (PyCObject_Check(cobject))
_PyGnomeVFS_API = (struct _PyGnomeVFS_Functions *)PyCObject_AsVoidPtr(cobject);
else {
g_warning("could not find _PyGnomeVFS_API object in the gnomevfs module");
return FALSE;
}
} else {
PyErr_Print();
g_warning("could not import gnomevfs");
return FALSE;
}
return TRUE;
}
static void
nautilus_python_load_file(GTypeModule *type_module, const gchar *filename)
......@@ -154,15 +217,18 @@ nautilus_python_init_python (void)
/* import gobject */
debug("init_pygobject");
np_init_pygobject();
if (!np_init_pygobject())
return FALSE;
/* import gtk */
debug("init_pygtk");
np_init_pygtk();
if (!np_init_pygtk())
return FALSE;
/* import gnomevfs */
debug("init_gnomevfs");
init_pygnomevfs();
if (!np_init_pygnomevfs())
return FALSE;
/* gobject.threads_init() */
debug("pyg_enable_threads");
......
......@@ -39,45 +39,6 @@ extern NautilusPythonDebug nautilus_python_debug;
#define debug_enter_args(x, y) { if (nautilus_python_debug & NAUTILUS_PYTHON_DEBUG_MISC) \
g_printf("%s: entered " x "\n", __FUNCTION__, y); }
/* Macros from PyGTK, changed to return FALSE instead of nothing */
#define np_init_pygobject() { \
PyObject *gobject = PyImport_ImportModule("gobject"); \
if (gobject != NULL) { \
PyObject *mdict = PyModule_GetDict(gobject); \
PyObject *cobject = PyDict_GetItemString(mdict, "_PyGObject_API"); \
if (PyCObject_Check(cobject)) \
_PyGObject_API = (struct _PyGObject_Functions *)PyCObject_AsVoidPtr(cobject); \
else { \
PyErr_SetString(PyExc_RuntimeError, \
"could not find _PyGObject_API object"); \
return FALSE; \
} \
} else { \
PyErr_Print(); \
g_warning("could not import gobject"); \
return FALSE; \
} \
}
#define np_init_pygtk() { \
PyObject *pygtk = PyImport_ImportModule("gtk._gtk"); \
if (pygtk != NULL) { \
PyObject *module_dict = PyModule_GetDict(pygtk); \
PyObject *cobject = PyDict_GetItemString(module_dict, "_PyGtk_API"); \
if (PyCObject_Check(cobject)) \
_PyGtk_API = (struct _PyGtk_FunctionStruct*) \
PyCObject_AsVoidPtr(cobject); \
else { \
PyErr_SetString(PyExc_RuntimeError, \
"could not find _PyGtk_API object"); \
return FALSE; \
} \
} else { \
PyErr_Print(); \
g_warning("could not import gtk._gtk"); \
return FALSE; \
} \
}
PyTypeObject *_PyNautilusColumn_Type;
#define PyNautilusColumn_Type (*_PyNautilusColumn_Type)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment