1 /* -*- Mode: C; c-basic-offset: 4 -*- */
8 #include <glib-object.h>
12 /* PyGClosure is a _private_ structure */
13 typedef void (* PyClosureExceptionHandler
) (GValue
*ret
, guint n_param_values
, const GValue
*params
);
14 typedef struct _PyGClosure PyGClosure
;
15 typedef struct _PyGObjectData PyGObjectData
;
20 PyObject
*extra_args
; /* tuple of extra args to pass to callback */
21 PyObject
*swap_data
; /* other object for gtk_signal_connect__object */
22 PyClosureExceptionHandler exception_handler
;
26 PYGOBJECT_USING_TOGGLE_REF
= 1 << 0,
27 PYGOBJECT_IS_FLOATING_REF
= 1 << 1,
28 PYGOBJECT_GOBJECT_WAS_FLOATING
= 1 << 2
31 /* closures is just an alias for what is found in the
36 PyObject
*inst_dict
; /* the instance dictionary -- must be last */
37 PyObject
*weakreflist
; /* list of weak references */
40 /* using union to preserve ABI compatibility (structure size
43 GSList
*closures
; /* stale field; no longer updated DO-NOT-USE! */
49 #define pygobject_get(v) (((PyGObject *)(v))->obj)
50 #define pygobject_check(v,base) (PyObject_TypeCheck(v,base))
56 gboolean free_on_dealloc
;
59 #define pyg_boxed_get(v,t) ((t *)((PyGBoxed *)(v))->boxed)
60 #define pyg_boxed_get_ptr(v) (((PyGBoxed *)(v))->boxed)
61 #define pyg_boxed_set_ptr(v,p) (((PyGBoxed *)(v))->boxed = (gpointer)p)
62 #define pyg_boxed_check(v,typecode) (PyObject_TypeCheck(v, &PyGBoxed_Type) && ((PyGBoxed *)(v))->gtype == typecode)
70 #define pyg_pointer_get(v,t) ((t *)((PyGPointer *)(v))->pointer)
71 #define pyg_pointer_get_ptr(v) (((PyGPointer *)(v))->pointer)
72 #define pyg_pointer_set_ptr(v,p) (((PyGPointer *)(v))->pointer = (gpointer)p)
73 #define pyg_pointer_check(v,typecode) (PyObject_TypeCheck(v, &PyGPointer_Type) && ((PyGPointer *)(v))->gtype == typecode)
75 typedef void (*PyGFatalExceptionFunc
) (void);
76 typedef void (*PyGThreadBlockFunc
) (void);
83 #define pyg_param_spec_get(v) (((PyGParamSpec *)v)->pspec)
84 #define pyg_param_spec_set(v,p) (((PyGParamSpec *)v)->pspec = (GParamSpec*)p)
85 #define pyg_param_spec_check(v) (PyObject_TypeCheck(v, &PyGParamSpec_Type))
87 /* Deprecated in favor of lower case with underscore macros above. */
88 #define PyGParamSpec_Get pyg_param_spec_get
89 #define PyGParamSpec_Check pyg_param_spec_check
91 typedef int (*PyGClassInitFunc
) (gpointer gclass
, PyTypeObject
*pyclass
);
92 typedef PyTypeObject
* (*PyGTypeRegistrationFunction
) (const gchar
*name
,
95 struct _PyGObject_Functions
{
97 * All field names in here are considered private,
98 * use the macros below instead, which provides stability
100 void (* register_class
)(PyObject
*dict
, const gchar
*class_name
,
101 GType gtype
, PyTypeObject
*type
, PyObject
*bases
);
102 void (* register_wrapper
)(PyObject
*self
);
103 PyTypeObject
*(* lookup_class
)(GType type
);
104 PyObject
*(* newgobj
)(GObject
*obj
);
106 GClosure
*(* closure_new
)(PyObject
*callback
, PyObject
*extra_args
,
107 PyObject
*swap_data
);
108 void (* object_watch_closure
)(PyObject
*self
, GClosure
*closure
);
109 GDestroyNotify destroy_notify
;
111 GType (* type_from_object
)(PyObject
*obj
);
112 PyObject
*(* type_wrapper_new
)(GType type
);
114 gint (* enum_get_value
)(GType enum_type
, PyObject
*obj
, gint
*val
);
115 gint (* flags_get_value
)(GType flag_type
, PyObject
*obj
, guint
*val
);
116 void (* register_gtype_custom
)(GType gtype
,
117 PyObject
*(* from_func
)(const GValue
*value
),
118 int (* to_func
)(GValue
*value
, PyObject
*obj
));
119 int (* value_from_pyobject
)(GValue
*value
, PyObject
*obj
);
120 PyObject
*(* value_as_pyobject
)(const GValue
*value
, gboolean copy_boxed
);
122 void (* register_interface
)(PyObject
*dict
, const gchar
*class_name
,
123 GType gtype
, PyTypeObject
*type
);
125 PyTypeObject
*boxed_type
;
126 void (* register_boxed
)(PyObject
*dict
, const gchar
*class_name
,
127 GType boxed_type
, PyTypeObject
*type
);
128 PyObject
*(* boxed_new
)(GType boxed_type
, gpointer boxed
,
129 gboolean copy_boxed
, gboolean own_ref
);
131 PyTypeObject
*pointer_type
;
132 void (* register_pointer
)(PyObject
*dict
, const gchar
*class_name
,
133 GType pointer_type
, PyTypeObject
*type
);
134 PyObject
*(* pointer_new
)(GType boxed_type
, gpointer pointer
);
136 void (* enum_add_constants
)(PyObject
*module
, GType enum_type
,
137 const gchar
*strip_prefix
);
138 void (* flags_add_constants
)(PyObject
*module
, GType flags_type
,
139 const gchar
*strip_prefix
);
141 const gchar
*(* constant_strip_prefix
)(const gchar
*name
,
142 const gchar
*strip_prefix
);
144 gboolean (* error_check
)(GError
**error
);
146 /* hooks to register handlers for getting GDK threads to cooperate
147 * with python threading */
148 void (* set_thread_block_funcs
) (PyGThreadBlockFunc block_threads_func
,
149 PyGThreadBlockFunc unblock_threads_func
);
150 PyGThreadBlockFunc block_threads
;
151 PyGThreadBlockFunc unblock_threads
;
153 PyTypeObject
*paramspec_type
;
154 PyObject
*(* paramspec_new
)(GParamSpec
*spec
);
155 GParamSpec
*(*paramspec_get
)(PyObject
*tuple
);
156 int (*pyobj_to_unichar_conv
)(PyObject
*pyobj
, void* ptr
);
157 gboolean (*parse_constructor_args
)(GType obj_type
,
163 PyObject
*(* param_gvalue_as_pyobject
) (const GValue
* gvalue
,
165 const GParamSpec
* pspec
);
166 int (* gvalue_from_param_pyobject
) (GValue
* value
,
168 const GParamSpec
* pspec
);
169 PyTypeObject
*enum_type
;
170 PyObject
*(*enum_add
)(PyObject
*module
,
171 const char *type_name_
,
172 const char *strip_prefix
,
174 PyObject
* (*enum_from_gtype
)(GType gtype
, int value
);
176 PyTypeObject
*flags_type
;
177 PyObject
*(*flags_add
)(PyObject
*module
,
178 const char *type_name_
,
179 const char *strip_prefix
,
181 PyObject
* (*flags_from_gtype
)(GType gtype
, guint value
);
183 gboolean threads_enabled
;
184 int (*enable_threads
) (void);
186 int (*gil_state_ensure
) (void);
187 void (*gil_state_release
) (int flag
);
189 void (*register_class_init
) (GType gtype
, PyGClassInitFunc class_init
);
190 void (*register_interface_info
) (GType gtype
, const GInterfaceInfo
*info
);
191 void (*closure_set_exception_handler
) (GClosure
*closure
, PyClosureExceptionHandler handler
);
193 void (*add_warning_redirection
) (const char *domain
,
195 void (*disable_warning_redirections
) (void);
197 /* type_register_custom API now removed, but leave a pointer here to not
199 void *_type_register_custom
;
201 gboolean (*gerror_exception_check
) (GError
**error
);
202 PyObject
* (*option_group_new
) (GOptionGroup
*group
);
203 GType (* type_from_object_strict
) (PyObject
*obj
, gboolean strict
);
205 PyObject
*(* newgobj_full
)(GObject
*obj
, gboolean steal
, gpointer g_class
);
206 PyTypeObject
*object_type
;
207 int (* value_from_pyobject_with_error
)(GValue
*value
, PyObject
*obj
);
211 /* Deprecated, only available for API compatibility. */
212 #define pyg_threads_enabled TRUE
213 #define pyg_gil_state_ensure PyGILState_Ensure
214 #define pyg_gil_state_release PyGILState_Release
215 #define pyg_begin_allow_threads Py_BEGIN_ALLOW_THREADS
216 #define pyg_end_allow_threads Py_END_ALLOW_THREADS
217 #define pyg_enable_threads()
218 #define pyg_set_thread_block_funcs(a, b)
219 #define pyg_block_threads()
220 #define pyg_unblock_threads()
223 #ifndef _INSIDE_PYGOBJECT_
225 #if defined(NO_IMPORT) || defined(NO_IMPORT_PYGOBJECT)
226 extern struct _PyGObject_Functions
*_PyGObject_API
;
228 struct _PyGObject_Functions
*_PyGObject_API
;
231 #define pygobject_register_class (_PyGObject_API->register_class)
232 #define pygobject_register_wrapper (_PyGObject_API->register_wrapper)
233 #define pygobject_lookup_class (_PyGObject_API->lookup_class)
234 #define pygobject_new (_PyGObject_API->newgobj)
235 #define pygobject_new_full (_PyGObject_API->newgobj_full)
236 #define PyGObject_Type (*_PyGObject_API->object_type)
237 #define pyg_closure_new (_PyGObject_API->closure_new)
238 #define pygobject_watch_closure (_PyGObject_API->object_watch_closure)
239 #define pyg_closure_set_exception_handler (_PyGObject_API->closure_set_exception_handler)
240 #define pyg_destroy_notify (_PyGObject_API->destroy_notify)
241 #define pyg_type_from_object_strict (_PyGObject_API->type_from_object_strict)
242 #define pyg_type_from_object (_PyGObject_API->type_from_object)
243 #define pyg_type_wrapper_new (_PyGObject_API->type_wrapper_new)
244 #define pyg_enum_get_value (_PyGObject_API->enum_get_value)
245 #define pyg_flags_get_value (_PyGObject_API->flags_get_value)
246 #define pyg_register_gtype_custom (_PyGObject_API->register_gtype_custom)
247 #define pyg_value_from_pyobject (_PyGObject_API->value_from_pyobject)
248 #define pyg_value_from_pyobject_with_error (_PyGObject_API->value_from_pyobject_with_error)
249 #define pyg_value_as_pyobject (_PyGObject_API->value_as_pyobject)
250 #define pyg_register_interface (_PyGObject_API->register_interface)
251 #define PyGBoxed_Type (*_PyGObject_API->boxed_type)
252 #define pyg_register_boxed (_PyGObject_API->register_boxed)
253 #define pyg_boxed_new (_PyGObject_API->boxed_new)
254 #define PyGPointer_Type (*_PyGObject_API->pointer_type)
255 #define pyg_register_pointer (_PyGObject_API->register_pointer)
256 #define pyg_pointer_new (_PyGObject_API->pointer_new)
257 #define pyg_enum_add_constants (_PyGObject_API->enum_add_constants)
258 #define pyg_flags_add_constants (_PyGObject_API->flags_add_constants)
259 #define pyg_constant_strip_prefix (_PyGObject_API->constant_strip_prefix)
260 #define pyg_error_check (_PyGObject_API->error_check)
261 #define PyGParamSpec_Type (*_PyGObject_API->paramspec_type)
262 #define pyg_param_spec_new (_PyGObject_API->paramspec_new)
263 #define pyg_param_spec_from_object (_PyGObject_API->paramspec_get)
264 #define pyg_pyobj_to_unichar_conv (_PyGObject_API->pyobj_to_unichar_conv)
265 #define pyg_parse_constructor_args (_PyGObject_API->parse_constructor_args)
266 #define pyg_param_gvalue_as_pyobject (_PyGObject_API->value_as_pyobject)
267 #define pyg_param_gvalue_from_pyobject (_PyGObject_API->gvalue_from_param_pyobject)
268 #define PyGEnum_Type (*_PyGObject_API->enum_type)
269 #define pyg_enum_add (_PyGObject_API->enum_add)
270 #define pyg_enum_from_gtype (_PyGObject_API->enum_from_gtype)
271 #define PyGFlags_Type (*_PyGObject_API->flags_type)
272 #define pyg_flags_add (_PyGObject_API->flags_add)
273 #define pyg_flags_from_gtype (_PyGObject_API->flags_from_gtype)
274 #define pyg_register_class_init (_PyGObject_API->register_class_init)
275 #define pyg_register_interface_info (_PyGObject_API->register_interface_info)
276 #define pyg_add_warning_redirection (_PyGObject_API->add_warning_redirection)
277 #define pyg_disable_warning_redirections (_PyGObject_API->disable_warning_redirections)
278 #define pyg_gerror_exception_check (_PyGObject_API->gerror_exception_check)
279 #define pyg_option_group_new (_PyGObject_API->option_group_new)
284 * @req_major: minimum version major number, or -1
285 * @req_minor: minimum version minor number, or -1
286 * @req_micro: minimum version micro number, or -1
288 * Imports and initializes the 'gobject' python module. Can
289 * optionally check for a required minimum version if @req_major,
290 * @req_minor, and @req_micro are all different from -1.
292 * Returns: a new reference to the gobject module on success, NULL in
293 * case of failure (and raises ImportError).
295 static inline PyObject
*
296 pygobject_init(int req_major
, int req_minor
, int req_micro
)
298 PyObject
*gobject
, *cobject
;
300 gobject
= PyImport_ImportModule("gi._gobject");
302 if (PyErr_Occurred())
304 PyObject
*type
, *value
, *traceback
;
305 PyObject
*py_orig_exc
;
306 PyErr_Fetch(&type
, &value
, &traceback
);
307 py_orig_exc
= PyObject_Repr(value
);
310 Py_XDECREF(traceback
);
313 #if PY_VERSION_HEX < 0x03000000
314 PyErr_Format(PyExc_ImportError
,
315 "could not import gobject (error was: %s)",
316 PyString_AsString(py_orig_exc
));
319 /* Can not use PyErr_Format because it doesn't have
320 * a format string for dealing with PyUnicode objects
321 * like PyUnicode_FromFormat has
323 PyObject
*errmsg
= PyUnicode_FromFormat("could not import gobject (error was: %U)",
327 PyErr_SetObject(PyExc_ImportError
,
331 /* if errmsg is NULL then we might have OOM
332 * PyErr should already be set and trying to
333 * return our own error would be futile
337 Py_DECREF(py_orig_exc
);
339 PyErr_SetString(PyExc_ImportError
,
340 "could not import gobject (no error given)");
345 cobject
= PyObject_GetAttrString(gobject
, "_PyGObject_API");
346 if (cobject
&& PyCapsule_CheckExact(cobject
))
347 _PyGObject_API
= (struct _PyGObject_Functions
*) PyCapsule_GetPointer(cobject
, "gobject._PyGObject_API");
349 PyErr_SetString(PyExc_ImportError
,
350 "could not import gobject (could not find _PyGObject_API object)");
357 int found_major
, found_minor
, found_micro
;
360 version
= PyObject_GetAttrString(gobject
, "pygobject_version");
362 PyErr_SetString(PyExc_ImportError
,
363 "could not import gobject (version too old)");
367 if (!PyArg_ParseTuple(version
, "iii",
368 &found_major
, &found_minor
, &found_micro
)) {
369 PyErr_SetString(PyExc_ImportError
,
370 "could not import gobject (version has invalid format)");
376 if (req_major
!= found_major
||
377 req_minor
> found_minor
||
378 (req_minor
== found_minor
&& req_micro
> found_micro
)) {
379 PyErr_Format(PyExc_ImportError
,
380 "could not import gobject (version mismatch, %d.%d.%d is required, "
381 "found %d.%d.%d)", req_major
, req_minor
, req_micro
,
382 found_major
, found_minor
, found_micro
);
391 * PYLIST_FROMGLIBLIST:
392 * @type: the type of the GLib list e.g. #GList or #GSList
393 * @prefix: the prefix of functions that manipulate a list of the type
396 * A macro that creates a type specific code block which converts a GLib
397 * list (#GSList or #GList) to a Python list. The first two args of the macro
398 * are used to specify the type and list function prefix so that the type
399 * specific macros can be generated.
401 * The rest of the args are for the standard args for the type specific
402 * macro(s) created from this macro.
404 #define PYLIST_FROMGLIBLIST(type,prefix,py_list,list,item_convert_func,\
405 list_free,list_item_free) \
410 void (*glib_list_free)(type*) = list_free; \
411 GFunc glib_list_item_free = (GFunc)list_item_free; \
413 len = prefix##_length(list); \
414 py_list = PyList_New(len); \
415 for (i = 0; i < len; i++) { \
416 gpointer list_item = prefix##_nth_data(list, i); \
418 item = item_convert_func; \
419 PyList_SetItem(py_list, i, item); \
421 if (glib_list_item_free != NULL) \
422 prefix##_foreach(list, glib_list_item_free, NULL); \
423 if (glib_list_free != NULL) \
424 glib_list_free(list); \
429 * @py_list: the name of the Python list
431 * @list: the #GList to be converted to a Python list
433 * @item_convert_func: the function that converts a list item to a Python
434 * object. The function must refer to the list item using "@list_item" and
435 * must return a #PyObject* object. An example conversion function is:
437 * PyString_FromString(list_item)
439 * A more elaborate function is:
441 * pyg_boxed_new(GTK_TYPE_RECENT_INFO, list_item, TRUE, TRUE)
443 * @list_free: the name of a function that takes a single arg (the list) and
444 * frees its memory. Can be NULL if the list should not be freed. An example
449 * @list_item_free: the name of a #GFunc function that frees the memory used
450 * by the items in the list or %NULL if the list items do not have to be
451 * freed. A simple example is:
456 * A macro that adds code that converts a #GList to a Python list.
459 #define PYLIST_FROMGLIST(py_list,list,item_convert_func,list_free,\
461 PYLIST_FROMGLIBLIST(GList,g_list,py_list,list,item_convert_func,\
462 list_free,list_item_free)
466 * @py_list: the name of the Python list
468 * @list: the #GSList to be converted to a Python list
470 * @item_convert_func: the function that converts a list item to a Python
471 * object. The function must refer to the list item using "@list_item" and
472 * must return a #PyObject* object. An example conversion function is:
474 * PyString_FromString(list_item)
476 * A more elaborate function is:
478 * pyg_boxed_new(GTK_TYPE_RECENT_INFO, list_item, TRUE, TRUE)
480 * @list_free: the name of a function that takes a single arg (the list) and
481 * frees its memory. Can be %NULL if the list should not be freed. An example
486 * @list_item_free: the name of a #GFunc function that frees the memory used
487 * by the items in the list or %NULL if the list items do not have to be
488 * freed. A simple example is:
493 * A macro that adds code that converts a #GSList to a Python list.
496 #define PYLIST_FROMGSLIST(py_list,list,item_convert_func,list_free,\
498 PYLIST_FROMGLIBLIST(GSList,g_slist,py_list,list,item_convert_func,\
499 list_free,list_item_free)
503 * @type: the type of the GLib list e.g. GList or GSList
504 * @prefix: the prefix of functions that manipulate a list of the type
505 * given by type e.g. g_list or g_slist
507 * A macro that creates a type specific code block to be used to convert a
508 * Python list to a GLib list (GList or GSList). The first two args of the
509 * macro are used to specify the type and list function prefix so that the
510 * type specific macros can be generated.
512 * The rest of the args are for the standard args for the type specific
513 * macro(s) created from this macro.
515 #define PYLIST_ASGLIBLIST(type,prefix,py_list,list,check_func,\
516 convert_func,child_free_func,errormsg,errorreturn) \
519 Py_ssize_t i, n_list; \
520 GFunc glib_child_free_func = (GFunc)child_free_func; \
522 if (!(py_list = PySequence_Fast(py_list, ""))) { \
524 return errorreturn; \
526 n_list = PySequence_Fast_GET_SIZE(py_list); \
527 for (i = 0; i < n_list; i++) { \
528 PyObject *py_item = PySequence_Fast_GET_ITEM(py_list, i); \
531 if (glib_child_free_func) \
532 prefix##_foreach(list, glib_child_free_func, NULL); \
533 prefix##_free(list); \
534 Py_DECREF(py_list); \
536 return errorreturn; \
538 list = prefix##_prepend(list, convert_func); \
540 Py_DECREF(py_list); \
541 list = prefix##_reverse(list); \
546 * @py_list: the Python list to be converted
547 * @list: the #GList list to be converted
548 * @check_func: the expression that takes a #PyObject* arg (must be named
549 * @py_item) and returns an int value indicating if the Python object matches
550 * the required list item type (0 - %False or 1 - %True). An example is:
552 * (PyString_Check(py_item)||PyUnicode_Check(py_item))
554 * @convert_func: the function that takes a #PyObject* arg (must be named
555 * py_item) and returns a pointer to the converted list object. An example
558 * pygobject_get(py_item)
560 * @child_free_func: the name of a #GFunc function that frees a GLib list
561 * item or %NULL if the list item does not have to be freed. This function is
562 * used to help free the items in a partially created list if there is an
563 * error. An example is:
567 * @errormsg: a function that sets up a Python error message. An example is:
569 * PyErr_SetString(PyExc_TypeError, "strings must be a sequence of" "strings
570 * or unicode objects")
572 * @errorreturn: the value to return if an error occurs, e.g.:
577 * A macro that creates code that converts a Python list to a #GList. The
578 * returned list must be freed using the appropriate list free function when
579 * it's no longer needed. If an error occurs the child_free_func is used to
580 * release the memory used by the list items and then the list memory is
583 #define PYLIST_ASGLIST(py_list,list,check_func,convert_func,child_free_func,\
584 errormsg,errorreturn) \
585 PYLIST_ASGLIBLIST(GList,g_list,py_list,list,check_func,convert_func,\
586 child_free_func,errormsg,errorreturn)
590 * @py_list: the Python list to be converted
591 * @list: the #GSList list to be converted
592 * @check_func: the expression that takes a #PyObject* arg (must be named
593 * @py_item) and returns an int value indicating if the Python object matches
594 * the required list item type (0 - %False or 1 - %True). An example is:
596 * (PyString_Check(py_item)||PyUnicode_Check(py_item))
598 * @convert_func: the function that takes a #PyObject* arg (must be named
599 * py_item) and returns a pointer to the converted list object. An example
602 * pygobject_get(py_item)
604 * @child_free_func: the name of a #GFunc function that frees a GLib list
605 * item or %NULL if the list item does not have to be freed. This function is
606 * used to help free the items in a partially created list if there is an
607 * error. An example is:
611 * @errormsg: a function that sets up a Python error message. An example is:
613 * PyErr_SetString(PyExc_TypeError, "strings must be a sequence of" "strings
614 * or unicode objects")
616 * @errorreturn: the value to return if an error occurs, e.g.:
621 * A macro that creates code that converts a Python list to a #GSList. The
622 * returned list must be freed using the appropriate list free function when
623 * it's no longer needed. If an error occurs the child_free_func is used to
624 * release the memory used by the list items and then the list memory is
627 #define PYLIST_ASGSLIST(py_list,list,check_func,convert_func,child_free_func,\
628 errormsg,errorreturn) \
629 PYLIST_ASGLIBLIST(GSList,g_slist,py_list,list,check_func,convert_func,\
630 child_free_func,errormsg,errorreturn)
632 #endif /* !_INSIDE_PYGOBJECT_ */
636 #endif /* !_PYGOBJECT_H_ */