Debian Codegraph @

gi/pygi-capi.c

raw
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2005-2009 Johan Dahlin <[email protected]>
5 *
6 * gimodule.c: wrapper for the gobject-introspection library.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 * USA
22 */
23
24 #include "pygenum.h"
25 #include "pygflags.h"
26 #include "pygboxed.h"
27 #include "pygi-basictype.h"
28 #include "pygi-error.h"
29 #include "pygi-type.h"
30 #include "pygi-util.h"
31 #include "pygi-value.h"
32 #include "pyginterface.h"
33 #include "pygobject-object.h"
34 #include "pygpointer.h"
35 #include "pygi-capi.h"
36
37
38 /**
39 * pyg_register_interface:
40 * @dict: a module dictionary.
41 * @class_name: the class name for the wrapper class.
42 * @gtype: the GType of the interface.
43 * @type: the wrapper class for the interface.
44 *
45 * Registers a Python class as the wrapper for a GInterface. As a
46 * convenience it will also place a reference to the wrapper class in
47 * the provided module dictionary.
48 */
49 static void
50 pyg_register_interface (PyObject *dict, const gchar *class_name, GType gtype,
51 PyTypeObject *type)
52 {
53 PyObject *o;
54
55 Py_SET_TYPE (type, &PyType_Type);
56 g_assert (Py_TYPE (&PyGInterface_Type) != NULL);
57 type->tp_base = &PyGInterface_Type;
58
59 if (PyType_Ready (type) < 0) {
60 g_warning ("could not ready `%s'", type->tp_name);
61 return;
62 }
63
64 if (gtype) {
65 o = pyg_type_wrapper_new (gtype);
66 PyDict_SetItemString (type->tp_dict, "__gtype__", o);
67 Py_DECREF (o);
68 }
69
70 g_type_set_qdata (gtype, pyginterface_type_key, type);
71
72 PyDict_SetItemString (dict, (char *)class_name, (PyObject *)type);
73 }
74
75 static void pyg_flags_add_constants (PyObject *module, GType flags_type,
76 const gchar *strip_prefix);
77
78 /**
79 * pyg_enum_add_constants:
80 * @module: a Python module
81 * @enum_type: the GType of the enumeration.
82 * @strip_prefix: the prefix to strip from the constant names.
83 *
84 * Adds constants to the given Python module for each value name of
85 * the enumeration. A prefix will be stripped from each enum name.
86 */
87 static void
88 pyg_enum_add_constants (PyObject *module, GType enum_type,
89 const gchar *strip_prefix)
90 {
91 GEnumClass *eclass;
92 guint i;
93
94 if (!G_TYPE_IS_ENUM (enum_type)) {
95 if (G_TYPE_IS_FLAGS (enum_type)) /* See bug #136204 */
96 pyg_flags_add_constants (module, enum_type, strip_prefix);
97 else
98 g_warning ("`%s' is not an enum type", g_type_name (enum_type));
99 return;
100 }
101 g_return_if_fail (strip_prefix != NULL);
102
103 eclass = G_ENUM_CLASS (g_type_class_ref (enum_type));
104
105 for (i = 0; i < eclass->n_values; i++) {
106 const gchar *name = eclass->values[i].value_name;
107 gint value = eclass->values[i].value;
108
109 PyModule_AddIntConstant (
110 module, (char *)pyg_constant_strip_prefix (name, strip_prefix),
111 (long)value);
112 }
113
114 g_type_class_unref (eclass);
115 }
116
117 /**
118 * pyg_flags_add_constants:
119 * @module: a Python module
120 * @flags_type: the GType of the flags type.
121 * @strip_prefix: the prefix to strip from the constant names.
122 *
123 * Adds constants to the given Python module for each value name of
124 * the flags set. A prefix will be stripped from each flag name.
125 */
126 static void
127 pyg_flags_add_constants (PyObject *module, GType flags_type,
128 const gchar *strip_prefix)
129 {
130 GFlagsClass *fclass;
131 guint i;
132
133 if (!G_TYPE_IS_FLAGS (flags_type)) {
134 if (G_TYPE_IS_ENUM (flags_type)) /* See bug #136204 */
135 pyg_enum_add_constants (module, flags_type, strip_prefix);
136 else
137 g_warning ("`%s' is not an flags type", g_type_name (flags_type));
138 return;
139 }
140 g_return_if_fail (strip_prefix != NULL);
141
142 fclass = G_FLAGS_CLASS (g_type_class_ref (flags_type));
143
144 for (i = 0; i < fclass->n_values; i++) {
145 const gchar *name = fclass->values[i].value_name;
146 guint value = fclass->values[i].value;
147
148 PyModule_AddIntConstant (
149 module, (char *)pyg_constant_strip_prefix (name, strip_prefix),
150 (long)value);
151 }
152
153 g_type_class_unref (fclass);
154 }
155
156 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
157
158 /**
159 * pyg_parse_constructor_args: helper function for PyGObject constructors
160 * @obj_type: GType of the GObject, for parameter introspection
161 * @arg_names: %NULL-terminated array of constructor argument names
162 * @prop_names: %NULL-terminated array of property names, with direct
163 * correspondence to @arg_names
164 * @params: GParameter array where parameters will be placed; length
165 * of this array must be at least equal to the number of
166 * arguments/properties
167 * @nparams: output parameter to contain actual number of arguments found
168 * @py_args: array of PyObject* containing the actual constructor arguments
169 *
170 * Parses an array of PyObject's and creates a GParameter array
171 *
172 * Return value: %TRUE if all is successful, otherwise %FALSE and
173 * python exception set.
174 **/
175 static gboolean
176 pyg_parse_constructor_args (GType obj_type, char **arg_names,
177 char **prop_names, GParameter *params,
178 guint *nparams, PyObject **py_args)
179 {
180 guint arg_i, param_i;
181 GObjectClass *oclass;
182
183 oclass = g_type_class_ref (obj_type);
184 g_return_val_if_fail (oclass, FALSE);
185
186 for (param_i = arg_i = 0; arg_names[arg_i]; ++arg_i) {
187 GParamSpec *spec;
188 if (!py_args[arg_i]) continue;
189 spec = g_object_class_find_property (oclass, prop_names[arg_i]);
190 params[param_i].name = prop_names[arg_i];
191 g_value_init (&params[param_i].value, spec->value_type);
192 if (pyg_value_from_pyobject (&params[param_i].value, py_args[arg_i])
193 == -1) {
194 guint i;
195 PyErr_Format (PyExc_TypeError,
196 "could not convert parameter '%s' of type '%s'",
197 arg_names[arg_i], g_type_name (spec->value_type));
198 g_type_class_unref (oclass);
199 for (i = 0; i < param_i; ++i) g_value_unset (&params[i].value);
200 return FALSE;
201 }
202 ++param_i;
203 }
204 g_type_class_unref (oclass);
205 *nparams = param_i;
206 return TRUE;
207 }
208
209 G_GNUC_END_IGNORE_DEPRECATIONS
210
211
212 static PyObject *
213 pyg_param_gvalue_as_pyobject (const GValue *gvalue, gboolean copy_boxed,
214 const GParamSpec *pspec)
215 {
216 if (G_IS_PARAM_SPEC_UNICHAR (pspec)) {
217 return pygi_gunichar_to_py (g_value_get_uint (gvalue));
218 } else {
219 return pyg_value_to_pyobject (gvalue, copy_boxed);
220 }
221 }
222
223 /* Only for backwards compatibility */
224 static int
225 pygobject_enable_threads (void)
226 {
227 return 0;
228 }
229
230 static int
231 pygobject_gil_state_ensure (void)
232 {
233 return PyGILState_Ensure ();
234 }
235
236 static void
237 pygobject_gil_state_release (int flag)
238 {
239 PyGILState_Release (flag);
240 }
241
242
243 static void
244 pyg_register_class_init (GType gtype, PyGClassInitFunc class_init)
245 {
246 GSList *list;
247
248 list = g_type_get_qdata (gtype, pygobject_class_init_key);
249 list = g_slist_prepend (list, class_init);
250 g_type_set_qdata (gtype, pygobject_class_init_key, list);
251 }
252
253
254 static GHashTable *log_handlers = NULL;
255 static gboolean log_handlers_disabled = FALSE;
256
257
258 static void
259 remove_handler (gpointer domain, gpointer handler, gpointer unused)
260 {
261 g_log_remove_handler (domain, GPOINTER_TO_UINT (handler));
262 }
263
264 static void
265 _log_func (const gchar *log_domain, GLogLevelFlags log_level,
266 const gchar *message, gpointer user_data)
267 {
268 if (G_LIKELY (Py_IsInitialized ())) {
269 PyGILState_STATE state;
270 PyObject *warning = user_data;
271
272 state = PyGILState_Ensure ();
273 PyErr_Warn (warning, (char *)message);
274 PyGILState_Release (state);
275 } else
276 g_log_default_handler (log_domain, log_level, message, user_data);
277 }
278
279 static void
280 add_warning_redirection (const char *domain, PyObject *warning)
281 {
282 g_return_if_fail (domain != NULL);
283 g_return_if_fail (warning != NULL);
284
285 if (!log_handlers_disabled) {
286 guint handler;
287 gpointer old_handler;
288
289 if (!log_handlers)
290 log_handlers =
291 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
292
293 if ((old_handler = g_hash_table_lookup (log_handlers, domain)))
294 g_log_remove_handler (domain, GPOINTER_TO_UINT (old_handler));
295
296 handler = g_log_set_handler (
297 domain, G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING, _log_func,
298 warning);
299 g_hash_table_insert (log_handlers, g_strdup (domain),
300 GUINT_TO_POINTER (handler));
301 }
302 }
303
304 static void
305 disable_warning_redirections (void)
306 {
307 log_handlers_disabled = TRUE;
308
309 if (log_handlers) {
310 g_hash_table_foreach (log_handlers, remove_handler, NULL);
311 g_hash_table_destroy (log_handlers);
312 log_handlers = NULL;
313 }
314 }
315
316 /**
317 * pyg_set_thread_block_funcs:
318 * Deprecated, only available for ABI compatibility.
319 */
320 static void
321 _pyg_set_thread_block_funcs (PyGThreadBlockFunc block_threads_func,
322 PyGThreadBlockFunc unblock_threads_func)
323 {
324 PyGILState_STATE state = PyGILState_Ensure ();
325 PyErr_Warn (PyExc_DeprecationWarning,
326 "Using pyg_set_thread_block_funcs is not longer needed. "
327 "PyGObject always uses Py_BLOCK/UNBLOCK_THREADS.");
328 PyGILState_Release (state);
329 }
330
331 static PyObject *
332 pyg_param_spec_new (GParamSpec *pspec)
333 {
334 PyErr_SetString (PyExc_NotImplementedError,
335 "Creating ParamSpecs through pyg_param_spec_new is no "
336 "longer supported");
337 return NULL;
338 }
339
340 static GParamSpec *
341 pyg_param_spec_from_object (PyObject *tuple)
342 {
343 PyErr_SetString (PyExc_NotImplementedError,
344 "Creating ParamSpecs through pyg_param_spec_from_object "
345 "is no longer supported");
346 return NULL;
347 }
348
349
350 /**
351 * pyg_pyobj_to_unichar_conv:
352 *
353 * Converts PyObject value to a unichar and write result to memory
354 * pointed to by ptr. Follows the calling convention of a ParseArgs
355 * converter (O& format specifier) so it may be used to convert function
356 * arguments.
357 *
358 * Returns: 1 if the conversion succeeds and 0 otherwise. If the conversion
359 * did not succeesd, a Python exception is raised
360 */
361 static int
362 pyg_pyobj_to_unichar_conv (PyObject *py_obj, void *ptr)
363 {
364 if (!pygi_gunichar_from_py (py_obj, ptr)) return 0;
365 return 1;
366 }
367
368 /**
369 * pyg_closure_set_exception_handler:
370 * @closure: a closure created with pyg_closure_new()
371 * @handler: the handler to call when an exception occurs or NULL for none
372 *
373 * Sets the handler to call when an exception occurs during closure invocation.
374 * The handler is responsible for providing a proper return value to the
375 * closure invocation. If @handler is %NULL, the default handler will be used.
376 * The default handler prints the exception to stderr and doesn't touch the
377 * closure's return value.
378 */
379 static void
380 pyg_closure_set_exception_handler (GClosure *closure,
381 PyClosureExceptionHandler handler)
382 {
383 PyGClosure *pygclosure;
384
385 g_return_if_fail (closure != NULL);
386
387 pygclosure = (PyGClosure *)closure;
388 pygclosure->exception_handler = handler;
389 }
390 const gpointer PyGParamSpec_Type_Stub = NULL;
391
392 struct _PyGObject_Functions pygobject_api_functions = {
393 pygobject_register_class,
394 pygobject_register_wrapper,
395 pygobject_lookup_class,
396 pygobject_new,
397
398 pyg_closure_new,
399 pygobject_watch_closure,
400 pyg_destroy_notify,
401
402 pyg_type_from_object,
403 pyg_type_wrapper_new,
404 pyg_enum_get_value,
405 pyg_flags_get_value,
406 pyg_register_gtype_custom,
407 pyg_value_from_pyobject,
408 pyg_value_to_pyobject,
409
410 pyg_register_interface,
411
412 &PyGBoxed_Type,
413 pygi_register_gboxed,
414 pygi_gboxed_new,
415
416 &PyGPointer_Type,
417 pyg_register_pointer,
418 pyg_pointer_new,
419
420 pyg_enum_add_constants,
421 pyg_flags_add_constants,
422
423 pyg_constant_strip_prefix,
424
425 pygi_error_check,
426
427 _pyg_set_thread_block_funcs,
428 (PyGThreadBlockFunc)0, /* block_threads */
429 (PyGThreadBlockFunc)0, /* unblock_threads */
430
431 (PyTypeObject *)&PyGParamSpec_Type_Stub,
432 pyg_param_spec_new,
433 pyg_param_spec_from_object,
434
435 pyg_pyobj_to_unichar_conv,
436 pyg_parse_constructor_args,
437 pyg_param_gvalue_as_pyobject,
438 pyg_param_gvalue_from_pyobject,
439
440 NULL /* PyGEnum_Type */,
441 pyg_enum_add,
442 pyg_enum_from_gtype,
443
444 NULL /* PyGFlags_Type */,
445 pyg_flags_add,
446 pyg_flags_from_gtype,
447
448 TRUE, /* threads_enabled */
449
450 pygobject_enable_threads,
451 pygobject_gil_state_ensure,
452 pygobject_gil_state_release,
453 pyg_register_class_init,
454 pyg_register_interface_info,
455
456 pyg_closure_set_exception_handler,
457
458 add_warning_redirection,
459 disable_warning_redirections,
460
461 NULL, /* previously type_register_custom */
462
463 pygi_gerror_exception_check,
464
465 NULL, /* previously pyg_option_group_new */
466 pyg_type_from_object_strict,
467
468 pygobject_new_full,
469 &PyGObject_Type,
470
471 pyg_value_from_pyobject_with_error
472 };
473
474 /**
475 * Returns 0 on success, or -1 and sets an exception.
476 */
477 int
478 pygi_register_warnings (PyObject *d)
479 {
480 PyObject *warning;
481
482 warning = PyErr_NewException ("gobject.Warning", PyExc_Warning, NULL);
483 if (warning == NULL) return -1;
484 PyDict_SetItemString (d, "Warning", warning);
485 add_warning_redirection ("GLib", warning);
486 add_warning_redirection ("GLib-GObject", warning);
487 add_warning_redirection ("GThread", warning);
488
489 return 0;
490 }
491
492 /**
493 * Returns 0 on success, or -1 and sets an exception.
494 */
495 int
496 pygi_register_api (PyObject *d)
497 {
498 PyObject *api;
499
500 pygobject_api_functions.enum_type = PyGEnum_Type;
501 pygobject_api_functions.flags_type = PyGFlags_Type;
502 api = PyCapsule_New (&pygobject_api_functions, "gobject._PyGObject_API",
503 NULL);
504 if (api == NULL) return -1;
505 PyDict_SetItemString (d, "_PyGObject_API", api);
506 Py_DECREF (api);
507 return 0;
508 }