bindings/python: Handle import failures without crashing
When the import of gi.repository.GLib failed, we would get a NULL pointer that we passed along without any checking. In this situation, the entire program would crash with a segmentation fault, and no message to indicate the problem. When the import fails, abort the SWIG init and print a message. The Python interpreter then prints out a backtrace, which can be useful in tracking down the problem.
This commit is contained in:
parent
9a5185c736
commit
aae2273b99
|
@ -45,9 +45,11 @@ which provides access to the error code and description."
|
||||||
%module(docstring=DOCSTRING) classes
|
%module(docstring=DOCSTRING) classes
|
||||||
|
|
||||||
%{
|
%{
|
||||||
|
#include <stdio.h>
|
||||||
#include <pygobject.h>
|
#include <pygobject.h>
|
||||||
#include <numpy/arrayobject.h>
|
#include <numpy/arrayobject.h>
|
||||||
|
|
||||||
|
PyObject *PyGObject_lib;
|
||||||
PyObject *GLib;
|
PyObject *GLib;
|
||||||
PyTypeObject *IOChannel;
|
PyTypeObject *IOChannel;
|
||||||
PyTypeObject *PollFD;
|
PyTypeObject *PollFD;
|
||||||
|
@ -63,8 +65,18 @@ typedef guint pyg_flags_type;
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%init %{
|
%init %{
|
||||||
pygobject_init(-1, -1, -1);
|
PyGObject_lib = pygobject_init(-1, -1, -1);
|
||||||
|
if (!PyGObject_lib)
|
||||||
|
fprintf(stderr, "pygobject initialization failed.\n");
|
||||||
GLib = PyImport_ImportModule("gi.repository.GLib");
|
GLib = PyImport_ImportModule("gi.repository.GLib");
|
||||||
|
/*
|
||||||
|
* This check can't save us if the import fails, but at least it gives us
|
||||||
|
* a starting point to trace the issue versus straight out crashing.
|
||||||
|
*/
|
||||||
|
if (!GLib) {
|
||||||
|
fprintf(stderr, "Import of gi.repository.GLib failed.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
IOChannel = (PyTypeObject *) PyObject_GetAttrString(GLib, "IOChannel");
|
IOChannel = (PyTypeObject *) PyObject_GetAttrString(GLib, "IOChannel");
|
||||||
PollFD = (PyTypeObject *) PyObject_GetAttrString(GLib, "PollFD");
|
PollFD = (PyTypeObject *) PyObject_GetAttrString(GLib, "PollFD");
|
||||||
import_array();
|
import_array();
|
||||||
|
|
Loading…
Reference in New Issue