| 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 |
* pygobject-typeinfo.c: everything needed to set up |
| 7 |
* Python classes derived from GObject. |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Lesser General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Lesser General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public |
| 20 |
* License along with this library; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 22 |
* USA |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "pygobject-types.h" |
| 26 |
#include "pygenum.h" |
| 27 |
#include "pygflags.h" |
| 28 |
#include "pygi-basictype.h" |
| 29 |
#include "pygi-fundamental.h" |
| 30 |
#include "pygi-property.h" |
| 31 |
#include "pygi-type.h" |
| 32 |
#include "pygi-util.h" |
| 33 |
#include "pygi-value.h" |
| 34 |
#include "pygobject-object.h" |
| 35 |
|
| 36 |
extern GQuark pygobject_instance_init_ref_count; |
| 37 |
|
| 38 |
static GPrivate pygobject_construction_wrapper; |
| 39 |
|
| 40 |
void |
| 41 |
pygobject_init_wrapper_set (PyObject *wrapper) |
| 42 |
{ |
| 43 |
g_private_set (&pygobject_construction_wrapper, wrapper); |
| 44 |
} |
| 45 |
|
| 46 |
static inline PyObject * |
| 47 |
pygobject_init_wrapper_get (void) |
| 48 |
{ |
| 49 |
return (PyObject *)g_private_get (&pygobject_construction_wrapper); |
| 50 |
} |
| 51 |
|
| 52 |
typedef struct _PyGSignalAccumulatorData { |
| 53 |
PyObject *callable; |
| 54 |
PyObject *user_data; |
| 55 |
} PyGSignalAccumulatorData; |
| 56 |
|
| 57 |
|
| 58 |
static gboolean |
| 59 |
_pyg_signal_accumulator (GSignalInvocationHint *ihint, GValue *return_accu, |
| 60 |
const GValue *handler_return, gpointer _data) |
| 61 |
{ |
| 62 |
PyObject *py_ihint, *py_return_accu, *py_handler_return, *py_detail; |
| 63 |
PyObject *py_retval; |
| 64 |
gboolean retval = FALSE; |
| 65 |
PyGSignalAccumulatorData *data = _data; |
| 66 |
PyGILState_STATE state; |
| 67 |
|
| 68 |
state = PyGILState_Ensure (); |
| 69 |
if (ihint->detail) |
| 70 |
py_detail = PyUnicode_FromString (g_quark_to_string (ihint->detail)); |
| 71 |
else { |
| 72 |
py_detail = Py_NewRef (Py_None); |
| 73 |
} |
| 74 |
|
| 75 |
py_ihint = Py_BuildValue ("lNi", (long int)ihint->signal_id, py_detail, |
| 76 |
ihint->run_type); |
| 77 |
py_handler_return = pyg_value_to_pyobject (handler_return, TRUE); |
| 78 |
py_return_accu = pyg_value_to_pyobject (return_accu, FALSE); |
| 79 |
if (data->user_data) |
| 80 |
py_retval = PyObject_CallFunction (data->callable, "NNNO", py_ihint, |
| 81 |
py_return_accu, py_handler_return, |
| 82 |
data->user_data); |
| 83 |
else |
| 84 |
py_retval = PyObject_CallFunction (data->callable, "NNN", py_ihint, |
| 85 |
py_return_accu, py_handler_return); |
| 86 |
if (!py_retval) |
| 87 |
PyErr_Print (); |
| 88 |
else { |
| 89 |
if (!PyTuple_Check (py_retval) || PyTuple_Size (py_retval) != 2) { |
| 90 |
PyErr_SetString (PyExc_TypeError, |
| 91 |
"accumulator function must return" |
| 92 |
" a (bool, object) tuple"); |
| 93 |
PyErr_Print (); |
| 94 |
} else { |
| 95 |
retval = PyObject_IsTrue (PyTuple_GET_ITEM (py_retval, 0)); |
| 96 |
if (pyg_value_from_pyobject (return_accu, |
| 97 |
PyTuple_GET_ITEM (py_retval, 1))) { |
| 98 |
PyErr_Print (); |
| 99 |
} |
| 100 |
} |
| 101 |
Py_DECREF (py_retval); |
| 102 |
} |
| 103 |
PyGILState_Release (state); |
| 104 |
return retval; |
| 105 |
} |
| 106 |
|
| 107 |
static gboolean |
| 108 |
override_signal (GType instance_type, const gchar *signal_name) |
| 109 |
{ |
| 110 |
guint signal_id; |
| 111 |
|
| 112 |
signal_id = g_signal_lookup (signal_name, instance_type); |
| 113 |
if (!signal_id) { |
| 114 |
gchar buf[128]; |
| 115 |
|
| 116 |
g_snprintf (buf, sizeof (buf), "could not look up %s", signal_name); |
| 117 |
PyErr_SetString (PyExc_TypeError, buf); |
| 118 |
return FALSE; |
| 119 |
} |
| 120 |
g_signal_override_class_closure (signal_id, instance_type, |
| 121 |
pyg_signal_class_closure_get ()); |
| 122 |
return TRUE; |
| 123 |
} |
| 124 |
|
| 125 |
static gboolean |
| 126 |
create_signal (GType instance_type, const gchar *signal_name, PyObject *tuple) |
| 127 |
{ |
| 128 |
GSignalFlags signal_flags; |
| 129 |
PyObject *py_return_type, *py_param_types; |
| 130 |
GType return_type; |
| 131 |
guint n_params, i; |
| 132 |
Py_ssize_t py_n_params; |
| 133 |
GType *param_types; |
| 134 |
guint signal_id; |
| 135 |
GSignalAccumulator accumulator = NULL; |
| 136 |
PyGSignalAccumulatorData *accum_data = NULL; |
| 137 |
PyObject *py_accum = NULL, *py_accum_data = NULL; |
| 138 |
|
| 139 |
if (!PyArg_ParseTuple (tuple, "iOO|OO", &signal_flags, &py_return_type, |
| 140 |
&py_param_types, &py_accum, &py_accum_data)) { |
| 141 |
gchar buf[128]; |
| 142 |
|
| 143 |
PyErr_Clear (); |
| 144 |
g_snprintf (buf, sizeof (buf), |
| 145 |
"value for __gsignals__['%s'] not in correct format", |
| 146 |
signal_name); |
| 147 |
PyErr_SetString (PyExc_TypeError, buf); |
| 148 |
return FALSE; |
| 149 |
} |
| 150 |
|
| 151 |
if (py_accum && !Py_IsNone (py_accum) && !PyCallable_Check (py_accum)) { |
| 152 |
gchar buf[128]; |
| 153 |
|
| 154 |
g_snprintf (buf, sizeof (buf), |
| 155 |
"accumulator for __gsignals__['%s'] must be callable", |
| 156 |
signal_name); |
| 157 |
PyErr_SetString (PyExc_TypeError, buf); |
| 158 |
return FALSE; |
| 159 |
} |
| 160 |
|
| 161 |
return_type = pyg_type_from_object (py_return_type); |
| 162 |
if (!return_type) return FALSE; |
| 163 |
if (!PySequence_Check (py_param_types)) { |
| 164 |
gchar buf[128]; |
| 165 |
|
| 166 |
g_snprintf ( |
| 167 |
buf, sizeof (buf), |
| 168 |
"third element of __gsignals__['%s'] tuple must be a sequence", |
| 169 |
signal_name); |
| 170 |
PyErr_SetString (PyExc_TypeError, buf); |
| 171 |
return FALSE; |
| 172 |
} |
| 173 |
py_n_params = PySequence_Length (py_param_types); |
| 174 |
if (py_n_params < 0) return FALSE; |
| 175 |
|
| 176 |
if (!pygi_guint_from_pyssize (py_n_params, &n_params)) return FALSE; |
| 177 |
|
| 178 |
param_types = g_new (GType, n_params); |
| 179 |
for (i = 0; i < n_params; i++) { |
| 180 |
PyObject *item = PySequence_GetItem (py_param_types, i); |
| 181 |
|
| 182 |
param_types[i] = pyg_type_from_object (item); |
| 183 |
if (param_types[i] == 0) { |
| 184 |
Py_DECREF (item); |
| 185 |
g_free (param_types); |
| 186 |
return FALSE; |
| 187 |
} |
| 188 |
Py_DECREF (item); |
| 189 |
} |
| 190 |
|
| 191 |
if (py_accum != NULL && !Py_IsNone (py_accum)) { |
| 192 |
accum_data = g_new (PyGSignalAccumulatorData, 1); |
| 193 |
accum_data->callable = Py_NewRef (py_accum); |
| 194 |
accum_data->user_data = Py_XNewRef (py_accum_data); |
| 195 |
accumulator = _pyg_signal_accumulator; |
| 196 |
} |
| 197 |
|
| 198 |
signal_id = g_signal_newv (signal_name, instance_type, signal_flags, |
| 199 |
pyg_signal_class_closure_get (), accumulator, |
| 200 |
accum_data, gi_cclosure_marshal_generic, |
| 201 |
return_type, n_params, param_types); |
| 202 |
g_free (param_types); |
| 203 |
|
| 204 |
if (signal_id == 0) { |
| 205 |
gchar buf[128]; |
| 206 |
|
| 207 |
g_snprintf (buf, sizeof (buf), "could not create signal for %s", |
| 208 |
signal_name); |
| 209 |
PyErr_SetString (PyExc_RuntimeError, buf); |
| 210 |
return FALSE; |
| 211 |
} |
| 212 |
return TRUE; |
| 213 |
} |
| 214 |
|
| 215 |
static PyObject * |
| 216 |
add_signals (GObjectClass *klass, PyObject *signals) |
| 217 |
{ |
| 218 |
gboolean ret = TRUE; |
| 219 |
Py_ssize_t pos = 0; |
| 220 |
PyObject *key, *value, *overridden_signals = NULL; |
| 221 |
GType instance_type = G_OBJECT_CLASS_TYPE (klass); |
| 222 |
|
| 223 |
overridden_signals = PyDict_New (); |
| 224 |
while (PyDict_Next (signals, &pos, &key, &value)) { |
| 225 |
const gchar *signal_name; |
| 226 |
gchar *signal_name_canon, *c; |
| 227 |
|
| 228 |
if (!PyUnicode_Check (key)) { |
| 229 |
PyErr_SetString (PyExc_TypeError, |
| 230 |
"__gsignals__ keys must be strings"); |
| 231 |
ret = FALSE; |
| 232 |
break; |
| 233 |
} |
| 234 |
signal_name = PyUnicode_AsUTF8 (key); |
| 235 |
|
| 236 |
if (Py_IsNone (value) |
| 237 |
|| (PyUnicode_Check (value) |
| 238 |
&& !strcmp (PyUnicode_AsUTF8 (value), "override"))) { |
| 239 |
/* canonicalize signal name, replacing '-' with '_' */ |
| 240 |
signal_name_canon = g_strdup (signal_name); |
| 241 |
for (c = signal_name_canon; *c; ++c) |
| 242 |
if (*c == '-') *c = '_'; |
| 243 |
if (PyDict_SetItemString (overridden_signals, signal_name_canon, |
| 244 |
key)) { |
| 245 |
g_free (signal_name_canon); |
| 246 |
ret = FALSE; |
| 247 |
break; |
| 248 |
} |
| 249 |
g_free (signal_name_canon); |
| 250 |
|
| 251 |
ret = override_signal (instance_type, signal_name); |
| 252 |
} else { |
| 253 |
ret = create_signal (instance_type, signal_name, value); |
| 254 |
} |
| 255 |
|
| 256 |
if (!ret) break; |
| 257 |
} |
| 258 |
if (ret) |
| 259 |
return overridden_signals; |
| 260 |
else { |
| 261 |
Py_XDECREF (overridden_signals); |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
static GParamSpec * |
| 267 |
create_property (const gchar *prop_name, GType prop_type, const gchar *nick, |
| 268 |
const gchar *blurb, PyObject *args, GParamFlags flags) |
| 269 |
{ |
| 270 |
GParamSpec *pspec = NULL; |
| 271 |
|
| 272 |
switch (G_TYPE_FUNDAMENTAL (prop_type)) { |
| 273 |
case G_TYPE_CHAR: { |
| 274 |
gchar minimum, maximum, default_value; |
| 275 |
|
| 276 |
if (!PyArg_ParseTuple (args, "ccc", &minimum, &maximum, |
| 277 |
&default_value)) |
| 278 |
return NULL; |
| 279 |
pspec = g_param_spec_char (prop_name, nick, blurb, minimum, maximum, |
| 280 |
default_value, flags); |
| 281 |
break; |
| 282 |
} |
| 283 |
case G_TYPE_UCHAR: { |
| 284 |
gchar minimum, maximum, default_value; |
| 285 |
|
| 286 |
if (!PyArg_ParseTuple (args, "ccc", &minimum, &maximum, |
| 287 |
&default_value)) |
| 288 |
return NULL; |
| 289 |
pspec = g_param_spec_uchar (prop_name, nick, blurb, minimum, maximum, |
| 290 |
default_value, flags); |
| 291 |
break; |
| 292 |
} |
| 293 |
case G_TYPE_BOOLEAN: { |
| 294 |
gboolean default_value; |
| 295 |
|
| 296 |
if (!PyArg_ParseTuple (args, "i", &default_value)) return NULL; |
| 297 |
pspec = g_param_spec_boolean (prop_name, nick, blurb, default_value, |
| 298 |
flags); |
| 299 |
break; |
| 300 |
} |
| 301 |
case G_TYPE_INT: { |
| 302 |
gint minimum, maximum, default_value; |
| 303 |
|
| 304 |
if (!PyArg_ParseTuple (args, "iii", &minimum, &maximum, |
| 305 |
&default_value)) |
| 306 |
return NULL; |
| 307 |
pspec = g_param_spec_int (prop_name, nick, blurb, minimum, maximum, |
| 308 |
default_value, flags); |
| 309 |
break; |
| 310 |
} |
| 311 |
case G_TYPE_UINT: { |
| 312 |
guint minimum, maximum, default_value; |
| 313 |
|
| 314 |
if (!PyArg_ParseTuple (args, "III", &minimum, &maximum, |
| 315 |
&default_value)) |
| 316 |
return NULL; |
| 317 |
pspec = g_param_spec_uint (prop_name, nick, blurb, minimum, maximum, |
| 318 |
default_value, flags); |
| 319 |
break; |
| 320 |
} |
| 321 |
case G_TYPE_LONG: { |
| 322 |
glong minimum, maximum, default_value; |
| 323 |
|
| 324 |
if (!PyArg_ParseTuple (args, "lll", &minimum, &maximum, |
| 325 |
&default_value)) |
| 326 |
return NULL; |
| 327 |
pspec = g_param_spec_long (prop_name, nick, blurb, minimum, maximum, |
| 328 |
default_value, flags); |
| 329 |
break; |
| 330 |
} |
| 331 |
case G_TYPE_ULONG: { |
| 332 |
gulong minimum, maximum, default_value; |
| 333 |
|
| 334 |
if (!PyArg_ParseTuple (args, "kkk", &minimum, &maximum, |
| 335 |
&default_value)) |
| 336 |
return NULL; |
| 337 |
pspec = g_param_spec_ulong (prop_name, nick, blurb, minimum, maximum, |
| 338 |
default_value, flags); |
| 339 |
break; |
| 340 |
} |
| 341 |
case G_TYPE_INT64: { |
| 342 |
gint64 minimum, maximum, default_value; |
| 343 |
|
| 344 |
if (!PyArg_ParseTuple (args, "LLL", &minimum, &maximum, |
| 345 |
&default_value)) |
| 346 |
return NULL; |
| 347 |
pspec = g_param_spec_int64 (prop_name, nick, blurb, minimum, maximum, |
| 348 |
default_value, flags); |
| 349 |
break; |
| 350 |
} |
| 351 |
case G_TYPE_UINT64: { |
| 352 |
guint64 minimum, maximum, default_value; |
| 353 |
|
| 354 |
if (!PyArg_ParseTuple (args, "KKK", &minimum, &maximum, |
| 355 |
&default_value)) |
| 356 |
return NULL; |
| 357 |
pspec = g_param_spec_uint64 (prop_name, nick, blurb, minimum, maximum, |
| 358 |
default_value, flags); |
| 359 |
break; |
| 360 |
} |
| 361 |
case G_TYPE_ENUM: { |
| 362 |
gint default_value; |
| 363 |
PyObject *pydefault; |
| 364 |
|
| 365 |
if (!PyArg_ParseTuple (args, "O", &pydefault)) return NULL; |
| 366 |
|
| 367 |
if (pyg_enum_get_value (prop_type, pydefault, (gint *)&default_value)) |
| 368 |
return NULL; |
| 369 |
|
| 370 |
pspec = g_param_spec_enum (prop_name, nick, blurb, prop_type, |
| 371 |
default_value, flags); |
| 372 |
break; |
| 373 |
} |
| 374 |
case G_TYPE_FLAGS: { |
| 375 |
guint default_value; |
| 376 |
PyObject *pydefault; |
| 377 |
|
| 378 |
if (!PyArg_ParseTuple (args, "O", &pydefault)) return NULL; |
| 379 |
|
| 380 |
if (pyg_flags_get_value (prop_type, pydefault, &default_value)) |
| 381 |
return NULL; |
| 382 |
|
| 383 |
pspec = g_param_spec_flags (prop_name, nick, blurb, prop_type, |
| 384 |
default_value, flags); |
| 385 |
break; |
| 386 |
} |
| 387 |
case G_TYPE_FLOAT: { |
| 388 |
gfloat minimum, maximum, default_value; |
| 389 |
|
| 390 |
if (!PyArg_ParseTuple (args, "fff", &minimum, &maximum, |
| 391 |
&default_value)) |
| 392 |
return NULL; |
| 393 |
pspec = g_param_spec_float (prop_name, nick, blurb, minimum, maximum, |
| 394 |
default_value, flags); |
| 395 |
break; |
| 396 |
} |
| 397 |
case G_TYPE_DOUBLE: { |
| 398 |
gdouble minimum, maximum, default_value; |
| 399 |
|
| 400 |
if (!PyArg_ParseTuple (args, "ddd", &minimum, &maximum, |
| 401 |
&default_value)) |
| 402 |
return NULL; |
| 403 |
pspec = g_param_spec_double (prop_name, nick, blurb, minimum, maximum, |
| 404 |
default_value, flags); |
| 405 |
break; |
| 406 |
} |
| 407 |
case G_TYPE_STRING: { |
| 408 |
const gchar *default_value; |
| 409 |
|
| 410 |
if (!PyArg_ParseTuple (args, "z", &default_value)) return NULL; |
| 411 |
pspec = |
| 412 |
g_param_spec_string (prop_name, nick, blurb, default_value, flags); |
| 413 |
break; |
| 414 |
} |
| 415 |
case G_TYPE_PARAM: |
| 416 |
if (!PyArg_ParseTuple (args, "")) return NULL; |
| 417 |
pspec = g_param_spec_param (prop_name, nick, blurb, prop_type, flags); |
| 418 |
break; |
| 419 |
case G_TYPE_BOXED: |
| 420 |
if (!PyArg_ParseTuple (args, "")) return NULL; |
| 421 |
pspec = g_param_spec_boxed (prop_name, nick, blurb, prop_type, flags); |
| 422 |
break; |
| 423 |
case G_TYPE_POINTER: |
| 424 |
if (!PyArg_ParseTuple (args, "")) return NULL; |
| 425 |
if (prop_type == G_TYPE_GTYPE) |
| 426 |
pspec = g_param_spec_gtype (prop_name, nick, blurb, G_TYPE_NONE, |
| 427 |
flags); |
| 428 |
else |
| 429 |
pspec = g_param_spec_pointer (prop_name, nick, blurb, flags); |
| 430 |
break; |
| 431 |
case G_TYPE_OBJECT: |
| 432 |
case G_TYPE_INTERFACE: |
| 433 |
if (!PyArg_ParseTuple (args, "")) return NULL; |
| 434 |
pspec = g_param_spec_object (prop_name, nick, blurb, prop_type, flags); |
| 435 |
break; |
| 436 |
case G_TYPE_VARIANT: { |
| 437 |
PyObject *pydefault; |
| 438 |
GVariant *default_value = NULL; |
| 439 |
|
| 440 |
if (!PyArg_ParseTuple (args, "O", &pydefault)) return NULL; |
| 441 |
if (!Py_IsNone (pydefault)) |
| 442 |
default_value = pyg_boxed_get (pydefault, GVariant); |
| 443 |
pspec = g_param_spec_variant ( |
| 444 |
prop_name, nick, blurb, G_VARIANT_TYPE_ANY, default_value, flags); |
| 445 |
break; |
| 446 |
} |
| 447 |
default: |
| 448 |
/* unhandled pspec type ... */ |
| 449 |
break; |
| 450 |
} |
| 451 |
|
| 452 |
if (!pspec) { |
| 453 |
char buf[128]; |
| 454 |
|
| 455 |
g_snprintf (buf, sizeof (buf), |
| 456 |
"could not create param spec for type %s", |
| 457 |
g_type_name (prop_type)); |
| 458 |
PyErr_SetString (PyExc_TypeError, buf); |
| 459 |
return NULL; |
| 460 |
} |
| 461 |
|
| 462 |
return pspec; |
| 463 |
} |
| 464 |
|
| 465 |
static gboolean |
| 466 |
add_properties (GObjectClass *klass, PyObject *properties) |
| 467 |
{ |
| 468 |
gboolean ret = TRUE; |
| 469 |
Py_ssize_t pos = 0; |
| 470 |
PyObject *key, *value; |
| 471 |
|
| 472 |
while (PyDict_Next (properties, &pos, &key, &value)) { |
| 473 |
const gchar *prop_name; |
| 474 |
GType prop_type; |
| 475 |
const gchar *nick, *blurb; |
| 476 |
GParamFlags flags; |
| 477 |
Py_ssize_t val_length; |
| 478 |
PyObject *slice, *item, *py_prop_type; |
| 479 |
GParamSpec *pspec; |
| 480 |
|
| 481 |
/* values are of format (type,nick,blurb, type_specific_args, flags) */ |
| 482 |
|
| 483 |
if (!PyUnicode_Check (key)) { |
| 484 |
PyErr_SetString (PyExc_TypeError, |
| 485 |
"__gproperties__ keys must be strings"); |
| 486 |
ret = FALSE; |
| 487 |
break; |
| 488 |
} |
| 489 |
prop_name = PyUnicode_AsUTF8 (key); |
| 490 |
|
| 491 |
if (!PyTuple_Check (value)) { |
| 492 |
PyErr_SetString (PyExc_TypeError, |
| 493 |
"__gproperties__ values must be tuples"); |
| 494 |
ret = FALSE; |
| 495 |
break; |
| 496 |
} |
| 497 |
val_length = PyTuple_Size (value); |
| 498 |
if (val_length < 4) { |
| 499 |
PyErr_SetString ( |
| 500 |
PyExc_TypeError, |
| 501 |
"__gproperties__ values must be at least 4 elements long"); |
| 502 |
ret = FALSE; |
| 503 |
break; |
| 504 |
} |
| 505 |
|
| 506 |
slice = PySequence_GetSlice (value, 0, 3); |
| 507 |
if (!slice) { |
| 508 |
ret = FALSE; |
| 509 |
break; |
| 510 |
} |
| 511 |
if (!PyArg_ParseTuple (slice, "Ozz", &py_prop_type, &nick, &blurb)) { |
| 512 |
Py_DECREF (slice); |
| 513 |
ret = FALSE; |
| 514 |
break; |
| 515 |
} |
| 516 |
Py_DECREF (slice); |
| 517 |
prop_type = pyg_type_from_object (py_prop_type); |
| 518 |
if (!prop_type) { |
| 519 |
ret = FALSE; |
| 520 |
break; |
| 521 |
} |
| 522 |
item = PyTuple_GetItem (value, val_length - 1); |
| 523 |
if (!PyLong_Check (item)) { |
| 524 |
PyErr_SetString ( |
| 525 |
PyExc_TypeError, |
| 526 |
"last element in __gproperties__ value tuple must be an int"); |
| 527 |
ret = FALSE; |
| 528 |
break; |
| 529 |
} |
| 530 |
if (!pygi_gint_from_py (item, &flags)) { |
| 531 |
ret = FALSE; |
| 532 |
break; |
| 533 |
} |
| 534 |
|
| 535 |
/* slice is the extra items in the tuple */ |
| 536 |
slice = PySequence_GetSlice (value, 3, val_length - 1); |
| 537 |
pspec = |
| 538 |
create_property (prop_name, prop_type, nick, blurb, slice, flags); |
| 539 |
Py_DECREF (slice); |
| 540 |
|
| 541 |
if (pspec) { |
| 542 |
g_object_class_install_property (klass, 1, pspec); |
| 543 |
} else { |
| 544 |
PyObject *type, *pvalue, *traceback; |
| 545 |
ret = FALSE; |
| 546 |
PyErr_Fetch (&type, &pvalue, &traceback); |
| 547 |
if (PyUnicode_Check (pvalue)) { |
| 548 |
char msg[256]; |
| 549 |
g_snprintf ( |
| 550 |
msg, 256, |
| 551 |
"%s (while registering property '%s' for GType '%s')", |
| 552 |
PyUnicode_AsUTF8 (pvalue), prop_name, |
| 553 |
G_OBJECT_CLASS_NAME (klass)); |
| 554 |
Py_DECREF (pvalue); |
| 555 |
value = PyUnicode_FromString (msg); |
| 556 |
} |
| 557 |
PyErr_Restore (type, pvalue, traceback); |
| 558 |
break; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
return ret; |
| 563 |
} |
| 564 |
|
| 565 |
/* |
| 566 |
* Create a wrapper, but keep the GObject floating intact. |
| 567 |
*/ |
| 568 |
static PyObject * |
| 569 |
pyg_object_new_retain_floating (GObject *object, GObjectClass *klass) |
| 570 |
{ |
| 571 |
PyObject *object_wrapper; |
| 572 |
|
| 573 |
if (g_object_is_floating (object)) { |
| 574 |
g_object_ref (object); |
| 575 |
object_wrapper = pygobject_new_full (object, |
| 576 |
/*steal=*/TRUE, klass); |
| 577 |
g_object_force_floating (object); |
| 578 |
} else { |
| 579 |
object_wrapper = pygobject_new_full (object, |
| 580 |
/*steal=*/FALSE, klass); |
| 581 |
} |
| 582 |
return object_wrapper; |
| 583 |
} |
| 584 |
|
| 585 |
static void |
| 586 |
pyg_object_get_property (GObject *object, guint property_id, GValue *value, |
| 587 |
GParamSpec *pspec) |
| 588 |
{ |
| 589 |
PyObject *object_wrapper, *retval; |
| 590 |
PyGILState_STATE state; |
| 591 |
|
| 592 |
state = PyGILState_Ensure (); |
| 593 |
|
| 594 |
object_wrapper = |
| 595 |
pyg_object_new_retain_floating (object, G_OBJECT_GET_CLASS (object)); |
| 596 |
|
| 597 |
if (object_wrapper == NULL) { |
| 598 |
PyGILState_Release (state); |
| 599 |
return; |
| 600 |
} |
| 601 |
|
| 602 |
retval = pygi_call_do_get_property (object_wrapper, pspec); |
| 603 |
if (retval && pyg_value_from_pyobject (value, retval) < 0) { |
| 604 |
PyErr_Print (); |
| 605 |
} |
| 606 |
Py_DECREF (object_wrapper); |
| 607 |
Py_XDECREF (retval); |
| 608 |
|
| 609 |
PyGILState_Release (state); |
| 610 |
} |
| 611 |
|
| 612 |
static void |
| 613 |
pyg_object_set_property (GObject *object, guint property_id, |
| 614 |
const GValue *value, GParamSpec *pspec) |
| 615 |
{ |
| 616 |
PyObject *object_wrapper, *retval; |
| 617 |
PyObject *py_pspec, *py_value; |
| 618 |
PyGILState_STATE state; |
| 619 |
|
| 620 |
state = PyGILState_Ensure (); |
| 621 |
|
| 622 |
object_wrapper = |
| 623 |
pyg_object_new_retain_floating (object, G_OBJECT_GET_CLASS (object)); |
| 624 |
|
| 625 |
py_pspec = pygi_fundamental_new (pspec); |
| 626 |
py_value = pyg_value_to_pyobject (value, TRUE); |
| 627 |
|
| 628 |
retval = PyObject_CallMethod (object_wrapper, "do_set_property", "OO", |
| 629 |
py_pspec, py_value); |
| 630 |
if (retval) { |
| 631 |
Py_DECREF (retval); |
| 632 |
} else { |
| 633 |
PyErr_Print (); |
| 634 |
} |
| 635 |
|
| 636 |
Py_DECREF (object_wrapper); |
| 637 |
Py_DECREF (py_pspec); |
| 638 |
Py_DECREF (py_value); |
| 639 |
|
| 640 |
PyGILState_Release (state); |
| 641 |
} |
| 642 |
|
| 643 |
static void |
| 644 |
pyg_object_dispose (GObject *object) |
| 645 |
{ |
| 646 |
GObjectClass *klass = G_OBJECT_GET_CLASS (object); |
| 647 |
PyGObjectData *inst_data = pyg_object_peek_inst_data (object); |
| 648 |
|
| 649 |
if (Py_IsInitialized () && inst_data && inst_data->call_do_dispose) { |
| 650 |
PyObject *object_wrapper, *retval; |
| 651 |
PyGILState_STATE state = PyGILState_Ensure (); |
| 652 |
|
| 653 |
/* We can't create a new Python wrapper in PyPy: it will be garbage collected, |
| 654 |
* leaving us with an extra reference on our GObject. */ |
| 655 |
#ifdef PYPY_VERSION |
| 656 |
object_wrapper = g_object_get_qdata (object, pygobject_wrapper_key); |
| 657 |
Py_XINCREF (object_wrapper); |
| 658 |
#else |
| 659 |
object_wrapper = pyg_object_new_retain_floating (object, klass); |
| 660 |
#endif |
| 661 |
|
| 662 |
/* Avoid redundant calls to do_dispose() */ |
| 663 |
inst_data->call_do_dispose = FALSE; |
| 664 |
|
| 665 |
if (object_wrapper != NULL |
| 666 |
&& PyObject_HasAttrString (object_wrapper, "do_dispose")) { |
| 667 |
retval = PyObject_CallMethod (object_wrapper, "do_dispose", NULL); |
| 668 |
if (retval) |
| 669 |
Py_DECREF (retval); |
| 670 |
else |
| 671 |
PyErr_Print (); |
| 672 |
} |
| 673 |
Py_XDECREF (object_wrapper); |
| 674 |
|
| 675 |
PyGILState_Release (state); |
| 676 |
} |
| 677 |
|
| 678 |
/* Find the first non-pygobject dispose method. */ |
| 679 |
while (klass && klass->dispose == pyg_object_dispose) { |
| 680 |
klass = g_type_class_peek_parent (klass); |
| 681 |
} |
| 682 |
|
| 683 |
if (klass && klass->dispose) { |
| 684 |
klass->dispose (object); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
static void |
| 689 |
pyg_object_constructed (GObject *object) |
| 690 |
{ |
| 691 |
GObjectClass *klass = G_OBJECT_GET_CLASS (object); |
| 692 |
PyObject *wrapper, *retval; |
| 693 |
PyGILState_STATE state; |
| 694 |
int instance_init_ref_count; |
| 695 |
|
| 696 |
/* Find the first non-pygobject constructed method. */ |
| 697 |
while (klass && klass->constructed == pyg_object_constructed) { |
| 698 |
klass = g_type_class_peek_parent (klass); |
| 699 |
} |
| 700 |
|
| 701 |
if (klass && klass->constructed) { |
| 702 |
klass->constructed (object); |
| 703 |
} |
| 704 |
|
| 705 |
state = PyGILState_Ensure (); |
| 706 |
|
| 707 |
wrapper = (PyObject *)g_object_get_qdata (object, pygobject_wrapper_key); |
| 708 |
g_assert (wrapper != NULL); |
| 709 |
|
| 710 |
if (!PyErr_Occurred () |
| 711 |
&& PyObject_HasAttrString (wrapper, "do_constructed")) { |
| 712 |
retval = PyObject_CallMethod (wrapper, "do_constructed", NULL); |
| 713 |
Py_XDECREF (retval); |
| 714 |
} |
| 715 |
|
| 716 |
/* Release the reference obtained in pygobject__g_instance_init(). */ |
| 717 |
instance_init_ref_count = GPOINTER_TO_INT ( |
| 718 |
g_object_get_qdata (object, pygobject_instance_init_ref_count)); |
| 719 |
for (int i = 0; i < instance_init_ref_count; i++) Py_DECREF (wrapper); |
| 720 |
g_object_set_qdata (object, pygobject_instance_init_ref_count, NULL); |
| 721 |
|
| 722 |
#ifdef PYPY_VERSION |
| 723 |
/* Force a new wrapper next time the wrapper is retrieved. |
| 724 |
* Somehow if we keep this wrapper around we may end up refering to |
| 725 |
* a semi-destroyed wrapper object. */ |
| 726 |
g_object_set_qdata_full (object, pygobject_wrapper_key, NULL, NULL); |
| 727 |
#endif |
| 728 |
|
| 729 |
PyGILState_Release (state); |
| 730 |
} |
| 731 |
|
| 732 |
void |
| 733 |
pygobject__g_instance_init (GTypeInstance *instance, gpointer g_class) |
| 734 |
{ |
| 735 |
GObject *object; |
| 736 |
PyObject *wrapper, *result; |
| 737 |
PyGILState_STATE state; |
| 738 |
gboolean needs_init = FALSE; |
| 739 |
|
| 740 |
g_return_if_fail (G_IS_OBJECT (instance)); |
| 741 |
|
| 742 |
object = (GObject *)instance; |
| 743 |
|
| 744 |
wrapper = g_object_get_qdata (object, pygobject_wrapper_key); |
| 745 |
|
| 746 |
if (wrapper == NULL) { |
| 747 |
wrapper = pygobject_init_wrapper_get (); |
| 748 |
if (wrapper && ((PyGObject *)wrapper)->obj == NULL) { |
| 749 |
((PyGObject *)wrapper)->obj = object; |
| 750 |
pygobject_register_wrapper (wrapper); |
| 751 |
} |
| 752 |
} |
| 753 |
pygobject_init_wrapper_set (NULL); |
| 754 |
|
| 755 |
state = PyGILState_Ensure (); |
| 756 |
|
| 757 |
if (wrapper == NULL) { |
| 758 |
/* This looks like a python object created through g_object_new(). |
| 759 |
* we have no python wrapper, so create it now. */ |
| 760 |
|
| 761 |
wrapper = pyg_object_new_retain_floating (object, g_class); |
| 762 |
needs_init = TRUE; |
| 763 |
} |
| 764 |
|
| 765 |
/* XXX: used for Gtk.Template */ |
| 766 |
gboolean is_final_subclass = G_OBJECT_TYPE (object) |
| 767 |
== G_OBJECT_CLASS_TYPE (g_class); |
| 768 |
if (is_final_subclass |
| 769 |
&& PyObject_HasAttrString ((PyObject *)Py_TYPE (wrapper), |
| 770 |
"__dontuse_ginstance_init__")) { |
| 771 |
gboolean was_floating = g_object_is_floating (object); |
| 772 |
g_object_ref_sink (object); |
| 773 |
|
| 774 |
result = |
| 775 |
PyObject_CallMethod (wrapper, "__dontuse_ginstance_init__", NULL); |
| 776 |
if (result == NULL) |
| 777 |
PyErr_Print (); |
| 778 |
else |
| 779 |
Py_DECREF (result); |
| 780 |
|
| 781 |
if (was_floating) |
| 782 |
g_object_force_floating (object); |
| 783 |
else |
| 784 |
g_object_unref (object); |
| 785 |
} |
| 786 |
|
| 787 |
if (needs_init) { |
| 788 |
result = PyObject_CallMethod (wrapper, "__init__", NULL); |
| 789 |
if (result == NULL) |
| 790 |
PyErr_Print (); |
| 791 |
else |
| 792 |
Py_DECREF (result); |
| 793 |
|
| 794 |
/* The wrapper's reference will be released in pyg_object_constructed(). */ |
| 795 |
g_object_set_qdata (object, pygobject_instance_init_ref_count, |
| 796 |
GINT_TO_POINTER (1)); |
| 797 |
} else { |
| 798 |
int instance_init_ref_count = GPOINTER_TO_INT ( |
| 799 |
g_object_get_qdata (object, pygobject_instance_init_ref_count)); |
| 800 |
|
| 801 |
/* Take an extra reference, will be released in pyg_object_constructed(). */ |
| 802 |
Py_INCREF (wrapper); |
| 803 |
g_object_set_qdata (object, pygobject_instance_init_ref_count, |
| 804 |
GINT_TO_POINTER (instance_init_ref_count + 1)); |
| 805 |
} |
| 806 |
|
| 807 |
PyGILState_Release (state); |
| 808 |
} |
| 809 |
|
| 810 |
void |
| 811 |
pygobject__g_class_init (GObjectClass *class, PyObject *py_class) |
| 812 |
{ |
| 813 |
PyObject *gproperties, *gsignals, *overridden_signals; |
| 814 |
PyObject *class_dict = ((PyTypeObject *)py_class)->tp_dict; |
| 815 |
|
| 816 |
class->set_property = pyg_object_set_property; |
| 817 |
class->get_property = pyg_object_get_property; |
| 818 |
class->constructed = pyg_object_constructed; |
| 819 |
class->dispose = pyg_object_dispose; |
| 820 |
|
| 821 |
/* install signals */ |
| 822 |
/* we look this up in the instance dictionary, so we don't |
| 823 |
* accidentally get a parent type's __gsignals__ attribute. */ |
| 824 |
gsignals = PyDict_GetItemString (class_dict, "__gsignals__"); |
| 825 |
if (gsignals) { |
| 826 |
if (!PyDict_Check (gsignals)) { |
| 827 |
PyErr_SetString (PyExc_TypeError, |
| 828 |
"__gsignals__ attribute not a dict!"); |
| 829 |
return; |
| 830 |
} |
| 831 |
if (!(overridden_signals = add_signals (class, gsignals))) { |
| 832 |
return; |
| 833 |
} |
| 834 |
if (PyDict_SetItemString (class_dict, "__gsignals__", |
| 835 |
overridden_signals)) { |
| 836 |
return; |
| 837 |
} |
| 838 |
Py_DECREF (overridden_signals); |
| 839 |
|
| 840 |
PyDict_DelItemString (class_dict, "__gsignals__"); |
| 841 |
} else { |
| 842 |
PyErr_Clear (); |
| 843 |
} |
| 844 |
|
| 845 |
/* install properties */ |
| 846 |
/* we look this up in the instance dictionary, so we don't |
| 847 |
* accidentally get a parent type's __gproperties__ attribute. */ |
| 848 |
gproperties = PyDict_GetItemString (class_dict, "__gproperties__"); |
| 849 |
if (gproperties) { |
| 850 |
if (!PyDict_Check (gproperties)) { |
| 851 |
PyErr_SetString (PyExc_TypeError, |
| 852 |
"__gproperties__ attribute not a dict!"); |
| 853 |
return; |
| 854 |
} |
| 855 |
if (!add_properties (class, gproperties)) { |
| 856 |
return; |
| 857 |
} |
| 858 |
PyDict_DelItemString (class_dict, "__gproperties__"); |
| 859 |
/* Borrowed reference. Py_DECREF(gproperties); */ |
| 860 |
} else { |
| 861 |
PyErr_Clear (); |
| 862 |
} |
| 863 |
} |