Debian Codegraph @

gi/pygobject.h

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2000 James Henstridge <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef __PYGOBJECT_H__
21 #define __PYGOBJECT_H__
22
23 #include <pygobject-types.h>
24
25 #ifdef _INSIDE_PYGOBJECT_
26 #error "pygobject.h exposes the external API."
27 #endif
28
29 G_BEGIN_DECLS
30
31 /* Deprecated, only available for API compatibility. */
32 #define pyg_threads_enabled TRUE
33 #define pyg_gil_state_ensure PyGILState_Ensure
34 #define pyg_gil_state_release PyGILState_Release
35 #define pyg_begin_allow_threads Py_BEGIN_ALLOW_THREADS
36 #define pyg_end_allow_threads Py_END_ALLOW_THREADS
37 #define pyg_enable_threads()
38 #define pyg_set_thread_block_funcs(a, b)
39 #define pyg_block_threads()
40 #define pyg_unblock_threads()
41
42
43 #if defined(NO_IMPORT) || defined(NO_IMPORT_PYGOBJECT)
44 extern struct _PyGObject_Functions *_PyGObject_API;
45 #else
46 struct _PyGObject_Functions *_PyGObject_API;
47 #endif
48
49 #define pygobject_register_class (_PyGObject_API->register_class)
50 #define pygobject_register_wrapper (_PyGObject_API->register_wrapper)
51 #define pygobject_lookup_class (_PyGObject_API->lookup_class)
52 #define pygobject_new (_PyGObject_API->newgobj)
53 #define pygobject_new_full (_PyGObject_API->newgobj_full)
54 #define PyGObject_Type (*_PyGObject_API->object_type)
55 #define pyg_closure_new (_PyGObject_API->closure_new)
56 #define pygobject_watch_closure (_PyGObject_API->object_watch_closure)
57 #define pyg_closure_set_exception_handler \
58 (_PyGObject_API->closure_set_exception_handler)
59 #define pyg_destroy_notify (_PyGObject_API->destroy_notify)
60 #define pyg_type_from_object_strict (_PyGObject_API->type_from_object_strict)
61 #define pyg_type_from_object (_PyGObject_API->type_from_object)
62 #define pyg_type_wrapper_new (_PyGObject_API->type_wrapper_new)
63 #define pyg_enum_get_value (_PyGObject_API->enum_get_value)
64 #define pyg_flags_get_value (_PyGObject_API->flags_get_value)
65 #define pyg_register_gtype_custom (_PyGObject_API->register_gtype_custom)
66 #define pyg_value_from_pyobject (_PyGObject_API->value_from_pyobject)
67 #define pyg_value_from_pyobject_with_error \
68 (_PyGObject_API->value_from_pyobject_with_error)
69 #define pyg_value_as_pyobject (_PyGObject_API->value_as_pyobject)
70 #define pyg_register_interface (_PyGObject_API->register_interface)
71 #define PyGBoxed_Type (*_PyGObject_API->boxed_type)
72 #define pyg_register_boxed (_PyGObject_API->register_boxed)
73 #define pyg_boxed_new (_PyGObject_API->boxed_new)
74 #define PyGPointer_Type (*_PyGObject_API->pointer_type)
75 #define pyg_register_pointer (_PyGObject_API->register_pointer)
76 #define pyg_pointer_new (_PyGObject_API->pointer_new)
77 #define pyg_enum_add_constants (_PyGObject_API->enum_add_constants)
78 #define pyg_flags_add_constants (_PyGObject_API->flags_add_constants)
79 #define pyg_constant_strip_prefix (_PyGObject_API->constant_strip_prefix)
80 #define pyg_error_check (_PyGObject_API->error_check)
81 #define PyGParamSpec_Type (*_PyGObject_API->paramspec_type)
82 #define pyg_param_spec_new (_PyGObject_API->paramspec_new)
83 #define pyg_param_spec_from_object (_PyGObject_API->paramspec_get)
84 #define pyg_pyobj_to_unichar_conv (_PyGObject_API->pyobj_to_unichar_conv)
85 #define pyg_parse_constructor_args (_PyGObject_API->parse_constructor_args)
86 #define pyg_param_gvalue_as_pyobject (_PyGObject_API->value_as_pyobject)
87 #define pyg_param_gvalue_from_pyobject \
88 (_PyGObject_API->gvalue_from_param_pyobject)
89 #define PyGEnum_Type (*_PyGObject_API->enum_type)
90 #define pyg_enum_add (_PyGObject_API->enum_add)
91 #define pyg_enum_from_gtype (_PyGObject_API->enum_from_gtype)
92 #define PyGFlags_Type (*_PyGObject_API->flags_type)
93 #define pyg_flags_add (_PyGObject_API->flags_add)
94 #define pyg_flags_from_gtype (_PyGObject_API->flags_from_gtype)
95 #define pyg_register_class_init (_PyGObject_API->register_class_init)
96 #define pyg_register_interface_info (_PyGObject_API->register_interface_info)
97 #define pyg_add_warning_redirection (_PyGObject_API->add_warning_redirection)
98 #define pyg_disable_warning_redirections \
99 (_PyGObject_API->disable_warning_redirections)
100 #define pyg_gerror_exception_check (_PyGObject_API->gerror_exception_check)
101
102
103 /**
104 * pygobject_init:
105 * @req_major: minimum version major number, or -1
106 * @req_minor: minimum version minor number, or -1
107 * @req_micro: minimum version micro number, or -1
108 *
109 * Imports and initializes the 'gobject' python module. Can
110 * optionally check for a required minimum version if @req_major,
111 * @req_minor, and @req_micro are all different from -1.
112 *
113 * Returns: a new reference to the gobject module on success, NULL in
114 * case of failure (and raises ImportError).
115 **/
116 static inline PyObject *
117 pygobject_init (int req_major, int req_minor, int req_micro)
118 {
119 PyObject *gobject, *cobject;
120
121 gobject = PyImport_ImportModule ("gi._gobject");
122 if (!gobject) {
123 if (PyErr_Occurred ()) {
124 PyObject *type, *value, *traceback;
125 PyObject *py_orig_exc;
126 PyErr_Fetch (&type, &value, &traceback);
127 py_orig_exc = PyObject_Repr (value);
128 Py_XDECREF (type);
129 Py_XDECREF (value);
130 Py_XDECREF (traceback);
131
132
133 #if PY_VERSION_HEX < 0x03000000
134 PyErr_Format (PyExc_ImportError,
135 "could not import gobject (error was: %s)",
136 PyString_AsString (py_orig_exc));
137 #else
138 {
139 /* Can not use PyErr_Format because it doesn't have
140 * a format string for dealing with PyUnicode objects
141 * like PyUnicode_FromFormat has
142 */
143 PyObject *errmsg = PyUnicode_FromFormat (
144 "could not import gobject (error was: %U)", py_orig_exc);
145
146 if (errmsg) {
147 PyErr_SetObject (PyExc_ImportError, errmsg);
148 Py_DECREF (errmsg);
149 }
150 /* if errmsg is NULL then we might have OOM
151 * PyErr should already be set and trying to
152 * return our own error would be futile
153 */
154 }
155 #endif
156 Py_DECREF (py_orig_exc);
157 } else {
158 PyErr_SetString (PyExc_ImportError,
159 "could not import gobject (no error given)");
160 }
161 return NULL;
162 }
163
164 cobject = PyObject_GetAttrString (gobject, "_PyGObject_API");
165 if (cobject && PyCapsule_CheckExact (cobject)) {
166 _PyGObject_API = (struct _PyGObject_Functions *)PyCapsule_GetPointer (
167 cobject, "gobject._PyGObject_API");
168 Py_DECREF (cobject);
169 } else {
170 PyErr_SetString (
171 PyExc_ImportError,
172 "could not import gobject (could not find _PyGObject_API object)");
173 Py_XDECREF (cobject);
174 Py_DECREF (gobject);
175 return NULL;
176 }
177
178 if (req_major != -1) {
179 int found_major, found_minor, found_micro;
180 PyObject *version;
181
182 version = PyObject_GetAttrString (gobject, "pygobject_version");
183 if (!version) {
184 PyErr_SetString (PyExc_ImportError,
185 "could not import gobject (version too old)");
186 Py_DECREF (gobject);
187 return NULL;
188 }
189 if (!PyArg_ParseTuple (version, "iii", &found_major, &found_minor,
190 &found_micro)) {
191 PyErr_SetString (
192 PyExc_ImportError,
193 "could not import gobject (version has invalid format)");
194 Py_DECREF (version);
195 Py_DECREF (gobject);
196 return NULL;
197 }
198 Py_DECREF (version);
199 if (req_major != found_major || req_minor > found_minor
200 || (req_minor == found_minor && req_micro > found_micro)) {
201 PyErr_Format (PyExc_ImportError,
202 "could not import gobject (version mismatch, "
203 "%d.%d.%d is required, "
204 "found %d.%d.%d)",
205 req_major, req_minor, req_micro, found_major,
206 found_minor, found_micro);
207 Py_DECREF (gobject);
208 return NULL;
209 }
210 }
211 return gobject;
212 }
213
214 /**
215 * PYLIST_FROMGLIBLIST:
216 * @type: the type of the GLib list e.g. #GList or #GSList
217 * @prefix: the prefix of functions that manipulate a list of the type
218 * given by type.
219 *
220 * A macro that creates a type specific code block which converts a GLib
221 * list (#GSList or #GList) to a Python list. The first two args of the macro
222 * are used to specify the type and list function prefix so that the type
223 * specific macros can be generated.
224 *
225 * The rest of the args are for the standard args for the type specific
226 * macro(s) created from this macro.
227 */
228 #define PYLIST_FROMGLIBLIST(type, prefix, py_list, list, item_convert_func, \
229 list_free, list_item_free) \
230 G_STMT_START \
231 { \
232 gint i, len; \
233 PyObject *item; \
234 void (*glib_list_free) (type *) = list_free; \
235 GFunc glib_list_item_free = (GFunc)list_item_free; \
236 \
237 len = prefix##_length (list); \
238 py_list = PyList_New (len); \
239 for (i = 0; i < len; i++) { \
240 gpointer list_item = prefix##_nth_data (list, i); \
241 \
242 item = item_convert_func; \
243 PyList_SetItem (py_list, i, item); \
244 } \
245 if (glib_list_item_free != NULL) \
246 prefix##_foreach (list, glib_list_item_free, NULL); \
247 if (glib_list_free != NULL) glib_list_free (list); \
248 } \
249 G_STMT_END
250
251 /**
252 * PYLIST_FROMGLIST:
253 * @py_list: the name of the Python list
254 *
255 * @list: the #GList to be converted to a Python list
256 *
257 * @item_convert_func: the function that converts a list item to a Python
258 * object. The function must refer to the list item using "@list_item" and
259 * must return a #PyObject* object. An example conversion function is:
260 * [[
261 * PyString_FromString(list_item)
262 * ]]
263 * A more elaborate function is:
264 * [[
265 * pyg_boxed_new(GTK_TYPE_RECENT_INFO, list_item, TRUE, TRUE)
266 * ]]
267 * @list_free: the name of a function that takes a single arg (the list) and
268 * frees its memory. Can be NULL if the list should not be freed. An example
269 * is:
270 * [[
271 * g_list_free
272 * ]]
273 * @list_item_free: the name of a #GFunc function that frees the memory used
274 * by the items in the list or %NULL if the list items do not have to be
275 * freed. A simple example is:
276 * [[
277 * g_free
278 * ]]
279 *
280 * A macro that adds code that converts a #GList to a Python list.
281 *
282 */
283 #define PYLIST_FROMGLIST(py_list, list, item_convert_func, list_free, \
284 list_item_free) \
285 PYLIST_FROMGLIBLIST (GList, g_list, py_list, list, item_convert_func, \
286 list_free, list_item_free)
287
288 /**
289 * PYLIST_FROMGSLIST:
290 * @py_list: the name of the Python list
291 *
292 * @list: the #GSList to be converted to a Python list
293 *
294 * @item_convert_func: the function that converts a list item to a Python
295 * object. The function must refer to the list item using "@list_item" and
296 * must return a #PyObject* object. An example conversion function is:
297 * [[
298 * PyString_FromString(list_item)
299 * ]]
300 * A more elaborate function is:
301 * [[
302 * pyg_boxed_new(GTK_TYPE_RECENT_INFO, list_item, TRUE, TRUE)
303 * ]]
304 * @list_free: the name of a function that takes a single arg (the list) and
305 * frees its memory. Can be %NULL if the list should not be freed. An example
306 * is:
307 * [[
308 * g_list_free
309 * ]]
310 * @list_item_free: the name of a #GFunc function that frees the memory used
311 * by the items in the list or %NULL if the list items do not have to be
312 * freed. A simple example is:
313 * [[
314 * g_free
315 * ]]
316 *
317 * A macro that adds code that converts a #GSList to a Python list.
318 *
319 */
320 #define PYLIST_FROMGSLIST(py_list, list, item_convert_func, list_free, \
321 list_item_free) \
322 PYLIST_FROMGLIBLIST (GSList, g_slist, py_list, list, item_convert_func, \
323 list_free, list_item_free)
324
325 /**
326 * PYLIST_ASGLIBLIST
327 * @type: the type of the GLib list e.g. GList or GSList
328 * @prefix: the prefix of functions that manipulate a list of the type
329 * given by type e.g. g_list or g_slist
330 *
331 * A macro that creates a type specific code block to be used to convert a
332 * Python list to a GLib list (GList or GSList). The first two args of the
333 * macro are used to specify the type and list function prefix so that the
334 * type specific macros can be generated.
335 *
336 * The rest of the args are for the standard args for the type specific
337 * macro(s) created from this macro.
338 */
339 #define PYLIST_ASGLIBLIST(type, prefix, py_list, list, check_func, \
340 convert_func, child_free_func, errormsg, \
341 errorreturn) \
342 G_STMT_START \
343 { \
344 Py_ssize_t i, n_list; \
345 GFunc glib_child_free_func = (GFunc)child_free_func; \
346 \
347 if (!(py_list = PySequence_Fast (py_list, ""))) { \
348 errormsg; \
349 return errorreturn; \
350 } \
351 n_list = PySequence_Fast_GET_SIZE (py_list); \
352 for (i = 0; i < n_list; i++) { \
353 PyObject *py_item = PySequence_Fast_GET_ITEM (py_list, i); \
354 \
355 if (!check_func) { \
356 if (glib_child_free_func) \
357 prefix##_foreach (list, glib_child_free_func, NULL); \
358 prefix##_free (list); \
359 Py_DECREF (py_list); \
360 errormsg; \
361 return errorreturn; \
362 } \
363 list = prefix##_prepend (list, convert_func); \
364 }; \
365 Py_DECREF (py_list); \
366 list = prefix##_reverse (list); \
367 } \
368 G_STMT_END
369 /**
370 * PYLIST_ASGLIST
371 * @py_list: the Python list to be converted
372 * @list: the #GList list to be converted
373 * @check_func: the expression that takes a #PyObject* arg (must be named
374 * @py_item) and returns an int value indicating if the Python object matches
375 * the required list item type (0 - %False or 1 - %True). An example is:
376 * [[
377 * (PyString_Check(py_item)||PyUnicode_Check(py_item))
378 * ]]
379 * @convert_func: the function that takes a #PyObject* arg (must be named
380 * py_item) and returns a pointer to the converted list object. An example
381 * is:
382 * [[
383 * pygobject_get(py_item)
384 * ]]
385 * @child_free_func: the name of a #GFunc function that frees a GLib list
386 * item or %NULL if the list item does not have to be freed. This function is
387 * used to help free the items in a partially created list if there is an
388 * error. An example is:
389 * [[
390 * g_free
391 * ]]
392 * @errormsg: a function that sets up a Python error message. An example is:
393 * [[
394 * PyErr_SetString(PyExc_TypeError, "strings must be a sequence of" "strings
395 * or unicode objects")
396 * ]]
397 * @errorreturn: the value to return if an error occurs, e.g.:
398 * [[
399 * %NULL
400 * ]]
401 *
402 * A macro that creates code that converts a Python list to a #GList. The
403 * returned list must be freed using the appropriate list free function when
404 * it's no longer needed. If an error occurs the child_free_func is used to
405 * release the memory used by the list items and then the list memory is
406 * freed.
407 */
408 #define PYLIST_ASGLIST(py_list, list, check_func, convert_func, \
409 child_free_func, errormsg, errorreturn) \
410 PYLIST_ASGLIBLIST (GList, g_list, py_list, list, check_func, \
411 convert_func, child_free_func, errormsg, errorreturn)
412
413 /**
414 * PYLIST_ASGSLIST
415 * @py_list: the Python list to be converted
416 * @list: the #GSList list to be converted
417 * @check_func: the expression that takes a #PyObject* arg (must be named
418 * @py_item) and returns an int value indicating if the Python object matches
419 * the required list item type (0 - %False or 1 - %True). An example is:
420 * [[
421 * (PyString_Check(py_item)||PyUnicode_Check(py_item))
422 * ]]
423 * @convert_func: the function that takes a #PyObject* arg (must be named
424 * py_item) and returns a pointer to the converted list object. An example
425 * is:
426 * [[
427 * pygobject_get(py_item)
428 * ]]
429 * @child_free_func: the name of a #GFunc function that frees a GLib list
430 * item or %NULL if the list item does not have to be freed. This function is
431 * used to help free the items in a partially created list if there is an
432 * error. An example is:
433 * [[
434 * g_free
435 * ]]
436 * @errormsg: a function that sets up a Python error message. An example is:
437 * [[
438 * PyErr_SetString(PyExc_TypeError, "strings must be a sequence of" "strings
439 * or unicode objects")
440 * ]]
441 * @errorreturn: the value to return if an error occurs, e.g.:
442 * [[
443 * %NULL
444 * ]]
445 *
446 * A macro that creates code that converts a Python list to a #GSList. The
447 * returned list must be freed using the appropriate list free function when
448 * it's no longer needed. If an error occurs the child_free_func is used to
449 * release the memory used by the list items and then the list memory is
450 * freed.
451 */
452 #define PYLIST_ASGSLIST(py_list, list, check_func, convert_func, \
453 child_free_func, errormsg, errorreturn) \
454 PYLIST_ASGLIBLIST (GSList, g_slist, py_list, list, check_func, \
455 convert_func, child_free_func, errormsg, errorreturn)
456
457 G_END_DECLS
458
459 #endif /* __PYGOBJECT_H__ */