1 /* -*- Mode: C; c-basic-offset: 4 -*- */
2 #ifndef _PYGOBJECT_H_
3 #define _PYGOBJECT_H_
4 
5 #include <Python.h>
6 
7 #include <glib.h>
8 #include <glib-object.h>
9 
10 G_BEGIN_DECLS
11 
12 /* This is deprecated, don't use */
13 #define PYGIL_API_IS_BUGGY FALSE
14 
15   /* PyGClosure is a _private_ structure */
16 typedef void (* PyClosureExceptionHandler) (GValue *ret, guint n_param_values, const GValue *params);
17 typedef struct _PyGClosure PyGClosure;
18 typedef struct _PyGObjectData PyGObjectData;
19 
20 struct _PyGClosure {
21     GClosure closure;
22     PyObject *callback;
23     PyObject *extra_args; /* tuple of extra args to pass to callback */
24     PyObject *swap_data; /* other object for gtk_signal_connect__object */
25     PyClosureExceptionHandler exception_handler;
26 };
27 
28 typedef enum {
29     PYGOBJECT_USING_TOGGLE_REF = 1 << 0
30 } PyGObjectFlags;
31 
32   /* closures is just an alias for what is found in the
33    * PyGObjectData */
34 typedef struct {
35     PyObject_HEAD
36     GObject *obj;
37     PyObject *inst_dict; /* the instance dictionary -- must be last */
38     PyObject *weakreflist; /* list of weak references */
39 
40       /*< private >*/
41       /* using union to preserve ABI compatibility (structure size
42        * must not change) */
43     union {
44         GSList *closures; /* stale field; no longer updated DO-NOT-USE! */
45         PyGObjectFlags flags;
46     } private_flags;
47 
48 } PyGObject;
49 
50 #define pygobject_get(v) (((PyGObject *)(v))->obj)
51 #define pygobject_check(v,base) (PyObject_TypeCheck(v,base))
52 
53 typedef struct {
54     PyObject_HEAD
55     gpointer boxed;
56     GType gtype;
57     gboolean free_on_dealloc;
58 } PyGBoxed;
59 
60 #define pyg_boxed_get(v,t)      ((t *)((PyGBoxed *)(v))->boxed)
61 #define pyg_boxed_check(v,typecode) (PyObject_TypeCheck(v, &PyGBoxed_Type) && ((PyGBoxed *)(v))->gtype == typecode)
62 
63 typedef struct {
64     PyObject_HEAD
65     gpointer pointer;
66     GType gtype;
67 } PyGPointer;
68 
69 #define pyg_pointer_get(v,t)      ((t *)((PyGPointer *)(v))->pointer)
70 #define pyg_pointer_check(v,typecode) (PyObject_TypeCheck(v, &PyGPointer_Type) && ((PyGPointer *)(v))->gtype == typecode)
71 
72 typedef void (*PyGFatalExceptionFunc) (void);
73 typedef void (*PyGThreadBlockFunc) (void);
74 
75 typedef struct {
76     PyObject_HEAD
77     GParamSpec *pspec;
78 } PyGParamSpec;
79 
80 #define PyGParamSpec_Get(v) (((PyGParamSpec *)v)->pspec)
81 #define PyGParamSpec_Check(v) (PyObject_TypeCheck(v, &PyGParamSpec_Type))
82 
83 typedef int (*PyGClassInitFunc) (gpointer gclass, PyTypeObject *pyclass);
84 typedef PyTypeObject * (*PyGTypeRegistrationFunction) (const gchar *name,
85 						       gpointer data);
86 
87 struct _PyGObject_Functions {
88     /*
89      * All field names in here are considered private,
90      * use the macros below instead, which provides stability
91      */
92     void (* register_class)(PyObject *dict, const gchar *class_name,
93 			    GType gtype, PyTypeObject *type, PyObject *bases);
94     void (* register_wrapper)(PyObject *self);
95     void (* register_sinkfunc)(GType type,
96 			       void (* sinkfunc)(GObject *object));
97     PyTypeObject *(* lookup_class)(GType type);
98     PyObject *(* newgobj)(GObject *obj);
99 
100     GClosure *(* closure_new)(PyObject *callback, PyObject *extra_args,
101 			      PyObject *swap_data);
102     void      (* object_watch_closure)(PyObject *self, GClosure *closure);
103     GDestroyNotify destroy_notify;
104 
105     GType (* type_from_object)(PyObject *obj);
106     PyObject *(* type_wrapper_new)(GType type);
107 
108     gint (* enum_get_value)(GType enum_type, PyObject *obj, gint *val);
109     gint (* flags_get_value)(GType flag_type, PyObject *obj, gint *val);
110     void (* register_gtype_custom)(GType gtype,
111 			    PyObject *(* from_func)(const GValue *value),
112 			    int (* to_func)(GValue *value, PyObject *obj));
113     int (* value_from_pyobject)(GValue *value, PyObject *obj);
114     PyObject *(* value_as_pyobject)(const GValue *value, gboolean copy_boxed);
115 
116     void (* register_interface)(PyObject *dict, const gchar *class_name,
117 				GType gtype, PyTypeObject *type);
118 
119     PyTypeObject *boxed_type;
120     void (* register_boxed)(PyObject *dict, const gchar *class_name,
121 			    GType boxed_type, PyTypeObject *type);
122     PyObject *(* boxed_new)(GType boxed_type, gpointer boxed,
123 			    gboolean copy_boxed, gboolean own_ref);
124 
125     PyTypeObject *pointer_type;
126     void (* register_pointer)(PyObject *dict, const gchar *class_name,
127 			      GType pointer_type, PyTypeObject *type);
128     PyObject *(* pointer_new)(GType boxed_type, gpointer pointer);
129 
130     void (* enum_add_constants)(PyObject *module, GType enum_type,
131 				const gchar *strip_prefix);
132     void (* flags_add_constants)(PyObject *module, GType flags_type,
133 				 const gchar *strip_prefix);
134 
135     const gchar *(* constant_strip_prefix)(const gchar *name,
136 				     const gchar *strip_prefix);
137 
138     gboolean (* error_check)(GError **error);
139 
140     /* hooks to register handlers for getting GDK threads to cooperate
141      * with python threading */
142     void (* set_thread_block_funcs) (PyGThreadBlockFunc block_threads_func,
143 				     PyGThreadBlockFunc unblock_threads_func);
144     PyGThreadBlockFunc block_threads;
145     PyGThreadBlockFunc unblock_threads;
146     PyTypeObject *paramspec_type;
147     PyObject *(* paramspec_new)(GParamSpec *spec);
148     GParamSpec *(*paramspec_get)(PyObject *tuple);
149     int (*pyobj_to_unichar_conv)(PyObject *pyobj, void* ptr);
150     gboolean (*parse_constructor_args)(GType        obj_type,
151                                        char       **arg_names,
152                                        char       **prop_names,
153                                        GParameter  *params,
154                                        guint       *nparams,
155                                        PyObject   **py_args);
156     PyObject *(* param_gvalue_as_pyobject) (const GValue* gvalue,
157                                             gboolean copy_boxed,
158 					    const GParamSpec* pspec);
159     int (* gvalue_from_param_pyobject) (GValue* value,
160                                         PyObject* py_obj,
161 					const GParamSpec* pspec);
162     PyTypeObject *enum_type;
163     PyObject *(*enum_add)(PyObject *module,
164 			  const char *type_name_,
165 			  const char *strip_prefix,
166 			  GType gtype);
167     PyObject* (*enum_from_gtype)(GType gtype, int value);
168 
169     PyTypeObject *flags_type;
170     PyObject *(*flags_add)(PyObject *module,
171 			   const char *type_name_,
172 			   const char *strip_prefix,
173 			   GType gtype);
174     PyObject* (*flags_from_gtype)(GType gtype, int value);
175 
176     gboolean threads_enabled;
177     int       (*enable_threads) (void);
178 
179     int       (*gil_state_ensure) (void);
180     void      (*gil_state_release) (int flag);
181 
182     void      (*register_class_init) (GType gtype, PyGClassInitFunc class_init);
183     void      (*register_interface_info) (GType gtype, const GInterfaceInfo *info);
184     void      (*closure_set_exception_handler) (GClosure *closure, PyClosureExceptionHandler handler);
185     int       (*pygobject_constructv) (PyGObject  *self,
186                                        guint       n_parameters,
187                                        GParameter *parameters);
188     int       (*pygobject_construct) (PyGObject  *self,
189                                       const char *first_property_name,
190                                       ...);
191     void      (*set_object_has_new_constructor) (GType type);
192 
193     void      (*add_warning_redirection) (const char *domain,
194                                           PyObject   *warning);
195     void      (*disable_warning_redirections) (void);
196     void      (*type_register_custom)(const gchar *type_name,
197 				      PyGTypeRegistrationFunction callback,
198 				      gpointer data);
199     gboolean  (*gerror_exception_check) (GError **error);
200     PyObject* (*option_group_new) (GOptionGroup *group);
201     GType (* type_from_object_strict) (PyObject *obj, gboolean strict);
202 };
203 
204 #ifndef _INSIDE_PYGOBJECT_
205 
206 #if defined(NO_IMPORT) || defined(NO_IMPORT_PYGOBJECT)
207 extern struct _PyGObject_Functions *_PyGObject_API;
208 #else
209 struct _PyGObject_Functions *_PyGObject_API;
210 #endif
211 
212 #define pygobject_register_class    (_PyGObject_API->register_class)
213 #define pygobject_register_wrapper  (_PyGObject_API->register_wrapper)
214 /* This is deprecated, sinkfuncs are not needed anymore */
215 #define pygobject_register_sinkfunc (_PyGObject_API->register_sinkfunc)
216 #define pygobject_lookup_class      (_PyGObject_API->lookup_class)
217 #define pygobject_new               (_PyGObject_API->newgobj)
218 #define pyg_closure_new             (_PyGObject_API->closure_new)
219 #define pygobject_watch_closure     (_PyGObject_API->object_watch_closure)
220 #define pyg_closure_set_exception_handler (_PyGObject_API->closure_set_exception_handler)
221 #define pyg_destroy_notify          (_PyGObject_API->destroy_notify)
222 #define pyg_type_from_object_strict   (_PyGObject_API->type_from_object_strict)
223 #define pyg_type_from_object        (_PyGObject_API->type_from_object)
224 #define pyg_type_wrapper_new        (_PyGObject_API->type_wrapper_new)
225 #define pyg_enum_get_value          (_PyGObject_API->enum_get_value)
226 #define pyg_flags_get_value         (_PyGObject_API->flags_get_value)
227 /* This is deprecated, call pyg_register_gtype_custom directly instead */
228 #define pyg_register_boxed_custom   pyg_register_gtype_custom
229 #define pyg_register_gtype_custom   (_PyGObject_API->register_gtype_custom)
230 #define pyg_value_from_pyobject     (_PyGObject_API->value_from_pyobject)
231 #define pyg_value_as_pyobject       (_PyGObject_API->value_as_pyobject)
232 #define pyg_register_interface      (_PyGObject_API->register_interface)
233 #define PyGBoxed_Type               (*_PyGObject_API->boxed_type)
234 #define pyg_register_boxed          (_PyGObject_API->register_boxed)
235 #define pyg_boxed_new               (_PyGObject_API->boxed_new)
236 #define PyGPointer_Type             (*_PyGObject_API->pointer_type)
237 #define pyg_register_pointer        (_PyGObject_API->register_pointer)
238 #define pyg_pointer_new             (_PyGObject_API->pointer_new)
239 #define pyg_enum_add_constants      (_PyGObject_API->enum_add_constants)
240 #define pyg_flags_add_constants     (_PyGObject_API->flags_add_constants)
241 #define pyg_constant_strip_prefix   (_PyGObject_API->constant_strip_prefix)
242 #define pyg_error_check             (_PyGObject_API->error_check)
243 #define pyg_set_thread_block_funcs  (_PyGObject_API->set_thread_block_funcs)
244 #define PyGParamSpec_Type           (*_PyGObject_API->paramspec_type)
245 #define pyg_param_spec_new          (_PyGObject_API->paramspec_new)
246 #define pyg_param_spec_from_object  (_PyGObject_API->paramspec_get)
247 #define pyg_pyobj_to_unichar_conv   (_PyGObject_API->pyobj_to_unichar_conv)
248 #define pyg_parse_constructor_args  (_PyGObject_API->parse_constructor_args)
249 #define pyg_param_gvalue_as_pyobject   (_PyGObject_API->value_as_pyobject)
250 #define pyg_param_gvalue_from_pyobject (_PyGObject_API->gvalue_from_param_pyobject)
251 #define PyGEnum_Type                (*_PyGObject_API->enum_type)
252 #define pyg_enum_add                (_PyGObject_API->enum_add)
253 #define pyg_enum_from_gtype         (_PyGObject_API->enum_from_gtype)
254 #define PyGFlags_Type               (*_PyGObject_API->flags_type)
255 #define pyg_flags_add               (_PyGObject_API->flags_add)
256 #define pyg_flags_from_gtype        (_PyGObject_API->flags_from_gtype)
257 #define pyg_enable_threads          (_PyGObject_API->enable_threads)
258 #define pyg_gil_state_ensure        (_PyGObject_API->gil_state_ensure)
259 #define pyg_gil_state_release       (_PyGObject_API->gil_state_release)
260 #define pyg_register_class_init     (_PyGObject_API->register_class_init)
261 #define pyg_register_interface_info (_PyGObject_API->register_interface_info)
262 #define pygobject_construct         (_PyGObject_API->pygobject_construct)
263 #define pygobject_constructv        (_PyGObject_API->pygobject_constructv)
264 #define pyg_set_object_has_new_constructor (_PyGObject_API->set_object_has_new_constructor)
265 #define pyg_add_warning_redirection   (_PyGObject_API->add_warning_redirection)
266 #define pyg_disable_warning_redirections (_PyGObject_API->disable_warning_redirections)
267 #define pyg_type_register_custom_callback (_PyGObject_API->type_register_custom)
268 #define pyg_gerror_exception_check (_PyGObject_API->gerror_exception_check)
269 #define pyg_option_group_new       (_PyGObject_API->option_group_new)
270 
271 #define pyg_block_threads()   G_STMT_START {   \
272     if (_PyGObject_API->block_threads != NULL) \
273       (* _PyGObject_API->block_threads)();     \
274   } G_STMT_END
275 #define pyg_unblock_threads() G_STMT_START {     \
276     if (_PyGObject_API->unblock_threads != NULL) \
277       (* _PyGObject_API->unblock_threads)();     \
278   } G_STMT_END
279 
280 #define pyg_threads_enabled (_PyGObject_API->threads_enabled)
281 
282 #define pyg_begin_allow_threads                 \
283     G_STMT_START {                              \
284         PyThreadState *_save = NULL;            \
285         if (_PyGObject_API->threads_enabled)    \
286             _save = PyEval_SaveThread();
287 #define pyg_end_allow_threads                   \
288         if (_PyGObject_API->threads_enabled)    \
289             PyEval_RestoreThread(_save);        \
290     } G_STMT_END
291 
292 
293 /**
294  * pygobject_init:
295  * @req_major: minimum version major number, or -1
296  * @req_minor: minimum version minor number, or -1
297  * @req_micro: minimum version micro number, or -1
298  *
299  * Imports and initializes the 'gobject' python module.  Can
300  * optionally check for a required minimum version if @req_major,
301  * @req_minor, and @req_micro are all different from -1.
302  *
303  * Returns: a new reference to the gobject module on success, NULL in
304  * case of failure (and raises ImportError).
305  **/
306 static inline PyObject *
pygobject_init(int req_major,int req_minor,int req_micro)307 pygobject_init(int req_major, int req_minor, int req_micro)
308 {
309     PyObject *gobject, *cobject;
310 
311     gobject = PyImport_ImportModule("gobject");
312     if (!gobject) {
313         if (PyErr_Occurred())
314         {
315             PyObject *type, *value, *traceback;
316             PyObject *py_orig_exc;
317             PyErr_Fetch(&type, &value, &traceback);
318             py_orig_exc = PyObject_Repr(value);
319             Py_XDECREF(type);
320             Py_XDECREF(value);
321             Py_XDECREF(traceback);
322 
323 
324 #if PY_VERSION_HEX < 0x03000000
325             PyErr_Format(PyExc_ImportError,
326                          "could not import gobject (error was: %s)",
327                          PyString_AsString(py_orig_exc));
328 #else
329             {
330                 /* Can not use PyErr_Format because it doesn't have
331                  * a format string for dealing with PyUnicode objects
332                  * like PyUnicode_FromFormat has
333                  */
334                 PyObject *errmsg = PyUnicode_FromFormat("could not import gobject (error was: %U)",
335                                                         py_orig_exc);
336 
337                 if (errmsg) {
338                    PyErr_SetObject(PyExc_ImportError,
339                                    errmsg);
340                    Py_DECREF(errmsg);
341                 }
342                 /* if errmsg is NULL then we might have OOM
343                  * PyErr should already be set and trying to
344                  * return our own error would be futile
345                  */
346             }
347 #endif
348             Py_DECREF(py_orig_exc);
349         } else {
350             PyErr_SetString(PyExc_ImportError,
351                             "could not import gobject (no error given)");
352         }
353         return NULL;
354     }
355 
356     cobject = PyObject_GetAttrString(gobject, "_PyGObject_API");
357 #if PY_VERSION_HEX >= 0x03000000
358     if (cobject && PyCapsule_CheckExact(cobject))
359         _PyGObject_API = (struct _PyGObject_Functions *) PyCapsule_GetPointer(cobject, "gobject._PyGObject_API");
360 
361 #else
362     if (cobject && PyCObject_Check(cobject))
363         _PyGObject_API = (struct _PyGObject_Functions *) PyCObject_AsVoidPtr(cobject);
364 #endif
365     else {
366         PyErr_SetString(PyExc_ImportError,
367                         "could not import gobject (could not find _PyGObject_API object)");
368         Py_DECREF(gobject);
369         return NULL;
370     }
371 
372     if (req_major != -1)
373     {
374         int found_major, found_minor, found_micro;
375         PyObject *version;
376 
377         version = PyObject_GetAttrString(gobject, "pygobject_version");
378         if (!version)
379             version = PyObject_GetAttrString(gobject, "pygtk_version");
380         if (!version) {
381             PyErr_SetString(PyExc_ImportError,
382                             "could not import gobject (version too old)");
383             Py_DECREF(gobject);
384             return NULL;
385         }
386         if (!PyArg_ParseTuple(version, "iii",
387                               &found_major, &found_minor, &found_micro)) {
388             PyErr_SetString(PyExc_ImportError,
389                             "could not import gobject (version has invalid format)");
390             Py_DECREF(version);
391             Py_DECREF(gobject);
392             return NULL;
393         }
394         Py_DECREF(version);
395         if (req_major != found_major ||
396             req_minor >  found_minor ||
397             (req_minor == found_minor && req_micro > found_micro)) {
398             PyErr_Format(PyExc_ImportError,
399                          "could not import gobject (version mismatch, %d.%d.%d is required, "
400                          "found %d.%d.%d)", req_major, req_minor, req_micro,
401                          found_major, found_minor, found_micro);
402             Py_DECREF(gobject);
403             return NULL;
404         }
405     }
406     return gobject;
407 }
408 
409 /* deprecated macro, use pygobject_init() instead. */
410 #define init_pygobject() G_STMT_START {         \
411     if (!pygobject_init(-1, -1, -1))            \
412         return;                                 \
413 } G_STMT_END
414 
415 /* deprecated macro, use pygobject_init() instead. */
416 #define init_pygobject_check(req_major, req_minor, req_micro) G_STMT_START {    \
417     if (!pygobject_init(req_major, req_minor, req_micro))                       \
418         return;                                                                 \
419 } G_STMT_END
420 
421 /**
422  * PYLIST_FROMGLIBLIST:
423  * @type: the type of the GLib list e.g. #GList or #GSList
424  * @prefix: the prefix of functions that manipulate a list of the type
425  * given by type.
426  *
427  * A macro that creates a type specific code block which converts a GLib
428  * list (#GSList or #GList) to a Python list. The first two args of the macro
429  * are used to specify the type and list function prefix so that the type
430  * specific macros can be generated.
431  *
432  * The rest of the args are for the standard args for the type specific
433  * macro(s) created from this macro.
434  */
435  #define PYLIST_FROMGLIBLIST(type,prefix,py_list,list,item_convert_func,\
436                             list_free,list_item_free)  \
437 G_STMT_START \
438 { \
439     gint i, len; \
440     PyObject *item; \
441     void (*glib_list_free)(type*) = list_free; \
442     GFunc glib_list_item_free = (GFunc)list_item_free;  \
443  \
444     len = prefix##_length(list); \
445     py_list = PyList_New(len); \
446     for (i = 0; i < len; i++) { \
447         gpointer list_item = prefix##_nth_data(list, i); \
448  \
449         item = item_convert_func; \
450         PyList_SetItem(py_list, i, item); \
451     } \
452     if (glib_list_item_free != NULL) \
453         prefix##_foreach(list, glib_list_item_free, NULL); \
454     if (glib_list_free != NULL) \
455         glib_list_free(list); \
456 } G_STMT_END
457 
458 /**
459  * PYLIST_FROMGLIST:
460  * @py_list: the name of the Python list
461  *
462  * @list: the #GList to be converted to a Python list
463  *
464  * @item_convert_func: the function that converts a list item to a Python
465  * object. The function must refer to the list item using "@list_item" and
466  * must return a #PyObject* object. An example conversion function is:
467  * [[
468  * PyString_FromString(list_item)
469  * ]]
470  * A more elaborate function is:
471  * [[
472  * pyg_boxed_new(GTK_TYPE_RECENT_INFO, list_item, TRUE, TRUE)
473  * ]]
474  * @list_free: the name of a function that takes a single arg (the list) and
475  * frees its memory. Can be NULL if the list should not be freed. An example
476  * is:
477  * [[
478  * g_list_free
479  * ]]
480  * @list_item_free: the name of a #GFunc function that frees the memory used
481  * by the items in the list or %NULL if the list items do not have to be
482  * freed. A simple example is:
483  * [[
484  * g_free
485  * ]]
486  *
487  * A macro that adds code that converts a #GList to a Python list.
488  *
489  */
490 #define PYLIST_FROMGLIST(py_list,list,item_convert_func,list_free,\
491                          list_item_free) \
492         PYLIST_FROMGLIBLIST(GList,g_list,py_list,list,item_convert_func,\
493                             list_free,list_item_free)
494 
495 /**
496  * PYLIST_FROMGSLIST:
497  * @py_list: the name of the Python list
498  *
499  * @list: the #GSList to be converted to a Python list
500  *
501  * @item_convert_func: the function that converts a list item to a Python
502  * object. The function must refer to the list item using "@list_item" and
503  * must return a #PyObject* object. An example conversion function is:
504  * [[
505  * PyString_FromString(list_item)
506  * ]]
507  * A more elaborate function is:
508  * [[
509  * pyg_boxed_new(GTK_TYPE_RECENT_INFO, list_item, TRUE, TRUE)
510  * ]]
511  * @list_free: the name of a function that takes a single arg (the list) and
512  * frees its memory. Can be %NULL if the list should not be freed. An example
513  * is:
514  * [[
515  * g_list_free
516  * ]]
517  * @list_item_free: the name of a #GFunc function that frees the memory used
518  * by the items in the list or %NULL if the list items do not have to be
519  * freed. A simple example is:
520  * [[
521  * g_free
522  * ]]
523  *
524  * A macro that adds code that converts a #GSList to a Python list.
525  *
526  */
527 #define PYLIST_FROMGSLIST(py_list,list,item_convert_func,list_free,\
528                           list_item_free) \
529         PYLIST_FROMGLIBLIST(GSList,g_slist,py_list,list,item_convert_func,\
530                             list_free,list_item_free)
531 
532 /**
533  * PYLIST_ASGLIBLIST
534  * @type: the type of the GLib list e.g. GList or GSList
535  * @prefix: the prefix of functions that manipulate a list of the type
536  * given by type e.g. g_list or g_slist
537  *
538  * A macro that creates a type specific code block to be used to convert a
539  * Python list to a GLib list (GList or GSList). The first two args of the
540  * macro are used to specify the type and list function prefix so that the
541  * type specific macros can be generated.
542  *
543  * The rest of the args are for the standard args for the type specific
544  * macro(s) created from this macro.
545  */
546 #define PYLIST_ASGLIBLIST(type,prefix,py_list,list,check_func,\
547                            convert_func,child_free_func,errormsg,errorreturn) \
548 G_STMT_START \
549 { \
550     Py_ssize_t i, n_list; \
551     GFunc glib_child_free_func = (GFunc)child_free_func;        \
552  \
553     if (!(py_list = PySequence_Fast(py_list, ""))) { \
554         errormsg; \
555         return errorreturn; \
556     } \
557     n_list = PySequence_Fast_GET_SIZE(py_list); \
558     for (i = 0; i < n_list; i++) { \
559         PyObject *py_item = PySequence_Fast_GET_ITEM(py_list, i); \
560  \
561         if (!check_func) { \
562             if (glib_child_free_func) \
563                     prefix##_foreach(list, glib_child_free_func, NULL); \
564             prefix##_free(list); \
565             Py_DECREF(py_list); \
566             errormsg; \
567             return errorreturn; \
568         } \
569         list = prefix##_prepend(list, convert_func); \
570     }; \
571         Py_DECREF(py_list); \
572         list =  prefix##_reverse(list); \
573 } \
574 G_STMT_END
575 /**
576  * PYLIST_ASGLIST
577  * @py_list: the Python list to be converted
578  * @list: the #GList list to be converted
579  * @check_func: the expression that takes a #PyObject* arg (must be named
580  * @py_item) and returns an int value indicating if the Python object matches
581  * the required list item type (0 - %False or 1 - %True). An example is:
582  * [[
583  * (PyString_Check(py_item)||PyUnicode_Check(py_item))
584  * ]]
585  * @convert_func: the function that takes a #PyObject* arg (must be named
586  * py_item) and returns a pointer to the converted list object. An example
587  * is:
588  * [[
589  * pygobject_get(py_item)
590  * ]]
591  * @child_free_func: the name of a #GFunc function that frees a GLib list
592  * item or %NULL if the list item does not have to be freed. This function is
593  * used to help free the items in a partially created list if there is an
594  * error. An example is:
595  * [[
596  * g_free
597  * ]]
598  * @errormsg: a function that sets up a Python error message. An example is:
599  * [[
600  * PyErr_SetString(PyExc_TypeError, "strings must be a sequence of" "strings
601  * or unicode objects")
602  * ]]
603  * @errorreturn: the value to return if an error occurs, e.g.:
604  * [[
605  * %NULL
606  * ]]
607  *
608  * A macro that creates code that converts a Python list to a #GList. The
609  * returned list must be freed using the appropriate list free function when
610  * it's no longer needed. If an error occurs the child_free_func is used to
611  * release the memory used by the list items and then the list memory is
612  * freed.
613  */
614 #define PYLIST_ASGLIST(py_list,list,check_func,convert_func,child_free_func,\
615                        errormsg,errorreturn) \
616         PYLIST_ASGLIBLIST(GList,g_list,py_list,list,check_func,convert_func,\
617                           child_free_func,errormsg,errorreturn)
618 
619 /**
620  * PYLIST_ASGSLIST
621  * @py_list: the Python list to be converted
622  * @list: the #GSList list to be converted
623  * @check_func: the expression that takes a #PyObject* arg (must be named
624  * @py_item) and returns an int value indicating if the Python object matches
625  * the required list item type (0 - %False or 1 - %True). An example is:
626  * [[
627  * (PyString_Check(py_item)||PyUnicode_Check(py_item))
628  * ]]
629  * @convert_func: the function that takes a #PyObject* arg (must be named
630  * py_item) and returns a pointer to the converted list object. An example
631  * is:
632  * [[
633  * pygobject_get(py_item)
634  * ]]
635  * @child_free_func: the name of a #GFunc function that frees a GLib list
636  * item or %NULL if the list item does not have to be freed. This function is
637  * used to help free the items in a partially created list if there is an
638  * error. An example is:
639  * [[
640  * g_free
641  * ]]
642  * @errormsg: a function that sets up a Python error message. An example is:
643  * [[
644  * PyErr_SetString(PyExc_TypeError, "strings must be a sequence of" "strings
645  * or unicode objects")
646  * ]]
647  * @errorreturn: the value to return if an error occurs, e.g.:
648  * [[
649  * %NULL
650  * ]]
651  *
652  * A macro that creates code that converts a Python list to a #GSList. The
653  * returned list must be freed using the appropriate list free function when
654  * it's no longer needed. If an error occurs the child_free_func is used to
655  * release the memory used by the list items and then the list memory is
656  * freed.
657  */
658 #define PYLIST_ASGSLIST(py_list,list,check_func,convert_func,child_free_func,\
659                         errormsg,errorreturn) \
660         PYLIST_ASGLIBLIST(GSList,g_slist,py_list,list,check_func,convert_func,\
661                           child_free_func,errormsg,errorreturn)
662 
663 #endif /* !_INSIDE_PYGOBJECT_ */
664 
665 G_END_DECLS
666 
667 #endif /* !_PYGOBJECT_H_ */
668