42 #include <glib-object.h> 82 #define pygobject_get(v) (((PyGObject *)(v))->obj) 83 #define pygobject_check(v,base) (PyObject_TypeCheck(v,base)) 92 #define pyg_boxed_get(v,t) ((t *)((PyGBoxed *)(v))->boxed) 93 #define pyg_boxed_check(v,typecode) (PyObject_TypeCheck(v, &PyGBoxed_Type) && ((PyGBoxed *)(v))->gtype == typecode) 101 #define pyg_pointer_get(v,t) ((t *)((PyGPointer *)(v))->pointer) 102 #define pyg_pointer_check(v,typecode) (PyObject_TypeCheck(v, &PyGPointer_Type) && ((PyGPointer *)(v))->gtype == typecode) 112 #define PyGParamSpec_Get(v) (((PyGParamSpec *)v)->pspec) 113 #define PyGParamSpec_Check(v) (PyObject_TypeCheck(v, &PyGParamSpec_Type)) 116 typedef PyTypeObject * (*PyGTypeRegistrationFunction) (
const gchar *name,
124 void (* register_class)(PyObject *dict,
const gchar *class_name,
125 GType gtype, PyTypeObject *type, PyObject *bases);
126 void (* register_wrapper)(PyObject *
self);
127 PyTypeObject *(* lookup_class)(GType type);
128 PyObject *(* newgobj)(GObject *obj);
132 void (* object_watch_closure)(PyObject *
self, GClosure *
closure);
135 GType (* type_from_object)(PyObject *obj);
136 PyObject *(* type_wrapper_new)(GType type);
138 gint (* enum_get_value)(GType enum_type, PyObject *obj, gint *val);
139 gint (* flags_get_value)(GType flag_type, PyObject *obj, gint *val);
140 void (* register_gtype_custom)(GType gtype,
141 PyObject *(* from_func)(
const GValue *value),
142 int (* to_func)(GValue *value, PyObject *obj));
143 int (* value_from_pyobject)(GValue *value, PyObject *obj);
144 PyObject *(* value_as_pyobject)(
const GValue *value, gboolean copy_boxed);
146 void (* register_interface)(PyObject *dict,
const gchar *class_name,
147 GType gtype, PyTypeObject *type);
150 void (* register_boxed)(PyObject *dict,
const gchar *class_name,
151 GType boxed_type, PyTypeObject *type);
152 PyObject *(* boxed_new)(GType boxed_type, gpointer boxed,
153 gboolean copy_boxed, gboolean own_ref);
156 void (* register_pointer)(PyObject *dict,
const gchar *class_name,
157 GType pointer_type, PyTypeObject *type);
158 PyObject *(* pointer_new)(GType boxed_type, gpointer pointer);
160 void (* enum_add_constants)(PyObject *module, GType enum_type,
161 const gchar *strip_prefix);
162 void (* flags_add_constants)(PyObject *module, GType flags_type,
163 const gchar *strip_prefix);
165 const gchar *(* constant_strip_prefix)(
const gchar *name,
166 const gchar *strip_prefix);
168 gboolean (* error_check)(GError **error);
177 PyObject *(* paramspec_new)(GParamSpec *spec);
178 GParamSpec *(*paramspec_get)(PyObject *tuple);
179 int (*pyobj_to_unichar_conv)(PyObject *pyobj,
void* ptr);
180 gboolean (*parse_constructor_args)(GType obj_type,
186 PyObject *(* param_gvalue_as_pyobject) (
const GValue* gvalue,
188 const GParamSpec* pspec);
189 int (* gvalue_from_param_pyobject) (GValue* value,
191 const GParamSpec* pspec);
193 PyObject *(*enum_add)(PyObject *module,
194 const char *type_name_,
195 const char *strip_prefix,
197 PyObject* (*enum_from_gtype)(GType gtype,
int value);
200 PyObject *(*flags_add)(PyObject *module,
201 const char *type_name_,
202 const char *strip_prefix,
204 PyObject* (*flags_from_gtype)(GType gtype,
int value);
207 int (*enable_threads) (void);
209 int (*gil_state_ensure) (void);
210 void (*gil_state_release) (
int flag);
213 void (*register_interface_info) (GType gtype,
const GInterfaceInfo *info);
216 void (*add_warning_redirection) (
const char *domain,
218 void (*disable_warning_redirections) (void);
219 void (*type_register_custom)(
const gchar *type_name,
222 gboolean (*gerror_exception_check) (GError **error);
223 PyObject* (*option_group_new) (GOptionGroup *group);
224 GType (* type_from_object_strict) (PyObject *obj, gboolean strict);
227 #ifndef _INSIDE_PYGOBJECT_ 229 #if defined(NO_IMPORT) || defined(NO_IMPORT_PYGOBJECT) 235 #define pygobject_register_class (_PyGObject_API->register_class) 236 #define pygobject_register_wrapper (_PyGObject_API->register_wrapper) 237 #define pygobject_lookup_class (_PyGObject_API->lookup_class) 238 #define pygobject_new (_PyGObject_API->newgobj) 239 #define pyg_closure_new (_PyGObject_API->closure_new) 240 #define pygobject_watch_closure (_PyGObject_API->object_watch_closure) 241 #define pyg_closure_set_exception_handler (_PyGObject_API->closure_set_exception_handler) 242 #define pyg_destroy_notify (_PyGObject_API->destroy_notify) 243 #define pyg_type_from_object_strict (_PyGObject_API->type_from_object_strict) 244 #define pyg_type_from_object (_PyGObject_API->type_from_object) 245 #define pyg_type_wrapper_new (_PyGObject_API->type_wrapper_new) 246 #define pyg_enum_get_value (_PyGObject_API->enum_get_value) 247 #define pyg_flags_get_value (_PyGObject_API->flags_get_value) 248 #define pyg_register_gtype_custom (_PyGObject_API->register_gtype_custom) 249 #define pyg_value_from_pyobject (_PyGObject_API->value_from_pyobject) 250 #define pyg_value_as_pyobject (_PyGObject_API->value_as_pyobject) 251 #define pyg_register_interface (_PyGObject_API->register_interface) 252 #define PyGBoxed_Type (*_PyGObject_API->boxed_type) 253 #define pyg_register_boxed (_PyGObject_API->register_boxed) 254 #define pyg_boxed_new (_PyGObject_API->boxed_new) 255 #define PyGPointer_Type (*_PyGObject_API->pointer_type) 256 #define pyg_register_pointer (_PyGObject_API->register_pointer) 257 #define pyg_pointer_new (_PyGObject_API->pointer_new) 258 #define pyg_enum_add_constants (_PyGObject_API->enum_add_constants) 259 #define pyg_flags_add_constants (_PyGObject_API->flags_add_constants) 260 #define pyg_constant_strip_prefix (_PyGObject_API->constant_strip_prefix) 261 #define pyg_error_check (_PyGObject_API->error_check) 262 #define pyg_set_thread_block_funcs (_PyGObject_API->set_thread_block_funcs) 263 #define PyGParamSpec_Type (*_PyGObject_API->paramspec_type) 264 #define pyg_param_spec_new (_PyGObject_API->paramspec_new) 265 #define pyg_param_spec_from_object (_PyGObject_API->paramspec_get) 266 #define pyg_pyobj_to_unichar_conv (_PyGObject_API->pyobj_to_unichar_conv) 267 #define pyg_parse_constructor_args (_PyGObject_API->parse_constructor_args) 268 #define pyg_param_gvalue_as_pyobject (_PyGObject_API->value_as_pyobject) 269 #define pyg_param_gvalue_from_pyobject (_PyGObject_API->gvalue_from_param_pyobject) 270 #define PyGEnum_Type (*_PyGObject_API->enum_type) 271 #define pyg_enum_add (_PyGObject_API->enum_add) 272 #define pyg_enum_from_gtype (_PyGObject_API->enum_from_gtype) 273 #define PyGFlags_Type (*_PyGObject_API->flags_type) 274 #define pyg_flags_add (_PyGObject_API->flags_add) 275 #define pyg_flags_from_gtype (_PyGObject_API->flags_from_gtype) 276 #define pyg_enable_threads (_PyGObject_API->enable_threads) 277 #define pyg_gil_state_ensure (_PyGObject_API->gil_state_ensure) 278 #define pyg_gil_state_release (_PyGObject_API->gil_state_release) 279 #define pyg_register_class_init (_PyGObject_API->register_class_init) 280 #define pyg_register_interface_info (_PyGObject_API->register_interface_info) 281 #define pyg_add_warning_redirection (_PyGObject_API->add_warning_redirection) 282 #define pyg_disable_warning_redirections (_PyGObject_API->disable_warning_redirections) 283 #define pyg_type_register_custom_callback (_PyGObject_API->type_register_custom) 284 #define pyg_gerror_exception_check (_PyGObject_API->gerror_exception_check) 285 #define pyg_option_group_new (_PyGObject_API->option_group_new) 287 #define pyg_block_threads() G_STMT_START { \ 288 if (_PyGObject_API->block_threads != NULL) \ 289 (* _PyGObject_API->block_threads)(); \ 291 #define pyg_unblock_threads() G_STMT_START { \ 292 if (_PyGObject_API->unblock_threads != NULL) \ 293 (* _PyGObject_API->unblock_threads)(); \ 296 #define pyg_threads_enabled (_PyGObject_API->threads_enabled) 298 #define pyg_begin_allow_threads \ 300 PyThreadState *_save = NULL; \ 301 if (_PyGObject_API->threads_enabled) \ 302 _save = PyEval_SaveThread(); 303 #define pyg_end_allow_threads \ 304 if (_PyGObject_API->threads_enabled) \ 305 PyEval_RestoreThread(_save); \ 322 static inline PyObject *
325 PyObject *gobject, *cobject;
327 gobject = PyImport_ImportModule(
"gi._gobject");
329 if (PyErr_Occurred())
331 PyObject *type, *value, *traceback;
332 PyObject *py_orig_exc;
333 PyErr_Fetch(&type, &value, &traceback);
334 py_orig_exc = PyObject_Repr(value);
337 Py_XDECREF(traceback);
340 #if PY_VERSION_HEX < 0x03000000 341 PyErr_Format(PyExc_ImportError,
342 "could not import gobject (error was: %s)",
343 PyString_AsString(py_orig_exc));
350 PyObject *errmsg = PyUnicode_FromFormat(
"could not import gobject (error was: %U)",
354 PyErr_SetObject(PyExc_ImportError,
364 Py_DECREF(py_orig_exc);
366 PyErr_SetString(PyExc_ImportError,
367 "could not import gobject (no error given)");
372 cobject = PyObject_GetAttrString(gobject,
"_PyGObject_API");
373 #if PY_VERSION_HEX >= 0x03000000 374 if (cobject && PyCapsule_CheckExact(cobject))
375 _PyGObject_API = (
struct _PyGObject_Functions *) PyCapsule_GetPointer(cobject,
"gobject._PyGObject_API");
378 if (cobject && PyCObject_Check(cobject))
382 PyErr_SetString(PyExc_ImportError,
383 "could not import gobject (could not find _PyGObject_API object)");
390 int found_major, found_minor, found_micro;
393 version = PyObject_GetAttrString(gobject,
"pygobject_version");
395 PyErr_SetString(PyExc_ImportError,
396 "could not import gobject (version too old)");
400 if (!PyArg_ParseTuple(version,
"iii",
401 &found_major, &found_minor, &found_micro)) {
402 PyErr_SetString(PyExc_ImportError,
403 "could not import gobject (version has invalid format)");
409 if (req_major != found_major ||
410 req_minor > found_minor ||
411 (req_minor == found_minor && req_micro > found_micro)) {
412 PyErr_Format(PyExc_ImportError,
413 "could not import gobject (version mismatch, %d.%d.%d is required, " 414 "found %d.%d.%d)", req_major, req_minor, req_micro,
415 found_major, found_minor, found_micro);
437 #define PYLIST_FROMGLIBLIST(type,prefix,py_list,list,item_convert_func,\ 438 list_free,list_item_free) \ 443 void (*glib_list_free)(type*) = list_free; \ 444 GFunc glib_list_item_free = (GFunc)list_item_free; \ 446 len = prefix##_length(list); \ 447 py_list = PyList_New(len); \ 448 for (i = 0; i < len; i++) { \ 449 gpointer list_item = prefix##_nth_data(list, i); \ 451 item = item_convert_func; \ 452 PyList_SetItem(py_list, i, item); \ 454 if (glib_list_item_free != NULL) \ 455 prefix##_foreach(list, glib_list_item_free, NULL); \ 456 if (glib_list_free != NULL) \ 457 glib_list_free(list); \ 492 #define PYLIST_FROMGLIST(py_list,list,item_convert_func,list_free,\ 494 PYLIST_FROMGLIBLIST(GList,g_list,py_list,list,item_convert_func,\ 495 list_free,list_item_free) 529 #define PYLIST_FROMGSLIST(py_list,list,item_convert_func,list_free,\ 531 PYLIST_FROMGLIBLIST(GSList,g_slist,py_list,list,item_convert_func,\ 532 list_free,list_item_free) 548 #define PYLIST_ASGLIBLIST(type,prefix,py_list,list,check_func,\ 549 convert_func,child_free_func,errormsg,errorreturn) \ 552 Py_ssize_t i, n_list; \ 553 GFunc glib_child_free_func = (GFunc)child_free_func; \ 555 if (!(py_list = PySequence_Fast(py_list, ""))) { \ 557 return errorreturn; \ 559 n_list = PySequence_Fast_GET_SIZE(py_list); \ 560 for (i = 0; i < n_list; i++) { \ 561 PyObject *py_item = PySequence_Fast_GET_ITEM(py_list, i); \ 564 if (glib_child_free_func) \ 565 prefix##_foreach(list, glib_child_free_func, NULL); \ 566 prefix##_free(list); \ 567 Py_DECREF(py_list); \ 569 return errorreturn; \ 571 list = prefix##_prepend(list, convert_func); \ 573 Py_DECREF(py_list); \ 574 list = prefix##_reverse(list); \ 616 #define PYLIST_ASGLIST(py_list,list,check_func,convert_func,child_free_func,\ 617 errormsg,errorreturn) \ 618 PYLIST_ASGLIBLIST(GList,g_list,py_list,list,check_func,convert_func,\ 619 child_free_func,errormsg,errorreturn) 660 #define PYLIST_ASGSLIST(py_list,list,check_func,convert_func,child_free_func,\ 661 errormsg,errorreturn) \ 662 PYLIST_ASGLIBLIST(GSList,g_slist,py_list,list,check_func,convert_func,\ 663 child_free_func,errormsg,errorreturn) PyTypeObject *(* PyGTypeRegistrationFunction)(const gchar *name, gpointer data)
int(* PyGClassInitFunc)(gpointer gclass, PyTypeObject *pyclass)
PyTypeObject * pointer_type
PyGThreadBlockFunc block_threads
void(* PyGFatalExceptionFunc)(void)
PyClosureExceptionHandler exception_handler
PyObject_HEAD GParamSpec * pspec
G_BEGIN_DECLS typedef void(* PyClosureExceptionHandler)(GValue *ret, guint n_param_values, const GValue *params)
PyTypeObject * boxed_type
PyTypeObject * flags_type
struct _PyGObjectData PyGObjectData
struct _PyGObject_Functions * _PyGObject_API
PyTypeObject * paramspec_type
static PyObject * pygobject_init(int req_major, int req_minor, int req_micro)
pygobject_init: : minimum version major number, or -1 : minimum version minor number, or -1 : minimum version micro number, or -1
GDestroyNotify destroy_notify
PyObject_HEAD gpointer boxed
void(* PyGThreadBlockFunc)(void)
PyGThreadBlockFunc unblock_threads
PyObject_HEAD GObject * obj
PyObject_HEAD gpointer pointer