| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* pygtk- Python bindings for the GTK toolkit. |
| 3 |
* Copyright (C) 1998-2003 James Henstridge |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "pygobject-object.h" |
| 20 |
|
| 21 |
#include "pygboxed.h" |
| 22 |
#include "pygenum.h" |
| 23 |
#include "pygflags.h" |
| 24 |
#include "pygi-basictype.h" |
| 25 |
#include "pygi-repository.h" |
| 26 |
#include "pygi-type.h" |
| 27 |
#include "pygi-util.h" |
| 28 |
#include "pygi-value.h" |
| 29 |
#include "pyginterface.h" |
| 30 |
#include "pygpointer.h" |
| 31 |
|
| 32 |
PyObject * |
| 33 |
pygi_type_import_by_name (const char *namespace_, const char *name) |
| 34 |
{ |
| 35 |
gchar *module_name; |
| 36 |
PyObject *py_module; |
| 37 |
PyObject *py_object; |
| 38 |
|
| 39 |
module_name = g_strconcat ("gi.repository.", namespace_, NULL); |
| 40 |
|
| 41 |
py_module = PyImport_ImportModule (module_name); |
| 42 |
|
| 43 |
g_free (module_name); |
| 44 |
|
| 45 |
if (py_module == NULL) { |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
|
| 49 |
py_object = PyObject_GetAttrString (py_module, name); |
| 50 |
|
| 51 |
Py_DECREF (py_module); |
| 52 |
|
| 53 |
return py_object; |
| 54 |
} |
| 55 |
|
| 56 |
PyObject * |
| 57 |
pygi_type_import_by_g_type (GType g_type) |
| 58 |
{ |
| 59 |
GIRepository *repository; |
| 60 |
GIBaseInfo *info; |
| 61 |
PyObject *type; |
| 62 |
|
| 63 |
repository = pygi_repository_get_default (); |
| 64 |
|
| 65 |
info = gi_repository_find_by_gtype (repository, g_type); |
| 66 |
if (info == NULL) { |
| 67 |
return NULL; |
| 68 |
} |
| 69 |
|
| 70 |
type = pygi_type_import_by_gi_info (info); |
| 71 |
gi_base_info_unref (info); |
| 72 |
|
| 73 |
return type; |
| 74 |
} |
| 75 |
|
| 76 |
PyObject * |
| 77 |
pygi_type_import_by_gi_info (GIBaseInfo *info) |
| 78 |
{ |
| 79 |
return pygi_type_import_by_name (gi_base_info_get_namespace (info), |
| 80 |
gi_base_info_get_name (info)); |
| 81 |
} |
| 82 |
|
| 83 |
PyObject * |
| 84 |
pygi_type_get_from_g_type (GType g_type) |
| 85 |
{ |
| 86 |
PyObject *py_g_type; |
| 87 |
PyObject *py_type; |
| 88 |
|
| 89 |
py_g_type = pyg_type_wrapper_new (g_type); |
| 90 |
if (py_g_type == NULL) { |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
py_type = PyObject_GetAttrString (py_g_type, "pytype"); |
| 95 |
if (Py_IsNone (py_type)) { |
| 96 |
py_type = pygi_type_import_by_g_type (g_type); |
| 97 |
} |
| 98 |
|
| 99 |
Py_DECREF (py_g_type); |
| 100 |
|
| 101 |
return py_type; |
| 102 |
} |
| 103 |
|
| 104 |
/* -------------- __gtype__ objects ---------------------------- */ |
| 105 |
|
| 106 |
typedef struct { |
| 107 |
PyObject_HEAD |
| 108 |
GType type; |
| 109 |
} PyGTypeWrapper; |
| 110 |
|
| 111 |
PYGI_DEFINE_TYPE ("gobject.GType", PyGTypeWrapper_Type, PyGTypeWrapper); |
| 112 |
|
| 113 |
static PyObject * |
| 114 |
generic_gsize_richcompare (gsize a, gsize b, int op) |
| 115 |
{ |
| 116 |
PyObject *res; |
| 117 |
|
| 118 |
switch (op) { |
| 119 |
case Py_EQ: |
| 120 |
res = (a == b) ? Py_True : Py_False; |
| 121 |
Py_INCREF (res); |
| 122 |
break; |
| 123 |
|
| 124 |
case Py_NE: |
| 125 |
res = (a != b) ? Py_True : Py_False; |
| 126 |
Py_INCREF (res); |
| 127 |
break; |
| 128 |
|
| 129 |
case Py_LT: |
| 130 |
res = (a < b) ? Py_True : Py_False; |
| 131 |
Py_INCREF (res); |
| 132 |
break; |
| 133 |
|
| 134 |
case Py_LE: |
| 135 |
res = (a <= b) ? Py_True : Py_False; |
| 136 |
Py_INCREF (res); |
| 137 |
break; |
| 138 |
|
| 139 |
case Py_GT: |
| 140 |
res = (a > b) ? Py_True : Py_False; |
| 141 |
Py_INCREF (res); |
| 142 |
break; |
| 143 |
|
| 144 |
case Py_GE: |
| 145 |
res = (a >= b) ? Py_True : Py_False; |
| 146 |
Py_INCREF (res); |
| 147 |
break; |
| 148 |
|
| 149 |
default: |
| 150 |
g_assert_not_reached (); |
| 151 |
} |
| 152 |
|
| 153 |
return res; |
| 154 |
} |
| 155 |
|
| 156 |
static PyObject * |
| 157 |
pyg_type_wrapper_richcompare (PyObject *self, PyObject *other, int op) |
| 158 |
{ |
| 159 |
if (Py_TYPE (self) == Py_TYPE (other) |
| 160 |
&& Py_TYPE (self) == &PyGTypeWrapper_Type) |
| 161 |
return generic_gsize_richcompare (((PyGTypeWrapper *)self)->type, |
| 162 |
((PyGTypeWrapper *)other)->type, op); |
| 163 |
else { |
| 164 |
Py_RETURN_NOTIMPLEMENTED; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
static long |
| 169 |
pyg_type_wrapper_hash (PyGTypeWrapper *self) |
| 170 |
{ |
| 171 |
return (long)self->type; |
| 172 |
} |
| 173 |
|
| 174 |
static PyObject * |
| 175 |
pyg_type_wrapper_repr (PyGTypeWrapper *self) |
| 176 |
{ |
| 177 |
char buf[80]; |
| 178 |
const gchar *name = g_type_name (self->type); |
| 179 |
|
| 180 |
g_snprintf (buf, sizeof (buf), "<GType %s (%lu)>", name ? name : "invalid", |
| 181 |
(unsigned long int)self->type); |
| 182 |
return PyUnicode_FromString (buf); |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
pyg_type_wrapper_dealloc (PyGTypeWrapper *self) |
| 187 |
{ |
| 188 |
PyObject_Free (self); |
| 189 |
} |
| 190 |
|
| 191 |
static GQuark |
| 192 |
_pyg_type_key (GType type) |
| 193 |
{ |
| 194 |
GQuark key; |
| 195 |
|
| 196 |
if (g_type_is_a (type, G_TYPE_INTERFACE)) { |
| 197 |
key = pyginterface_type_key; |
| 198 |
} else if (g_type_is_a (type, G_TYPE_ENUM)) { |
| 199 |
key = pygenum_class_key; |
| 200 |
} else if (g_type_is_a (type, G_TYPE_FLAGS)) { |
| 201 |
key = pygflags_class_key; |
| 202 |
} else if (g_type_is_a (type, G_TYPE_POINTER)) { |
| 203 |
key = pygpointer_class_key; |
| 204 |
} else if (g_type_is_a (type, G_TYPE_BOXED)) { |
| 205 |
key = pygboxed_type_key; |
| 206 |
} else { |
| 207 |
key = pygobject_class_key; |
| 208 |
} |
| 209 |
|
| 210 |
return key; |
| 211 |
} |
| 212 |
|
| 213 |
static PyObject * |
| 214 |
_wrap_g_type_wrapper__get_pytype (PyGTypeWrapper *self, void *closure) |
| 215 |
{ |
| 216 |
GQuark key; |
| 217 |
PyObject *py_type; |
| 218 |
|
| 219 |
key = _pyg_type_key (self->type); |
| 220 |
|
| 221 |
py_type = g_type_get_qdata (self->type, key); |
| 222 |
if (!py_type) py_type = Py_None; |
| 223 |
|
| 224 |
return Py_NewRef (py_type); |
| 225 |
} |
| 226 |
|
| 227 |
static int |
| 228 |
_wrap_g_type_wrapper__set_pytype (PyGTypeWrapper *self, PyObject *value, |
| 229 |
void *closure) |
| 230 |
{ |
| 231 |
GQuark key; |
| 232 |
PyObject *py_type; |
| 233 |
|
| 234 |
key = _pyg_type_key (self->type); |
| 235 |
|
| 236 |
py_type = g_type_get_qdata (self->type, key); |
| 237 |
Py_CLEAR (py_type); |
| 238 |
if (Py_IsNone (value)) |
| 239 |
g_type_set_qdata (self->type, key, NULL); |
| 240 |
else if (PyType_Check (value)) { |
| 241 |
Py_INCREF (value); |
| 242 |
g_type_set_qdata (self->type, key, value); |
| 243 |
} else { |
| 244 |
PyErr_SetString (PyExc_TypeError, |
| 245 |
"Value must be None or a type object"); |
| 246 |
return -1; |
| 247 |
} |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
static PyObject * |
| 253 |
_wrap_g_type_wrapper__get_name (PyGTypeWrapper *self, void *closure) |
| 254 |
{ |
| 255 |
const char *name = g_type_name (self->type); |
| 256 |
return PyUnicode_FromString (name ? name : "invalid"); |
| 257 |
} |
| 258 |
|
| 259 |
static PyObject * |
| 260 |
_wrap_g_type_wrapper__get_parent (PyGTypeWrapper *self, void *closure) |
| 261 |
{ |
| 262 |
return pyg_type_wrapper_new (g_type_parent (self->type)); |
| 263 |
} |
| 264 |
|
| 265 |
static PyObject * |
| 266 |
_wrap_g_type_wrapper__get_fundamental (PyGTypeWrapper *self, void *closure) |
| 267 |
{ |
| 268 |
return pyg_type_wrapper_new (g_type_fundamental (self->type)); |
| 269 |
} |
| 270 |
|
| 271 |
static PyObject * |
| 272 |
_wrap_g_type_wrapper__get_children (PyGTypeWrapper *self, void *closure) |
| 273 |
{ |
| 274 |
guint n_children, i; |
| 275 |
GType *children; |
| 276 |
PyObject *retval; |
| 277 |
|
| 278 |
children = g_type_children (self->type, &n_children); |
| 279 |
|
| 280 |
retval = PyList_New (n_children); |
| 281 |
for (i = 0; i < n_children; i++) |
| 282 |
PyList_SetItem (retval, i, pyg_type_wrapper_new (children[i])); |
| 283 |
g_free (children); |
| 284 |
|
| 285 |
return retval; |
| 286 |
} |
| 287 |
|
| 288 |
static PyObject * |
| 289 |
_wrap_g_type_wrapper__get_interfaces (PyGTypeWrapper *self, void *closure) |
| 290 |
{ |
| 291 |
guint n_interfaces, i; |
| 292 |
GType *interfaces; |
| 293 |
PyObject *retval; |
| 294 |
|
| 295 |
interfaces = g_type_interfaces (self->type, &n_interfaces); |
| 296 |
|
| 297 |
retval = PyList_New (n_interfaces); |
| 298 |
for (i = 0; i < n_interfaces; i++) |
| 299 |
PyList_SetItem (retval, i, pyg_type_wrapper_new (interfaces[i])); |
| 300 |
g_free (interfaces); |
| 301 |
|
| 302 |
return retval; |
| 303 |
} |
| 304 |
|
| 305 |
static PyObject * |
| 306 |
_wrap_g_type_wrapper__get_depth (PyGTypeWrapper *self, void *closure) |
| 307 |
{ |
| 308 |
return pygi_guint_to_py (g_type_depth (self->type)); |
| 309 |
} |
| 310 |
|
| 311 |
static PyGetSetDef _PyGTypeWrapper_getsets[] = { |
| 312 |
{ "pytype", (getter)_wrap_g_type_wrapper__get_pytype, |
| 313 |
(setter)_wrap_g_type_wrapper__set_pytype }, |
| 314 |
{ "name", (getter)_wrap_g_type_wrapper__get_name, (setter)0 }, |
| 315 |
{ "fundamental", (getter)_wrap_g_type_wrapper__get_fundamental, |
| 316 |
(setter)0 }, |
| 317 |
{ "parent", (getter)_wrap_g_type_wrapper__get_parent, (setter)0 }, |
| 318 |
{ "children", (getter)_wrap_g_type_wrapper__get_children, (setter)0 }, |
| 319 |
{ "interfaces", (getter)_wrap_g_type_wrapper__get_interfaces, (setter)0 }, |
| 320 |
{ "depth", (getter)_wrap_g_type_wrapper__get_depth, (setter)0 }, |
| 321 |
{ NULL, (getter)0, (setter)0 }, |
| 322 |
}; |
| 323 |
|
| 324 |
static PyObject * |
| 325 |
_wrap_g_type_is_interface (PyGTypeWrapper *self) |
| 326 |
{ |
| 327 |
return pygi_gboolean_to_py (G_TYPE_IS_INTERFACE (self->type)); |
| 328 |
} |
| 329 |
|
| 330 |
static PyObject * |
| 331 |
_wrap_g_type_is_classed (PyGTypeWrapper *self) |
| 332 |
{ |
| 333 |
return pygi_gboolean_to_py (G_TYPE_IS_CLASSED (self->type)); |
| 334 |
} |
| 335 |
|
| 336 |
static PyObject * |
| 337 |
_wrap_g_type_is_instantiatable (PyGTypeWrapper *self) |
| 338 |
{ |
| 339 |
return pygi_gboolean_to_py (G_TYPE_IS_INSTANTIATABLE (self->type)); |
| 340 |
} |
| 341 |
|
| 342 |
static PyObject * |
| 343 |
_wrap_g_type_is_derivable (PyGTypeWrapper *self) |
| 344 |
{ |
| 345 |
return pygi_gboolean_to_py (G_TYPE_IS_DERIVABLE (self->type)); |
| 346 |
} |
| 347 |
|
| 348 |
static PyObject * |
| 349 |
_wrap_g_type_is_deep_derivable (PyGTypeWrapper *self) |
| 350 |
{ |
| 351 |
return pygi_gboolean_to_py (G_TYPE_IS_DEEP_DERIVABLE (self->type)); |
| 352 |
} |
| 353 |
|
| 354 |
static PyObject * |
| 355 |
_wrap_g_type_is_abstract (PyGTypeWrapper *self) |
| 356 |
{ |
| 357 |
return pygi_gboolean_to_py (G_TYPE_IS_ABSTRACT (self->type)); |
| 358 |
} |
| 359 |
|
| 360 |
static PyObject * |
| 361 |
_wrap_g_type_is_value_abstract (PyGTypeWrapper *self) |
| 362 |
{ |
| 363 |
return pygi_gboolean_to_py (G_TYPE_IS_VALUE_ABSTRACT (self->type)); |
| 364 |
} |
| 365 |
|
| 366 |
static PyObject * |
| 367 |
_wrap_g_type_is_value_type (PyGTypeWrapper *self) |
| 368 |
{ |
| 369 |
return pygi_gboolean_to_py (G_TYPE_IS_VALUE_TYPE (self->type)); |
| 370 |
} |
| 371 |
|
| 372 |
static PyObject * |
| 373 |
_wrap_g_type_has_value_table (PyGTypeWrapper *self) |
| 374 |
{ |
| 375 |
return pygi_gboolean_to_py (G_TYPE_HAS_VALUE_TABLE (self->type)); |
| 376 |
} |
| 377 |
|
| 378 |
static PyObject * |
| 379 |
_wrap_g_type_from_name (PyGTypeWrapper *_, PyObject *args) |
| 380 |
{ |
| 381 |
char *type_name; |
| 382 |
GType type; |
| 383 |
|
| 384 |
if (!PyArg_ParseTuple (args, "s:GType.from_name", &type_name)) return NULL; |
| 385 |
|
| 386 |
type = g_type_from_name (type_name); |
| 387 |
if (type == 0) { |
| 388 |
PyErr_SetString (PyExc_RuntimeError, "unknown type name"); |
| 389 |
return NULL; |
| 390 |
} |
| 391 |
|
| 392 |
return pyg_type_wrapper_new (type); |
| 393 |
} |
| 394 |
|
| 395 |
static PyObject * |
| 396 |
_wrap_g_type_is_a (PyGTypeWrapper *self, PyObject *args) |
| 397 |
{ |
| 398 |
PyObject *gparent; |
| 399 |
GType parent; |
| 400 |
|
| 401 |
if (!PyArg_ParseTuple (args, "O:GType.is_a", &gparent)) |
| 402 |
return NULL; |
| 403 |
else if ((parent = pyg_type_from_object (gparent)) == 0) |
| 404 |
return NULL; |
| 405 |
|
| 406 |
return pygi_gboolean_to_py (g_type_is_a (self->type, parent)); |
| 407 |
} |
| 408 |
|
| 409 |
static PyMethodDef _PyGTypeWrapper_methods[] = { |
| 410 |
{ "is_interface", (PyCFunction)_wrap_g_type_is_interface, METH_NOARGS }, |
| 411 |
{ "is_classed", (PyCFunction)_wrap_g_type_is_classed, METH_NOARGS }, |
| 412 |
{ "is_instantiatable", (PyCFunction)_wrap_g_type_is_instantiatable, |
| 413 |
METH_NOARGS }, |
| 414 |
{ "is_derivable", (PyCFunction)_wrap_g_type_is_derivable, METH_NOARGS }, |
| 415 |
{ "is_deep_derivable", (PyCFunction)_wrap_g_type_is_deep_derivable, |
| 416 |
METH_NOARGS }, |
| 417 |
{ "is_abstract", (PyCFunction)_wrap_g_type_is_abstract, METH_NOARGS }, |
| 418 |
{ "is_value_abstract", (PyCFunction)_wrap_g_type_is_value_abstract, |
| 419 |
METH_NOARGS }, |
| 420 |
{ "is_value_type", (PyCFunction)_wrap_g_type_is_value_type, METH_NOARGS }, |
| 421 |
{ "has_value_table", (PyCFunction)_wrap_g_type_has_value_table, |
| 422 |
METH_NOARGS }, |
| 423 |
{ "from_name", (PyCFunction)_wrap_g_type_from_name, |
| 424 |
METH_VARARGS | METH_STATIC }, |
| 425 |
{ "is_a", (PyCFunction)_wrap_g_type_is_a, METH_VARARGS }, |
| 426 |
{ NULL, 0, 0 }, |
| 427 |
}; |
| 428 |
|
| 429 |
static int |
| 430 |
pyg_type_wrapper_init (PyGTypeWrapper *self, PyObject *args, PyObject *kwargs) |
| 431 |
{ |
| 432 |
static char *kwlist[] = { "object", NULL }; |
| 433 |
PyObject *py_object; |
| 434 |
GType type; |
| 435 |
|
| 436 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "O:GType.__init__", kwlist, |
| 437 |
&py_object)) |
| 438 |
return -1; |
| 439 |
|
| 440 |
if (!(type = pyg_type_from_object (py_object))) return -1; |
| 441 |
|
| 442 |
self->type = type; |
| 443 |
|
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* pyg_type_wrapper_new: |
| 449 |
* type: a GType |
| 450 |
* |
| 451 |
* Creates a Python wrapper for a GType. |
| 452 |
* |
| 453 |
* Returns: the Python wrapper. |
| 454 |
*/ |
| 455 |
PyObject * |
| 456 |
pyg_type_wrapper_new (GType type) |
| 457 |
{ |
| 458 |
PyGTypeWrapper *self; |
| 459 |
|
| 460 |
g_assert (Py_TYPE (&PyGTypeWrapper_Type) != NULL); |
| 461 |
self = |
| 462 |
(PyGTypeWrapper *)PyObject_New (PyGTypeWrapper, &PyGTypeWrapper_Type); |
| 463 |
if (self == NULL) return NULL; |
| 464 |
|
| 465 |
self->type = type; |
| 466 |
return (PyObject *)self; |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* pyg_type_from_object_strict: |
| 471 |
* obj: a Python object |
| 472 |
* strict: if set to TRUE, raises an exception if it can't perform the |
| 473 |
* conversion |
| 474 |
* |
| 475 |
* converts a python object to a GType. If strict is set, raises an |
| 476 |
* exception if it can't perform the conversion, otherwise returns |
| 477 |
* PY_TYPE_OBJECT. |
| 478 |
* |
| 479 |
* Returns: the corresponding GType, or 0 on error. |
| 480 |
*/ |
| 481 |
|
| 482 |
GType |
| 483 |
pyg_type_from_object_strict (PyObject *obj, gboolean strict) |
| 484 |
{ |
| 485 |
PyObject *gtype; |
| 486 |
GType type; |
| 487 |
|
| 488 |
/* NULL check */ |
| 489 |
if (!obj) { |
| 490 |
PyErr_SetString (PyExc_TypeError, "can't get type from NULL object"); |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
/* map some standard types to primitive GTypes ... */ |
| 495 |
if (Py_IsNone (obj)) return G_TYPE_NONE; |
| 496 |
if (PyType_Check (obj)) { |
| 497 |
PyTypeObject *tp = (PyTypeObject *)obj; |
| 498 |
|
| 499 |
if (tp == &PyLong_Type) |
| 500 |
return G_TYPE_INT; |
| 501 |
else if (tp == &PyBool_Type) |
| 502 |
return G_TYPE_BOOLEAN; |
| 503 |
else if (tp == &PyFloat_Type) |
| 504 |
return G_TYPE_DOUBLE; |
| 505 |
else if (tp == &PyUnicode_Type) |
| 506 |
return G_TYPE_STRING; |
| 507 |
else if (tp == &PyBaseObject_Type) |
| 508 |
return PY_TYPE_OBJECT; |
| 509 |
} |
| 510 |
|
| 511 |
if (Py_TYPE (obj) == &PyGTypeWrapper_Type) { |
| 512 |
return ((PyGTypeWrapper *)obj)->type; |
| 513 |
} |
| 514 |
|
| 515 |
/* handle strings */ |
| 516 |
if (PyUnicode_Check (obj)) { |
| 517 |
gchar *name = PyUnicode_AsUTF8 (obj); |
| 518 |
|
| 519 |
type = g_type_from_name (name); |
| 520 |
if (type != 0) { |
| 521 |
return type; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
/* finally, look for a __gtype__ attribute on the object */ |
| 526 |
gtype = PyObject_GetAttrString (obj, "__gtype__"); |
| 527 |
|
| 528 |
if (gtype) { |
| 529 |
if (Py_TYPE (gtype) == &PyGTypeWrapper_Type) { |
| 530 |
type = ((PyGTypeWrapper *)gtype)->type; |
| 531 |
Py_DECREF (gtype); |
| 532 |
return type; |
| 533 |
} |
| 534 |
Py_DECREF (gtype); |
| 535 |
} |
| 536 |
|
| 537 |
PyErr_Clear (); |
| 538 |
|
| 539 |
/* Some API like those that take GValues can hold a python object as |
| 540 |
* a pointer. This is potentially dangerous becuase everything is |
| 541 |
* passed in as a PyObject so we can't actually type check it. Only |
| 542 |
* fallback to PY_TYPE_OBJECT if strict checking is disabled |
| 543 |
*/ |
| 544 |
if (!strict) return PY_TYPE_OBJECT; |
| 545 |
|
| 546 |
PyErr_SetString (PyExc_TypeError, "could not get typecode from object"); |
| 547 |
return 0; |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* pyg_type_from_object: |
| 552 |
* obj: a Python object |
| 553 |
* |
| 554 |
* converts a python object to a GType. Raises an exception if it |
| 555 |
* can't perform the conversion. |
| 556 |
* |
| 557 |
* Returns: the corresponding GType, or 0 on error. |
| 558 |
*/ |
| 559 |
GType |
| 560 |
pyg_type_from_object (PyObject *obj) |
| 561 |
{ |
| 562 |
/* Legacy call always defaults to strict type checking */ |
| 563 |
return pyg_type_from_object_strict (obj, TRUE); |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* pyg_enum_get_value: |
| 568 |
* @enum_type: the GType of the flag. |
| 569 |
* @obj: a Python object representing the flag value |
| 570 |
* @val: a pointer to the location to store the integer representation of the flag. |
| 571 |
* |
| 572 |
* Converts a Python object to the integer equivalent. The conversion |
| 573 |
* will depend on the type of the Python object. If the object is an |
| 574 |
* integer, it is passed through directly. If it is a string, it will |
| 575 |
* be treated as a full or short enum name as defined in the GType. |
| 576 |
* |
| 577 |
* Returns: 0 on success or -1 on failure |
| 578 |
*/ |
| 579 |
gint |
| 580 |
pyg_enum_get_value (GType enum_type, PyObject *obj, gint *val) |
| 581 |
{ |
| 582 |
GEnumClass *eclass = NULL; |
| 583 |
gint res = -1; |
| 584 |
|
| 585 |
g_return_val_if_fail (val != NULL, -1); |
| 586 |
if (!obj) { |
| 587 |
*val = 0; |
| 588 |
return 0; |
| 589 |
} |
| 590 |
|
| 591 |
if (PyNumber_Check (obj)) { |
| 592 |
if (pygi_gint_from_py (obj, val)) res = 0; |
| 593 |
} else if (PyUnicode_Check (obj)) { |
| 594 |
GEnumValue *info; |
| 595 |
char *str = PyUnicode_AsUTF8 (obj); |
| 596 |
|
| 597 |
if (enum_type != G_TYPE_NONE) |
| 598 |
eclass = G_ENUM_CLASS (g_type_class_ref (enum_type)); |
| 599 |
else { |
| 600 |
PyErr_SetString (PyExc_TypeError, |
| 601 |
"could not convert string to enum because there " |
| 602 |
"is no GType associated to look up the value"); |
| 603 |
return -1; |
| 604 |
} |
| 605 |
info = g_enum_get_value_by_name (eclass, str); |
| 606 |
|
| 607 |
if (!info) info = g_enum_get_value_by_nick (eclass, str); |
| 608 |
if (info) { |
| 609 |
*val = info->value; |
| 610 |
res = 0; |
| 611 |
} else { |
| 612 |
PyErr_SetString (PyExc_TypeError, "could not convert string"); |
| 613 |
res = -1; |
| 614 |
} |
| 615 |
g_type_class_unref (eclass); |
| 616 |
} else { |
| 617 |
PyErr_SetString (PyExc_TypeError, |
| 618 |
"enum values must be strings or ints"); |
| 619 |
res = -1; |
| 620 |
} |
| 621 |
return res; |
| 622 |
} |
| 623 |
|
| 624 |
/** |
| 625 |
* pyg_flags_get_value: |
| 626 |
* @flag_type: the GType of the flag. |
| 627 |
* @obj: a Python object representing the flag value |
| 628 |
* @val: a pointer to the location to store the integer representation of the flag. |
| 629 |
* |
| 630 |
* Converts a Python object to the integer equivalent. The conversion |
| 631 |
* will depend on the type of the Python object. If the object is an |
| 632 |
* integer, it is passed through directly. If it is a string, it will |
| 633 |
* be treated as a full or short flag name as defined in the GType. |
| 634 |
* If it is a tuple, then the items are treated as strings and ORed |
| 635 |
* together. |
| 636 |
* |
| 637 |
* Returns: 0 on success or -1 on failure |
| 638 |
*/ |
| 639 |
gint |
| 640 |
pyg_flags_get_value (GType flag_type, PyObject *obj, guint *val) |
| 641 |
{ |
| 642 |
GFlagsClass *fclass = NULL; |
| 643 |
gint res = -1; |
| 644 |
|
| 645 |
g_return_val_if_fail (val != NULL, -1); |
| 646 |
if (!obj) { |
| 647 |
*val = 0; |
| 648 |
return 0; |
| 649 |
} |
| 650 |
|
| 651 |
if (PyNumber_Check (obj)) { |
| 652 |
if (pygi_guint_from_py (obj, val)) res = 0; |
| 653 |
} else if (PyUnicode_Check (obj)) { |
| 654 |
GFlagsValue *info; |
| 655 |
char *str = PyUnicode_AsUTF8 (obj); |
| 656 |
|
| 657 |
if (flag_type != G_TYPE_NONE) |
| 658 |
fclass = G_FLAGS_CLASS (g_type_class_ref (flag_type)); |
| 659 |
else { |
| 660 |
PyErr_SetString (PyExc_TypeError, |
| 661 |
"could not convert string to flag because there " |
| 662 |
"is no GType associated to look up the value"); |
| 663 |
return -1; |
| 664 |
} |
| 665 |
info = g_flags_get_value_by_name (fclass, str); |
| 666 |
|
| 667 |
if (!info) info = g_flags_get_value_by_nick (fclass, str); |
| 668 |
if (info) { |
| 669 |
*val = info->value; |
| 670 |
res = 0; |
| 671 |
} else { |
| 672 |
PyErr_SetString (PyExc_TypeError, "could not convert string"); |
| 673 |
res = -1; |
| 674 |
} |
| 675 |
g_type_class_unref (fclass); |
| 676 |
} else if (PyTuple_Check (obj)) { |
| 677 |
Py_ssize_t i, len; |
| 678 |
|
| 679 |
len = PyTuple_Size (obj); |
| 680 |
*val = 0; |
| 681 |
res = 0; |
| 682 |
|
| 683 |
if (flag_type != G_TYPE_NONE) |
| 684 |
fclass = G_FLAGS_CLASS (g_type_class_ref (flag_type)); |
| 685 |
else { |
| 686 |
PyErr_SetString (PyExc_TypeError, |
| 687 |
"could not convert string to flag because there " |
| 688 |
"is no GType associated to look up the value"); |
| 689 |
res = -1; |
| 690 |
} |
| 691 |
|
| 692 |
for (i = 0; i < len; i++) { |
| 693 |
PyObject *item = PyTuple_GetItem (obj, i); |
| 694 |
char *str = PyUnicode_AsUTF8 (item); |
| 695 |
GFlagsValue *info = g_flags_get_value_by_name (fclass, str); |
| 696 |
|
| 697 |
if (!info) info = g_flags_get_value_by_nick (fclass, str); |
| 698 |
if (info) { |
| 699 |
*val |= info->value; |
| 700 |
} else { |
| 701 |
PyErr_SetString (PyExc_TypeError, "could not convert string"); |
| 702 |
res = -1; |
| 703 |
break; |
| 704 |
} |
| 705 |
} |
| 706 |
g_type_class_unref (fclass); |
| 707 |
} else { |
| 708 |
PyErr_SetString ( |
| 709 |
PyExc_TypeError, |
| 710 |
"flag values must be strings, ints, longs, or tuples"); |
| 711 |
res = -1; |
| 712 |
} |
| 713 |
return res; |
| 714 |
} |
| 715 |
|
| 716 |
static GQuark pyg_type_marshal_key = 0; |
| 717 |
static GQuark pyg_type_marshal_helper_key = 0; |
| 718 |
|
| 719 |
typedef enum _marshal_helper_data_e marshal_helper_data_e; |
| 720 |
enum _marshal_helper_data_e { |
| 721 |
MARSHAL_HELPER_NONE = 0, |
| 722 |
MARSHAL_HELPER_RETURN_NULL, |
| 723 |
MARSHAL_HELPER_IMPORT_DONE, |
| 724 |
}; |
| 725 |
|
| 726 |
PyGTypeMarshal * |
| 727 |
pyg_type_lookup (GType type) |
| 728 |
{ |
| 729 |
GType ptype = type; |
| 730 |
PyGTypeMarshal *tm = NULL; |
| 731 |
marshal_helper_data_e marshal_helper; |
| 732 |
|
| 733 |
if (type == G_TYPE_INVALID) return NULL; |
| 734 |
|
| 735 |
marshal_helper = |
| 736 |
GPOINTER_TO_INT (g_type_get_qdata (type, pyg_type_marshal_helper_key)); |
| 737 |
|
| 738 |
/* If we called this function before with @type and nothing was found, |
| 739 |
* return NULL early to not spend time in the loop below */ |
| 740 |
if (marshal_helper == MARSHAL_HELPER_RETURN_NULL) return NULL; |
| 741 |
|
| 742 |
/* Otherwise do recursive type lookup */ |
| 743 |
do { |
| 744 |
if (marshal_helper == MARSHAL_HELPER_IMPORT_DONE) |
| 745 |
pygi_type_import_by_g_type (ptype); |
| 746 |
|
| 747 |
if ((tm = g_type_get_qdata (ptype, pyg_type_marshal_key)) != NULL) |
| 748 |
break; |
| 749 |
ptype = g_type_parent (ptype); |
| 750 |
} while (ptype); |
| 751 |
|
| 752 |
if (marshal_helper == MARSHAL_HELPER_NONE) { |
| 753 |
marshal_helper = (tm == NULL) ? MARSHAL_HELPER_RETURN_NULL |
| 754 |
: MARSHAL_HELPER_IMPORT_DONE; |
| 755 |
g_type_set_qdata (type, pyg_type_marshal_helper_key, |
| 756 |
GINT_TO_POINTER (marshal_helper)); |
| 757 |
} |
| 758 |
return tm; |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* pyg_register_gtype_custom: |
| 763 |
* @gtype: the GType for the new type |
| 764 |
* @from_func: a function to convert GValues to Python objects |
| 765 |
* @to_func: a function to convert Python objects to GValues |
| 766 |
* |
| 767 |
* In order to handle specific conversion of gboxed types or new |
| 768 |
* fundamental types, you may use this function to register conversion |
| 769 |
* handlers. |
| 770 |
*/ |
| 771 |
|
| 772 |
void |
| 773 |
pyg_register_gtype_custom (GType gtype, fromvaluefunc from_func, |
| 774 |
tovaluefunc to_func) |
| 775 |
{ |
| 776 |
PyGTypeMarshal *tm; |
| 777 |
|
| 778 |
if (!pyg_type_marshal_key) { |
| 779 |
pyg_type_marshal_key = g_quark_from_static_string ("PyGType::marshal"); |
| 780 |
pyg_type_marshal_helper_key = |
| 781 |
g_quark_from_static_string ("PyGType::marshal-helper"); |
| 782 |
} |
| 783 |
|
| 784 |
tm = g_new (PyGTypeMarshal, 1); |
| 785 |
tm->fromvalue = from_func; |
| 786 |
tm->tovalue = to_func; |
| 787 |
g_type_set_qdata (gtype, pyg_type_marshal_key, tm); |
| 788 |
} |
| 789 |
|
| 790 |
/* -------------- PyGClosure ----------------- */ |
| 791 |
|
| 792 |
static void |
| 793 |
pyg_closure_invalidate (gpointer data, GClosure *closure) |
| 794 |
{ |
| 795 |
PyGClosure *pc = (PyGClosure *)closure; |
| 796 |
PyGILState_STATE state; |
| 797 |
|
| 798 |
state = PyGILState_Ensure (); |
| 799 |
Py_XDECREF (pc->callback); |
| 800 |
Py_XDECREF (pc->extra_args); |
| 801 |
Py_XDECREF (pc->swap_data); |
| 802 |
PyGILState_Release (state); |
| 803 |
|
| 804 |
pc->callback = NULL; |
| 805 |
pc->extra_args = NULL; |
| 806 |
pc->swap_data = NULL; |
| 807 |
} |
| 808 |
|
| 809 |
static void |
| 810 |
pyg_closure_marshal (GClosure *closure, GValue *return_value, |
| 811 |
guint n_param_values, const GValue *param_values, |
| 812 |
gpointer invocation_hint, gpointer marshal_data) |
| 813 |
{ |
| 814 |
PyGILState_STATE state; |
| 815 |
PyGClosure *pc = (PyGClosure *)closure; |
| 816 |
PyObject *params, *ret; |
| 817 |
guint i; |
| 818 |
|
| 819 |
state = PyGILState_Ensure (); |
| 820 |
|
| 821 |
/* construct Python tuple for the parameter values */ |
| 822 |
params = PyTuple_New (n_param_values); |
| 823 |
for (i = 0; i < n_param_values; i++) { |
| 824 |
/* swap in a different initial data for connect_object() */ |
| 825 |
if (i == 0 && G_CCLOSURE_SWAP_DATA (closure)) { |
| 826 |
g_return_if_fail (pc->swap_data != NULL); |
| 827 |
Py_INCREF (pc->swap_data); |
| 828 |
PyTuple_SetItem (params, 0, pc->swap_data); |
| 829 |
} else { |
| 830 |
PyObject *item = pyg_value_to_pyobject (¶m_values[i], FALSE); |
| 831 |
|
| 832 |
/* error condition */ |
| 833 |
if (!item) { |
| 834 |
if (!PyErr_Occurred ()) |
| 835 |
PyErr_SetString ( |
| 836 |
PyExc_TypeError, |
| 837 |
"can't convert parameter to desired type"); |
| 838 |
|
| 839 |
if (pc->exception_handler) |
| 840 |
pc->exception_handler (return_value, n_param_values, |
| 841 |
param_values); |
| 842 |
else |
| 843 |
PyErr_Print (); |
| 844 |
|
| 845 |
goto out; |
| 846 |
} |
| 847 |
PyTuple_SetItem (params, i, item); |
| 848 |
} |
| 849 |
} |
| 850 |
/* params passed to function may have extra arguments */ |
| 851 |
if (pc->extra_args) { |
| 852 |
PyObject *tuple = params; |
| 853 |
params = PySequence_Concat (tuple, pc->extra_args); |
| 854 |
Py_DECREF (tuple); |
| 855 |
} |
| 856 |
ret = PyObject_CallObject (pc->callback, params); |
| 857 |
if (ret == NULL) { |
| 858 |
if (pc->exception_handler) |
| 859 |
pc->exception_handler (return_value, n_param_values, param_values); |
| 860 |
else |
| 861 |
PyErr_Print (); |
| 862 |
goto out; |
| 863 |
} |
| 864 |
|
| 865 |
if (G_IS_VALUE (return_value) |
| 866 |
&& pyg_value_from_pyobject (return_value, ret) != 0) { |
| 867 |
/* If we already have an exception set, use that, otherwise set a |
| 868 |
* generic one */ |
| 869 |
if (!PyErr_Occurred ()) |
| 870 |
PyErr_SetString (PyExc_TypeError, |
| 871 |
"can't convert return value to desired type"); |
| 872 |
|
| 873 |
if (pc->exception_handler) |
| 874 |
pc->exception_handler (return_value, n_param_values, param_values); |
| 875 |
else |
| 876 |
PyErr_Print (); |
| 877 |
} |
| 878 |
Py_DECREF (ret); |
| 879 |
|
| 880 |
out: |
| 881 |
Py_DECREF (params); |
| 882 |
PyGILState_Release (state); |
| 883 |
} |
| 884 |
|
| 885 |
/** |
| 886 |
* pyg_closure_new: |
| 887 |
* callback: a Python callable object |
| 888 |
* extra_args: a tuple of extra arguments, or None/NULL. |
| 889 |
* swap_data: an alternative python object to pass first. |
| 890 |
* |
| 891 |
* Creates a GClosure wrapping a Python callable and optionally a set |
| 892 |
* of additional function arguments. This is needed to attach python |
| 893 |
* handlers to signals, for instance. |
| 894 |
* |
| 895 |
* Returns: the new closure. |
| 896 |
*/ |
| 897 |
GClosure * |
| 898 |
pyg_closure_new (PyObject *callback, PyObject *extra_args, PyObject *swap_data) |
| 899 |
{ |
| 900 |
GClosure *closure; |
| 901 |
|
| 902 |
g_return_val_if_fail (callback != NULL, NULL); |
| 903 |
closure = g_closure_new_simple (sizeof (PyGClosure), NULL); |
| 904 |
g_closure_add_invalidate_notifier (closure, NULL, pyg_closure_invalidate); |
| 905 |
g_closure_set_marshal (closure, pyg_closure_marshal); |
| 906 |
Py_INCREF (callback); |
| 907 |
((PyGClosure *)closure)->callback = callback; |
| 908 |
if (extra_args && !Py_IsNone (extra_args)) { |
| 909 |
Py_INCREF (extra_args); |
| 910 |
if (!PyTuple_Check (extra_args)) { |
| 911 |
PyObject *tmp = PyTuple_New (1); |
| 912 |
PyTuple_SetItem (tmp, 0, extra_args); |
| 913 |
extra_args = tmp; |
| 914 |
} |
| 915 |
((PyGClosure *)closure)->extra_args = extra_args; |
| 916 |
} |
| 917 |
if (swap_data) { |
| 918 |
Py_INCREF (swap_data); |
| 919 |
((PyGClosure *)closure)->swap_data = swap_data; |
| 920 |
closure->derivative_flag = TRUE; |
| 921 |
} |
| 922 |
return closure; |
| 923 |
} |
| 924 |
|
| 925 |
/* -------------- PySignalClassClosure ----------------- */ |
| 926 |
/* a closure used for the `class closure' of a signal. As this gets |
| 927 |
* all the info from the first argument to the closure and the |
| 928 |
* invocation hint, we can have a single closure that handles all |
| 929 |
* class closure cases. We call a method by the name of the signal |
| 930 |
* with "do_" prepended. |
| 931 |
* |
| 932 |
* We also remove the first argument from the * param list, as it is |
| 933 |
* the instance object, which is passed * implicitly to the method |
| 934 |
* object. */ |
| 935 |
|
| 936 |
static void |
| 937 |
pyg_signal_class_closure_marshal (GClosure *closure, GValue *return_value, |
| 938 |
guint n_param_values, |
| 939 |
const GValue *param_values, |
| 940 |
gpointer invocation_hint, |
| 941 |
gpointer marshal_data) |
| 942 |
{ |
| 943 |
PyGILState_STATE state; |
| 944 |
GObject *object; |
| 945 |
PyObject *object_wrapper; |
| 946 |
GSignalInvocationHint *hint = (GSignalInvocationHint *)invocation_hint; |
| 947 |
gchar *method_name, *tmp; |
| 948 |
PyObject *method; |
| 949 |
PyObject *params, *ret; |
| 950 |
Py_ssize_t py_len; |
| 951 |
guint i, len; |
| 952 |
|
| 953 |
state = PyGILState_Ensure (); |
| 954 |
|
| 955 |
g_return_if_fail (invocation_hint != NULL); |
| 956 |
/* get the object passed as the first argument to the closure */ |
| 957 |
object = g_value_get_object (¶m_values[0]); |
| 958 |
g_return_if_fail (object != NULL && G_IS_OBJECT (object)); |
| 959 |
|
| 960 |
/* get the wrapper for this object */ |
| 961 |
object_wrapper = pygobject_new (object); |
| 962 |
g_return_if_fail (object_wrapper != NULL); |
| 963 |
|
| 964 |
/* construct method name for this class closure */ |
| 965 |
method_name = g_strconcat ("do_", g_signal_name (hint->signal_id), NULL); |
| 966 |
|
| 967 |
/* convert dashes to underscores. For some reason, g_signal_name |
| 968 |
* seems to convert all the underscores in the signal name to |
| 969 |
dashes??? */ |
| 970 |
for (tmp = method_name; *tmp != '\0'; tmp++) |
| 971 |
if (*tmp == '-') *tmp = '_'; |
| 972 |
|
| 973 |
method = PyObject_GetAttrString (object_wrapper, method_name); |
| 974 |
g_free (method_name); |
| 975 |
|
| 976 |
if (!method) { |
| 977 |
PyErr_Clear (); |
| 978 |
Py_DECREF (object_wrapper); |
| 979 |
PyGILState_Release (state); |
| 980 |
return; |
| 981 |
} |
| 982 |
Py_DECREF (object_wrapper); |
| 983 |
|
| 984 |
/* construct Python tuple for the parameter values; don't copy boxed values |
| 985 |
initially because we'll check after the call to see if a copy is needed. */ |
| 986 |
params = PyTuple_New (n_param_values - 1); |
| 987 |
for (i = 1; i < n_param_values; i++) { |
| 988 |
PyObject *item = pyg_value_to_pyobject (¶m_values[i], FALSE); |
| 989 |
|
| 990 |
/* error condition */ |
| 991 |
if (!item) { |
| 992 |
Py_DECREF (params); |
| 993 |
PyGILState_Release (state); |
| 994 |
return; |
| 995 |
} |
| 996 |
PyTuple_SetItem (params, i - 1, item); |
| 997 |
} |
| 998 |
|
| 999 |
ret = PyObject_CallObject (method, params); |
| 1000 |
|
| 1001 |
/* Copy boxed values if others ref them, this needs to be done regardless of |
| 1002 |
exception status. */ |
| 1003 |
py_len = PyTuple_Size (params); |
| 1004 |
len = (guint)py_len; |
| 1005 |
for (i = 0; i < len; i++) { |
| 1006 |
PyObject *item = PyTuple_GetItem (params, i); |
| 1007 |
if (item != NULL && PyObject_TypeCheck (item, &PyGBoxed_Type) |
| 1008 |
&& Py_REFCNT (item) != 1) { |
| 1009 |
PyGBoxed *boxed_item = (PyGBoxed *)item; |
| 1010 |
if (!boxed_item->free_on_dealloc) { |
| 1011 |
gpointer boxed_ptr = pyg_boxed_get_ptr (boxed_item); |
| 1012 |
pyg_boxed_set_ptr ( |
| 1013 |
boxed_item, g_boxed_copy (boxed_item->gtype, boxed_ptr)); |
| 1014 |
boxed_item->free_on_dealloc = TRUE; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
if (ret == NULL) { |
| 1020 |
PyErr_Print (); |
| 1021 |
Py_DECREF (method); |
| 1022 |
Py_DECREF (params); |
| 1023 |
PyGILState_Release (state); |
| 1024 |
return; |
| 1025 |
} |
| 1026 |
Py_DECREF (method); |
| 1027 |
Py_DECREF (params); |
| 1028 |
if (G_IS_VALUE (return_value)) pyg_value_from_pyobject (return_value, ret); |
| 1029 |
Py_DECREF (ret); |
| 1030 |
PyGILState_Release (state); |
| 1031 |
} |
| 1032 |
|
| 1033 |
/** |
| 1034 |
* pyg_signal_class_closure_get: |
| 1035 |
* |
| 1036 |
* Returns the GClosure used for the class closure of signals. When |
| 1037 |
* called, it will invoke the method do_signalname (for the signal |
| 1038 |
* "signalname"). |
| 1039 |
* |
| 1040 |
* Returns: the closure. |
| 1041 |
*/ |
| 1042 |
GClosure * |
| 1043 |
pyg_signal_class_closure_get (void) |
| 1044 |
{ |
| 1045 |
static GClosure *closure; |
| 1046 |
|
| 1047 |
if (closure == NULL) { |
| 1048 |
closure = g_closure_new_simple (sizeof (GClosure), NULL); |
| 1049 |
g_closure_set_marshal (closure, pyg_signal_class_closure_marshal); |
| 1050 |
|
| 1051 |
g_closure_ref (closure); |
| 1052 |
g_closure_sink (closure); |
| 1053 |
} |
| 1054 |
return closure; |
| 1055 |
} |
| 1056 |
|
| 1057 |
/* ----- __doc__ descriptor for GObject and GInterface ----- */ |
| 1058 |
|
| 1059 |
static void |
| 1060 |
object_doc_dealloc (PyObject *self) |
| 1061 |
{ |
| 1062 |
PyObject_Free (self); |
| 1063 |
} |
| 1064 |
|
| 1065 |
/* append information about signals of a particular gtype */ |
| 1066 |
static void |
| 1067 |
add_signal_docs (GType gtype, GString *string) |
| 1068 |
{ |
| 1069 |
GTypeClass *class = NULL; |
| 1070 |
guint *signal_ids, n_ids = 0, i; |
| 1071 |
|
| 1072 |
if (G_TYPE_IS_CLASSED (gtype)) class = g_type_class_ref (gtype); |
| 1073 |
signal_ids = g_signal_list_ids (gtype, &n_ids); |
| 1074 |
|
| 1075 |
if (n_ids > 0) { |
| 1076 |
g_string_append_printf (string, "Signals from %s:\n", |
| 1077 |
g_type_name (gtype)); |
| 1078 |
|
| 1079 |
for (i = 0; i < n_ids; i++) { |
| 1080 |
GSignalQuery query; |
| 1081 |
guint j; |
| 1082 |
|
| 1083 |
g_signal_query (signal_ids[i], &query); |
| 1084 |
|
| 1085 |
g_string_append (string, " "); |
| 1086 |
g_string_append (string, query.signal_name); |
| 1087 |
g_string_append (string, " ("); |
| 1088 |
for (j = 0; j < query.n_params; j++) { |
| 1089 |
g_string_append (string, g_type_name (query.param_types[j])); |
| 1090 |
if (j != query.n_params - 1) g_string_append (string, ", "); |
| 1091 |
} |
| 1092 |
g_string_append (string, ")"); |
| 1093 |
if (query.return_type && query.return_type != G_TYPE_NONE) { |
| 1094 |
g_string_append (string, " -> "); |
| 1095 |
g_string_append (string, g_type_name (query.return_type)); |
| 1096 |
} |
| 1097 |
g_string_append (string, "\n"); |
| 1098 |
} |
| 1099 |
g_free (signal_ids); |
| 1100 |
g_string_append (string, "\n"); |
| 1101 |
} |
| 1102 |
if (class) g_type_class_unref (class); |
| 1103 |
} |
| 1104 |
|
| 1105 |
static void |
| 1106 |
add_property_docs (GType gtype, GString *string) |
| 1107 |
{ |
| 1108 |
GObjectClass *class; |
| 1109 |
GParamSpec **props; |
| 1110 |
guint n_props = 0, i; |
| 1111 |
gboolean has_prop = FALSE; |
| 1112 |
const gchar *blurb = NULL; |
| 1113 |
|
| 1114 |
class = g_type_class_ref (gtype); |
| 1115 |
props = g_object_class_list_properties (class, &n_props); |
| 1116 |
|
| 1117 |
for (i = 0; i < n_props; i++) { |
| 1118 |
if (props[i]->owner_type != gtype) |
| 1119 |
continue; /* these are from a parent type */ |
| 1120 |
|
| 1121 |
/* print out the heading first */ |
| 1122 |
if (!has_prop) { |
| 1123 |
g_string_append_printf (string, "Properties from %s:\n", |
| 1124 |
g_type_name (gtype)); |
| 1125 |
has_prop = TRUE; |
| 1126 |
} |
| 1127 |
g_string_append_printf (string, " %s -> %s: %s\n", |
| 1128 |
g_param_spec_get_name (props[i]), |
| 1129 |
g_type_name (props[i]->value_type), |
| 1130 |
g_param_spec_get_nick (props[i])); |
| 1131 |
|
| 1132 |
/* g_string_append_printf crashes on win32 if the third |
| 1133 |
argument is NULL. */ |
| 1134 |
blurb = g_param_spec_get_blurb (props[i]); |
| 1135 |
if (blurb) g_string_append_printf (string, " %s\n", blurb); |
| 1136 |
} |
| 1137 |
g_free (props); |
| 1138 |
if (has_prop) g_string_append (string, "\n"); |
| 1139 |
g_type_class_unref (class); |
| 1140 |
} |
| 1141 |
|
| 1142 |
static PyObject * |
| 1143 |
object_doc_descr_get (PyObject *self, PyObject *obj, PyObject *type) |
| 1144 |
{ |
| 1145 |
GType gtype = 0; |
| 1146 |
GString *string; |
| 1147 |
PyObject *pystring; |
| 1148 |
|
| 1149 |
if (obj && pygobject_check (obj, &PyGObject_Type)) { |
| 1150 |
gtype = G_OBJECT_TYPE (pygobject_get (obj)); |
| 1151 |
if (!gtype) |
| 1152 |
PyErr_SetString (PyExc_RuntimeError, "could not get object type"); |
| 1153 |
} else { |
| 1154 |
gtype = pyg_type_from_object (type); |
| 1155 |
} |
| 1156 |
if (!gtype) return NULL; |
| 1157 |
|
| 1158 |
string = g_string_new_len (NULL, 512); |
| 1159 |
|
| 1160 |
if (g_type_is_a (gtype, G_TYPE_INTERFACE)) |
| 1161 |
g_string_append_printf (string, "Interface %s\n\n", |
| 1162 |
g_type_name (gtype)); |
| 1163 |
else if (g_type_is_a (gtype, G_TYPE_OBJECT)) |
| 1164 |
g_string_append_printf (string, "Object %s\n\n", g_type_name (gtype)); |
| 1165 |
else |
| 1166 |
g_string_append_printf (string, "%s\n\n", g_type_name (gtype)); |
| 1167 |
|
| 1168 |
if (((PyTypeObject *)type)->tp_doc) |
| 1169 |
g_string_append_printf (string, "%s\n\n", |
| 1170 |
((PyTypeObject *)type)->tp_doc); |
| 1171 |
|
| 1172 |
if (g_type_is_a (gtype, G_TYPE_OBJECT)) { |
| 1173 |
GType parent = G_TYPE_OBJECT; |
| 1174 |
GArray *parents = g_array_new (FALSE, FALSE, sizeof (GType)); |
| 1175 |
int iparent; |
| 1176 |
|
| 1177 |
while (parent) { |
| 1178 |
g_array_append_val (parents, parent); |
| 1179 |
parent = g_type_next_base (gtype, parent); |
| 1180 |
} |
| 1181 |
|
| 1182 |
for (iparent = parents->len - 1; iparent >= 0; --iparent) { |
| 1183 |
GType *interfaces; |
| 1184 |
guint n_interfaces, i; |
| 1185 |
|
| 1186 |
parent = g_array_index (parents, GType, iparent); |
| 1187 |
add_signal_docs (parent, string); |
| 1188 |
add_property_docs (parent, string); |
| 1189 |
|
| 1190 |
/* add docs for implemented interfaces */ |
| 1191 |
interfaces = g_type_interfaces (parent, &n_interfaces); |
| 1192 |
for (i = 0; i < n_interfaces; i++) |
| 1193 |
add_signal_docs (interfaces[i], string); |
| 1194 |
g_free (interfaces); |
| 1195 |
} |
| 1196 |
g_array_free (parents, TRUE); |
| 1197 |
} |
| 1198 |
|
| 1199 |
pystring = PyUnicode_FromStringAndSize (string->str, string->len); |
| 1200 |
g_string_free (string, TRUE); |
| 1201 |
return pystring; |
| 1202 |
} |
| 1203 |
|
| 1204 |
PYGI_DEFINE_TYPE ("gobject.GObject.__doc__", PyGObjectDoc_Type, PyObject); |
| 1205 |
|
| 1206 |
/** |
| 1207 |
* pyg_object_descr_doc_get: |
| 1208 |
* |
| 1209 |
* Returns an object intended to be the __doc__ attribute of GObject |
| 1210 |
* wrappers. When read in the context of the object it will return |
| 1211 |
* some documentation about the signals and properties of the object. |
| 1212 |
* |
| 1213 |
* Returns: the descriptor. |
| 1214 |
*/ |
| 1215 |
PyObject * |
| 1216 |
pyg_object_descr_doc_get (void) |
| 1217 |
{ |
| 1218 |
static PyObject *doc_descr = NULL; |
| 1219 |
|
| 1220 |
if (!doc_descr) { |
| 1221 |
Py_SET_TYPE (&PyGObjectDoc_Type, &PyType_Type); |
| 1222 |
if (PyType_Ready (&PyGObjectDoc_Type)) return NULL; |
| 1223 |
|
| 1224 |
doc_descr = PyObject_New (PyObject, &PyGObjectDoc_Type); |
| 1225 |
if (doc_descr == NULL) return NULL; |
| 1226 |
} |
| 1227 |
return doc_descr; |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
gboolean |
| 1232 |
pyg_gtype_is_custom (GType gtype) |
| 1233 |
{ |
| 1234 |
return g_type_get_qdata (gtype, pygobject_custom_key) != NULL; |
| 1235 |
} |
| 1236 |
|
| 1237 |
static PyObject * |
| 1238 |
strv_from_gvalue (const GValue *value) |
| 1239 |
{ |
| 1240 |
gchar **argv; |
| 1241 |
PyObject *py_argv; |
| 1242 |
gsize i; |
| 1243 |
|
| 1244 |
argv = (gchar **)g_value_get_boxed (value); |
| 1245 |
py_argv = PyList_New (0); |
| 1246 |
|
| 1247 |
for (i = 0; argv && argv[i]; i++) { |
| 1248 |
int res; |
| 1249 |
PyObject *item = pygi_utf8_to_py (argv[i]); |
| 1250 |
if (item == NULL) { |
| 1251 |
Py_DECREF (py_argv); |
| 1252 |
return NULL; |
| 1253 |
} |
| 1254 |
res = PyList_Append (py_argv, item); |
| 1255 |
Py_DECREF (item); |
| 1256 |
if (res == -1) { |
| 1257 |
Py_DECREF (py_argv); |
| 1258 |
return NULL; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
return py_argv; |
| 1263 |
} |
| 1264 |
|
| 1265 |
static int |
| 1266 |
strv_to_gvalue (GValue *value, PyObject *obj) |
| 1267 |
{ |
| 1268 |
Py_ssize_t argc, i; |
| 1269 |
gchar **argv; |
| 1270 |
|
| 1271 |
if (!(PyTuple_Check (obj) || PyList_Check (obj))) return -1; |
| 1272 |
|
| 1273 |
argc = PySequence_Length (obj); |
| 1274 |
argv = g_new (gchar *, argc + 1); |
| 1275 |
for (i = 0; i < argc; ++i) { |
| 1276 |
PyObject *item = PySequence_Fast_GET_ITEM (obj, i); |
| 1277 |
if (!pygi_utf8_from_py (item, &(argv[i]))) goto error; |
| 1278 |
} |
| 1279 |
|
| 1280 |
argv[i] = NULL; |
| 1281 |
g_value_take_boxed (value, argv); |
| 1282 |
return 0; |
| 1283 |
|
| 1284 |
error: |
| 1285 |
for (i = i - 1; i >= 0; i--) { |
| 1286 |
g_free (argv[i]); |
| 1287 |
} |
| 1288 |
g_free (argv); |
| 1289 |
return -1; |
| 1290 |
} |
| 1291 |
|
| 1292 |
/** |
| 1293 |
* Returns 0 on success, or -1 and sets an exception. |
| 1294 |
*/ |
| 1295 |
int |
| 1296 |
pygi_type_register_types (PyObject *d) |
| 1297 |
{ |
| 1298 |
PyGTypeWrapper_Type.tp_dealloc = (destructor)pyg_type_wrapper_dealloc; |
| 1299 |
PyGTypeWrapper_Type.tp_richcompare = pyg_type_wrapper_richcompare; |
| 1300 |
PyGTypeWrapper_Type.tp_repr = (reprfunc)pyg_type_wrapper_repr; |
| 1301 |
PyGTypeWrapper_Type.tp_hash = (hashfunc)pyg_type_wrapper_hash; |
| 1302 |
PyGTypeWrapper_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1303 |
PyGTypeWrapper_Type.tp_methods = _PyGTypeWrapper_methods; |
| 1304 |
PyGTypeWrapper_Type.tp_getset = _PyGTypeWrapper_getsets; |
| 1305 |
PyGTypeWrapper_Type.tp_init = (initproc)pyg_type_wrapper_init; |
| 1306 |
PyGTypeWrapper_Type.tp_alloc = PyType_GenericAlloc; |
| 1307 |
PyGTypeWrapper_Type.tp_new = PyType_GenericNew; |
| 1308 |
if (PyType_Ready (&PyGTypeWrapper_Type)) return -1; |
| 1309 |
|
| 1310 |
PyDict_SetItemString (d, "GType", (PyObject *)&PyGTypeWrapper_Type); |
| 1311 |
|
| 1312 |
/* This type lazily registered in pyg_object_descr_doc_get */ |
| 1313 |
PyGObjectDoc_Type.tp_dealloc = (destructor)object_doc_dealloc; |
| 1314 |
PyGObjectDoc_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1315 |
PyGObjectDoc_Type.tp_descr_get = (descrgetfunc)object_doc_descr_get; |
| 1316 |
|
| 1317 |
pyg_register_gtype_custom (G_TYPE_STRV, strv_from_gvalue, strv_to_gvalue); |
| 1318 |
|
| 1319 |
return 0; |
| 1320 |
} |
| 1321 |
|
| 1322 |
/** |
| 1323 |
* pygi_interface_type_tag: |
| 1324 |
* |
| 1325 |
* A simple way to map types often obtained by calling |
| 1326 |
* `gi_type_info_get_interface` to an enum. |
| 1327 |
* This makes for easier to read code. |
| 1328 |
*/ |
| 1329 |
PyGIInterfaceTypeTag |
| 1330 |
pygi_interface_type_tag (GIBaseInfo *info) |
| 1331 |
{ |
| 1332 |
PyGIInterfaceTypeTag iface_type_tag; |
| 1333 |
|
| 1334 |
if (GI_IS_FLAGS_INFO (info)) { |
| 1335 |
/* Check flags before enums: flags are a subtype of enum. */ |
| 1336 |
iface_type_tag = PYGI_INTERFACE_TYPE_TAG_FLAGS; |
| 1337 |
} else if (GI_IS_ENUM_INFO (info)) { |
| 1338 |
iface_type_tag = PYGI_INTERFACE_TYPE_TAG_ENUM; |
| 1339 |
} else if (GI_IS_INTERFACE_INFO (info)) { |
| 1340 |
iface_type_tag = PYGI_INTERFACE_TYPE_TAG_INTERFACE; |
| 1341 |
} else if (GI_IS_OBJECT_INFO (info)) { |
| 1342 |
iface_type_tag = PYGI_INTERFACE_TYPE_TAG_OBJECT; |
| 1343 |
} else if (GI_IS_STRUCT_INFO (info)) { |
| 1344 |
iface_type_tag = PYGI_INTERFACE_TYPE_TAG_STRUCT; |
| 1345 |
} else if (GI_IS_UNION_INFO (info)) { |
| 1346 |
iface_type_tag = PYGI_INTERFACE_TYPE_TAG_UNION; |
| 1347 |
} else if (GI_IS_CALLBACK_INFO (info)) { |
| 1348 |
iface_type_tag = PYGI_INTERFACE_TYPE_TAG_CALLBACK; |
| 1349 |
} else { |
| 1350 |
g_critical ("Type %s is not handled", |
| 1351 |
g_type_name_from_instance ((GTypeInstance *)info)); |
| 1352 |
g_assert_not_reached (); |
| 1353 |
} |
| 1354 |
|
| 1355 |
return iface_type_tag; |
| 1356 |
} |