Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1 | /* Descriptors -- a new, flexible way to describe attributes */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | #include "structmember.h" /* Why is this not included in Python.h? */ |
| 5 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 6 | static void |
| 7 | descr_dealloc(PyDescrObject *descr) |
| 8 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 9 | _PyObject_GC_UNTRACK(descr); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 10 | Py_XDECREF(descr->d_type); |
| 11 | Py_XDECREF(descr->d_name); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 12 | PyObject_GC_Del(descr); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | static char * |
| 16 | descr_name(PyDescrObject *descr) |
| 17 | { |
| 18 | if (descr->d_name != NULL && PyString_Check(descr->d_name)) |
| 19 | return PyString_AS_STRING(descr->d_name); |
| 20 | else |
| 21 | return "?"; |
| 22 | } |
| 23 | |
| 24 | static PyObject * |
| 25 | descr_repr(PyDescrObject *descr, char *format) |
| 26 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 27 | return PyString_FromFormat(format, descr_name(descr), |
| 28 | descr->d_type->tp_name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static PyObject * |
| 32 | method_repr(PyMethodDescrObject *descr) |
| 33 | { |
| 34 | return descr_repr((PyDescrObject *)descr, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 35 | "<method '%s' of '%s' objects>"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static PyObject * |
| 39 | member_repr(PyMemberDescrObject *descr) |
| 40 | { |
| 41 | return descr_repr((PyDescrObject *)descr, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 42 | "<member '%s' of '%s' objects>"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static PyObject * |
| 46 | getset_repr(PyGetSetDescrObject *descr) |
| 47 | { |
| 48 | return descr_repr((PyDescrObject *)descr, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 49 | "<attribute '%s' of '%s' objects>"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static PyObject * |
| 53 | wrapper_repr(PyWrapperDescrObject *descr) |
| 54 | { |
| 55 | return descr_repr((PyDescrObject *)descr, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 56 | "<slot wrapper '%s' of '%s' objects>"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static int |
| 60 | descr_check(PyDescrObject *descr, PyObject *obj, PyTypeObject *type, |
Guido van Rossum | 8098ddb | 2001-08-16 08:27:33 +0000 | [diff] [blame] | 61 | PyObject **pres) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 62 | { |
Guido van Rossum | 8098ddb | 2001-08-16 08:27:33 +0000 | [diff] [blame] | 63 | if (obj == NULL || (obj == Py_None && type != Py_None->ob_type)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 64 | Py_INCREF(descr); |
| 65 | *pres = (PyObject *)descr; |
| 66 | return 1; |
| 67 | } |
| 68 | if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) { |
| 69 | PyErr_Format(PyExc_TypeError, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 70 | "descriptor '%s' for '%s' objects " |
| 71 | "doesn't apply to '%s' object", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 72 | descr_name((PyDescrObject *)descr), |
| 73 | descr->d_type->tp_name, |
| 74 | obj->ob_type->tp_name); |
| 75 | *pres = NULL; |
| 76 | return 1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static PyObject * |
| 82 | method_get(PyMethodDescrObject *descr, PyObject *obj, PyTypeObject *type) |
| 83 | { |
| 84 | PyObject *res; |
| 85 | |
| 86 | if (descr_check((PyDescrObject *)descr, obj, type, &res)) |
| 87 | return res; |
| 88 | return PyCFunction_New(descr->d_method, obj); |
| 89 | } |
| 90 | |
| 91 | static PyObject * |
| 92 | member_get(PyMemberDescrObject *descr, PyObject *obj, PyTypeObject *type) |
| 93 | { |
| 94 | PyObject *res; |
| 95 | |
| 96 | if (descr_check((PyDescrObject *)descr, obj, type, &res)) |
| 97 | return res; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 98 | return PyMember_GetOne((char *)obj, descr->d_member); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static PyObject * |
| 102 | getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyTypeObject *type) |
| 103 | { |
| 104 | PyObject *res; |
| 105 | |
| 106 | if (descr_check((PyDescrObject *)descr, obj, type, &res)) |
| 107 | return res; |
| 108 | if (descr->d_getset->get != NULL) |
| 109 | return descr->d_getset->get(obj, descr->d_getset->closure); |
| 110 | PyErr_Format(PyExc_TypeError, |
| 111 | "attribute '%300s' of '%.100s' objects is not readable", |
| 112 | descr_name((PyDescrObject *)descr), |
| 113 | descr->d_type->tp_name); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | static PyObject * |
| 118 | wrapper_get(PyWrapperDescrObject *descr, PyObject *obj, PyTypeObject *type) |
| 119 | { |
| 120 | PyObject *res; |
| 121 | |
| 122 | if (descr_check((PyDescrObject *)descr, obj, type, &res)) |
| 123 | return res; |
| 124 | return PyWrapper_New((PyObject *)descr, obj); |
| 125 | } |
| 126 | |
| 127 | static int |
| 128 | descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value, |
| 129 | int *pres) |
| 130 | { |
| 131 | assert(obj != NULL); |
| 132 | if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) { |
| 133 | PyErr_Format(PyExc_TypeError, |
| 134 | "descriptor '%.200s' for '%.100s' objects " |
| 135 | "doesn't apply to '%.100s' object", |
| 136 | descr_name(descr), |
| 137 | descr->d_type->tp_name, |
| 138 | obj->ob_type->tp_name); |
| 139 | *pres = -1; |
| 140 | return 1; |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int |
| 146 | member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value) |
| 147 | { |
| 148 | int res; |
| 149 | |
| 150 | if (descr_setcheck((PyDescrObject *)descr, obj, value, &res)) |
| 151 | return res; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 152 | return PyMember_SetOne((char *)obj, descr->d_member, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int |
| 156 | getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value) |
| 157 | { |
| 158 | int res; |
| 159 | |
| 160 | if (descr_setcheck((PyDescrObject *)descr, obj, value, &res)) |
| 161 | return res; |
| 162 | if (descr->d_getset->set != NULL) |
| 163 | return descr->d_getset->set(obj, value, |
| 164 | descr->d_getset->closure); |
| 165 | PyErr_Format(PyExc_TypeError, |
| 166 | "attribute '%300s' of '%.100s' objects is not writable", |
| 167 | descr_name((PyDescrObject *)descr), |
| 168 | descr->d_type->tp_name); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | static PyObject * |
| 173 | methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds) |
| 174 | { |
| 175 | int argc; |
| 176 | PyObject *self, *func, *result; |
| 177 | |
| 178 | /* Make sure that the first argument is acceptable as 'self' */ |
| 179 | assert(PyTuple_Check(args)); |
| 180 | argc = PyTuple_GET_SIZE(args); |
| 181 | if (argc < 1) { |
| 182 | PyErr_Format(PyExc_TypeError, |
| 183 | "descriptor '%.300s' of '%.100s' " |
| 184 | "object needs an argument", |
| 185 | descr_name((PyDescrObject *)descr), |
| 186 | descr->d_type->tp_name); |
| 187 | return NULL; |
| 188 | } |
| 189 | self = PyTuple_GET_ITEM(args, 0); |
| 190 | if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) { |
| 191 | PyErr_Format(PyExc_TypeError, |
| 192 | "descriptor '%.200s' " |
| 193 | "requires a '%.100s' object " |
| 194 | "but received a '%.100s'", |
| 195 | descr_name((PyDescrObject *)descr), |
| 196 | descr->d_type->tp_name, |
| 197 | self->ob_type->tp_name); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | func = PyCFunction_New(descr->d_method, self); |
| 202 | if (func == NULL) |
| 203 | return NULL; |
| 204 | args = PyTuple_GetSlice(args, 1, argc); |
| 205 | if (args == NULL) { |
| 206 | Py_DECREF(func); |
| 207 | return NULL; |
| 208 | } |
| 209 | result = PyEval_CallObjectWithKeywords(func, args, kwds); |
| 210 | Py_DECREF(args); |
| 211 | Py_DECREF(func); |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | static PyObject * |
| 216 | wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds) |
| 217 | { |
| 218 | int argc; |
| 219 | PyObject *self, *func, *result; |
| 220 | |
| 221 | /* Make sure that the first argument is acceptable as 'self' */ |
| 222 | assert(PyTuple_Check(args)); |
| 223 | argc = PyTuple_GET_SIZE(args); |
| 224 | if (argc < 1) { |
| 225 | PyErr_Format(PyExc_TypeError, |
| 226 | "descriptor '%.300s' of '%.100s' " |
| 227 | "object needs an argument", |
| 228 | descr_name((PyDescrObject *)descr), |
| 229 | descr->d_type->tp_name); |
| 230 | return NULL; |
| 231 | } |
| 232 | self = PyTuple_GET_ITEM(args, 0); |
| 233 | if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) { |
| 234 | PyErr_Format(PyExc_TypeError, |
| 235 | "descriptor '%.200s' " |
| 236 | "requires a '%.100s' object " |
| 237 | "but received a '%.100s'", |
| 238 | descr_name((PyDescrObject *)descr), |
| 239 | descr->d_type->tp_name, |
| 240 | self->ob_type->tp_name); |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | func = PyWrapper_New((PyObject *)descr, self); |
| 245 | if (func == NULL) |
| 246 | return NULL; |
| 247 | args = PyTuple_GetSlice(args, 1, argc); |
| 248 | if (args == NULL) { |
| 249 | Py_DECREF(func); |
| 250 | return NULL; |
| 251 | } |
| 252 | result = PyEval_CallObjectWithKeywords(func, args, kwds); |
| 253 | Py_DECREF(args); |
| 254 | Py_DECREF(func); |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | static PyObject * |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 259 | method_get_doc(PyMethodDescrObject *descr, void *closure) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 260 | { |
| 261 | if (descr->d_method->ml_doc == NULL) { |
| 262 | Py_INCREF(Py_None); |
| 263 | return Py_None; |
| 264 | } |
| 265 | return PyString_FromString(descr->d_method->ml_doc); |
| 266 | } |
| 267 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 268 | static PyMemberDef descr_members[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 269 | {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY}, |
| 270 | {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY}, |
| 271 | {0} |
| 272 | }; |
| 273 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 274 | static PyGetSetDef method_getset[] = { |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 275 | {"__doc__", (getter)method_get_doc}, |
| 276 | {0} |
| 277 | }; |
| 278 | |
| 279 | static PyObject * |
| 280 | member_get_doc(PyMemberDescrObject *descr, void *closure) |
| 281 | { |
| 282 | if (descr->d_member->doc == NULL) { |
| 283 | Py_INCREF(Py_None); |
| 284 | return Py_None; |
| 285 | } |
| 286 | return PyString_FromString(descr->d_member->doc); |
| 287 | } |
| 288 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 289 | static PyGetSetDef member_getset[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 290 | {"__doc__", (getter)member_get_doc}, |
| 291 | {0} |
| 292 | }; |
| 293 | |
| 294 | static PyObject * |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 295 | getset_get_doc(PyGetSetDescrObject *descr, void *closure) |
| 296 | { |
| 297 | if (descr->d_getset->doc == NULL) { |
| 298 | Py_INCREF(Py_None); |
| 299 | return Py_None; |
| 300 | } |
| 301 | return PyString_FromString(descr->d_getset->doc); |
| 302 | } |
| 303 | |
| 304 | static PyGetSetDef getset_getset[] = { |
| 305 | {"__doc__", (getter)getset_get_doc}, |
| 306 | {0} |
| 307 | }; |
| 308 | |
| 309 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 310 | wrapper_get_doc(PyWrapperDescrObject *descr, void *closure) |
| 311 | { |
| 312 | if (descr->d_base->doc == NULL) { |
| 313 | Py_INCREF(Py_None); |
| 314 | return Py_None; |
| 315 | } |
| 316 | return PyString_FromString(descr->d_base->doc); |
| 317 | } |
| 318 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 319 | static PyGetSetDef wrapper_getset[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 320 | {"__doc__", (getter)wrapper_get_doc}, |
| 321 | {0} |
| 322 | }; |
| 323 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 324 | static int |
| 325 | descr_traverse(PyObject *self, visitproc visit, void *arg) |
| 326 | { |
| 327 | PyDescrObject *descr = (PyDescrObject *)self; |
| 328 | int err; |
| 329 | |
| 330 | if (descr->d_type) { |
| 331 | err = visit((PyObject *)(descr->d_type), arg); |
| 332 | if (err) |
| 333 | return err; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 338 | static PyTypeObject PyMethodDescr_Type = { |
| 339 | PyObject_HEAD_INIT(&PyType_Type) |
| 340 | 0, |
| 341 | "method_descriptor", |
| 342 | sizeof(PyMethodDescrObject), |
| 343 | 0, |
| 344 | (destructor)descr_dealloc, /* tp_dealloc */ |
| 345 | 0, /* tp_print */ |
| 346 | 0, /* tp_getattr */ |
| 347 | 0, /* tp_setattr */ |
| 348 | 0, /* tp_compare */ |
| 349 | (reprfunc)method_repr, /* tp_repr */ |
| 350 | 0, /* tp_as_number */ |
| 351 | 0, /* tp_as_sequence */ |
| 352 | 0, /* tp_as_mapping */ |
| 353 | 0, /* tp_hash */ |
| 354 | (ternaryfunc)methoddescr_call, /* tp_call */ |
| 355 | 0, /* tp_str */ |
| 356 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 357 | 0, /* tp_setattro */ |
| 358 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 359 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 360 | 0, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 361 | descr_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 362 | 0, /* tp_clear */ |
| 363 | 0, /* tp_richcompare */ |
| 364 | 0, /* tp_weaklistoffset */ |
| 365 | 0, /* tp_iter */ |
| 366 | 0, /* tp_iternext */ |
| 367 | 0, /* tp_methods */ |
| 368 | descr_members, /* tp_members */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 369 | method_getset, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 370 | 0, /* tp_base */ |
| 371 | 0, /* tp_dict */ |
| 372 | (descrgetfunc)method_get, /* tp_descr_get */ |
| 373 | 0, /* tp_descr_set */ |
| 374 | }; |
| 375 | |
| 376 | static PyTypeObject PyMemberDescr_Type = { |
| 377 | PyObject_HEAD_INIT(&PyType_Type) |
| 378 | 0, |
| 379 | "member_descriptor", |
| 380 | sizeof(PyMemberDescrObject), |
| 381 | 0, |
| 382 | (destructor)descr_dealloc, /* tp_dealloc */ |
| 383 | 0, /* tp_print */ |
| 384 | 0, /* tp_getattr */ |
| 385 | 0, /* tp_setattr */ |
| 386 | 0, /* tp_compare */ |
| 387 | (reprfunc)member_repr, /* tp_repr */ |
| 388 | 0, /* tp_as_number */ |
| 389 | 0, /* tp_as_sequence */ |
| 390 | 0, /* tp_as_mapping */ |
| 391 | 0, /* tp_hash */ |
| 392 | (ternaryfunc)0, /* tp_call */ |
| 393 | 0, /* tp_str */ |
| 394 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 395 | 0, /* tp_setattro */ |
| 396 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 397 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 398 | 0, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 399 | descr_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 400 | 0, /* tp_clear */ |
| 401 | 0, /* tp_richcompare */ |
| 402 | 0, /* tp_weaklistoffset */ |
| 403 | 0, /* tp_iter */ |
| 404 | 0, /* tp_iternext */ |
| 405 | 0, /* tp_methods */ |
| 406 | descr_members, /* tp_members */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 407 | member_getset, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 408 | 0, /* tp_base */ |
| 409 | 0, /* tp_dict */ |
| 410 | (descrgetfunc)member_get, /* tp_descr_get */ |
| 411 | (descrsetfunc)member_set, /* tp_descr_set */ |
| 412 | }; |
| 413 | |
| 414 | static PyTypeObject PyGetSetDescr_Type = { |
| 415 | PyObject_HEAD_INIT(&PyType_Type) |
| 416 | 0, |
| 417 | "getset_descriptor", |
| 418 | sizeof(PyGetSetDescrObject), |
| 419 | 0, |
| 420 | (destructor)descr_dealloc, /* tp_dealloc */ |
| 421 | 0, /* tp_print */ |
| 422 | 0, /* tp_getattr */ |
| 423 | 0, /* tp_setattr */ |
| 424 | 0, /* tp_compare */ |
| 425 | (reprfunc)getset_repr, /* tp_repr */ |
| 426 | 0, /* tp_as_number */ |
| 427 | 0, /* tp_as_sequence */ |
| 428 | 0, /* tp_as_mapping */ |
| 429 | 0, /* tp_hash */ |
| 430 | (ternaryfunc)0, /* tp_call */ |
| 431 | 0, /* tp_str */ |
| 432 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 433 | 0, /* tp_setattro */ |
| 434 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 435 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 436 | 0, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 437 | descr_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 438 | 0, /* tp_clear */ |
| 439 | 0, /* tp_richcompare */ |
| 440 | 0, /* tp_weaklistoffset */ |
| 441 | 0, /* tp_iter */ |
| 442 | 0, /* tp_iternext */ |
| 443 | 0, /* tp_methods */ |
| 444 | descr_members, /* tp_members */ |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 445 | getset_getset, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 446 | 0, /* tp_base */ |
| 447 | 0, /* tp_dict */ |
| 448 | (descrgetfunc)getset_get, /* tp_descr_get */ |
| 449 | (descrsetfunc)getset_set, /* tp_descr_set */ |
| 450 | }; |
| 451 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 452 | PyTypeObject PyWrapperDescr_Type = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 453 | PyObject_HEAD_INIT(&PyType_Type) |
| 454 | 0, |
| 455 | "wrapper_descriptor", |
| 456 | sizeof(PyWrapperDescrObject), |
| 457 | 0, |
| 458 | (destructor)descr_dealloc, /* tp_dealloc */ |
| 459 | 0, /* tp_print */ |
| 460 | 0, /* tp_getattr */ |
| 461 | 0, /* tp_setattr */ |
| 462 | 0, /* tp_compare */ |
| 463 | (reprfunc)wrapper_repr, /* tp_repr */ |
| 464 | 0, /* tp_as_number */ |
| 465 | 0, /* tp_as_sequence */ |
| 466 | 0, /* tp_as_mapping */ |
| 467 | 0, /* tp_hash */ |
| 468 | (ternaryfunc)wrapperdescr_call, /* tp_call */ |
| 469 | 0, /* tp_str */ |
| 470 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 471 | 0, /* tp_setattro */ |
| 472 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 473 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 474 | 0, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 475 | descr_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 476 | 0, /* tp_clear */ |
| 477 | 0, /* tp_richcompare */ |
| 478 | 0, /* tp_weaklistoffset */ |
| 479 | 0, /* tp_iter */ |
| 480 | 0, /* tp_iternext */ |
| 481 | 0, /* tp_methods */ |
| 482 | descr_members, /* tp_members */ |
| 483 | wrapper_getset, /* tp_getset */ |
| 484 | 0, /* tp_base */ |
| 485 | 0, /* tp_dict */ |
| 486 | (descrgetfunc)wrapper_get, /* tp_descr_get */ |
| 487 | 0, /* tp_descr_set */ |
| 488 | }; |
| 489 | |
| 490 | static PyDescrObject * |
| 491 | descr_new(PyTypeObject *descrtype, PyTypeObject *type, char *name) |
| 492 | { |
| 493 | PyDescrObject *descr; |
| 494 | |
| 495 | descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0); |
| 496 | if (descr != NULL) { |
| 497 | Py_XINCREF(type); |
| 498 | descr->d_type = type; |
| 499 | descr->d_name = PyString_InternFromString(name); |
| 500 | if (descr->d_name == NULL) { |
| 501 | Py_DECREF(descr); |
| 502 | descr = NULL; |
| 503 | } |
| 504 | } |
| 505 | return descr; |
| 506 | } |
| 507 | |
| 508 | PyObject * |
| 509 | PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method) |
| 510 | { |
| 511 | PyMethodDescrObject *descr; |
| 512 | |
| 513 | descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type, |
| 514 | type, method->ml_name); |
| 515 | if (descr != NULL) |
| 516 | descr->d_method = method; |
| 517 | return (PyObject *)descr; |
| 518 | } |
| 519 | |
| 520 | PyObject * |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 521 | PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 522 | { |
| 523 | PyMemberDescrObject *descr; |
| 524 | |
| 525 | descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type, |
| 526 | type, member->name); |
| 527 | if (descr != NULL) |
| 528 | descr->d_member = member; |
| 529 | return (PyObject *)descr; |
| 530 | } |
| 531 | |
| 532 | PyObject * |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 533 | PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 534 | { |
| 535 | PyGetSetDescrObject *descr; |
| 536 | |
| 537 | descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type, |
| 538 | type, getset->name); |
| 539 | if (descr != NULL) |
| 540 | descr->d_getset = getset; |
| 541 | return (PyObject *)descr; |
| 542 | } |
| 543 | |
| 544 | PyObject * |
| 545 | PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped) |
| 546 | { |
| 547 | PyWrapperDescrObject *descr; |
| 548 | |
| 549 | descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type, |
| 550 | type, base->name); |
| 551 | if (descr != NULL) { |
| 552 | descr->d_base = base; |
| 553 | descr->d_wrapped = wrapped; |
| 554 | } |
| 555 | return (PyObject *)descr; |
| 556 | } |
| 557 | |
| 558 | int |
| 559 | PyDescr_IsData(PyObject *d) |
| 560 | { |
| 561 | return d->ob_type->tp_descr_set != NULL; |
| 562 | } |
| 563 | |
| 564 | |
| 565 | /* --- Readonly proxy for dictionaries (actually any mapping) --- */ |
| 566 | |
| 567 | /* This has no reason to be in this file except that adding new files is a |
| 568 | bit of a pain */ |
| 569 | |
| 570 | typedef struct { |
| 571 | PyObject_HEAD |
| 572 | PyObject *dict; |
| 573 | } proxyobject; |
| 574 | |
| 575 | static int |
| 576 | proxy_len(proxyobject *pp) |
| 577 | { |
| 578 | return PyObject_Size(pp->dict); |
| 579 | } |
| 580 | |
| 581 | static PyObject * |
| 582 | proxy_getitem(proxyobject *pp, PyObject *key) |
| 583 | { |
| 584 | return PyObject_GetItem(pp->dict, key); |
| 585 | } |
| 586 | |
| 587 | static PyMappingMethods proxy_as_mapping = { |
| 588 | (inquiry)proxy_len, /* mp_length */ |
| 589 | (binaryfunc)proxy_getitem, /* mp_subscript */ |
| 590 | 0, /* mp_ass_subscript */ |
| 591 | }; |
| 592 | |
| 593 | static int |
| 594 | proxy_contains(proxyobject *pp, PyObject *key) |
| 595 | { |
| 596 | return PySequence_Contains(pp->dict, key); |
| 597 | } |
| 598 | |
| 599 | static PySequenceMethods proxy_as_sequence = { |
| 600 | 0, /* sq_length */ |
| 601 | 0, /* sq_concat */ |
| 602 | 0, /* sq_repeat */ |
| 603 | 0, /* sq_item */ |
| 604 | 0, /* sq_slice */ |
| 605 | 0, /* sq_ass_item */ |
| 606 | 0, /* sq_ass_slice */ |
| 607 | (objobjproc)proxy_contains, /* sq_contains */ |
| 608 | 0, /* sq_inplace_concat */ |
| 609 | 0, /* sq_inplace_repeat */ |
| 610 | }; |
| 611 | |
| 612 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 613 | proxy_has_key(proxyobject *pp, PyObject *key) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 614 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 615 | return PyInt_FromLong(PySequence_Contains(pp->dict, key)); |
| 616 | } |
| 617 | |
| 618 | static PyObject * |
| 619 | proxy_get(proxyobject *pp, PyObject *args) |
| 620 | { |
| 621 | PyObject *key, *def = Py_None; |
| 622 | |
| 623 | if (!PyArg_ParseTuple(args, "O|O:get", &key, &def)) |
| 624 | return NULL; |
| 625 | return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def); |
| 626 | } |
| 627 | |
| 628 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 629 | proxy_keys(proxyobject *pp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 630 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 631 | return PyMapping_Keys(pp->dict); |
| 632 | } |
| 633 | |
| 634 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 635 | proxy_values(proxyobject *pp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 636 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 637 | return PyMapping_Values(pp->dict); |
| 638 | } |
| 639 | |
| 640 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 641 | proxy_items(proxyobject *pp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 642 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 643 | return PyMapping_Items(pp->dict); |
| 644 | } |
| 645 | |
| 646 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 647 | proxy_copy(proxyobject *pp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 648 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 649 | return PyObject_CallMethod(pp->dict, "copy", NULL); |
| 650 | } |
| 651 | |
| 652 | static PyMethodDef proxy_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 653 | {"has_key", (PyCFunction)proxy_has_key, METH_O, "XXX"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 654 | {"get", (PyCFunction)proxy_get, METH_VARARGS, "XXX"}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 655 | {"keys", (PyCFunction)proxy_keys, METH_NOARGS, "XXX"}, |
| 656 | {"values", (PyCFunction)proxy_values, METH_NOARGS, "XXX"}, |
| 657 | {"items", (PyCFunction)proxy_items, METH_NOARGS, "XXX"}, |
| 658 | {"copy", (PyCFunction)proxy_copy, METH_NOARGS, "XXX"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 659 | {0} |
| 660 | }; |
| 661 | |
| 662 | static void |
| 663 | proxy_dealloc(proxyobject *pp) |
| 664 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 665 | _PyObject_GC_UNTRACK(pp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 666 | Py_DECREF(pp->dict); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 667 | PyObject_GC_Del(pp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static PyObject * |
| 671 | proxy_getiter(proxyobject *pp) |
| 672 | { |
| 673 | return PyObject_GetIter(pp->dict); |
| 674 | } |
| 675 | |
Neil Schemenauer | 2677512 | 2001-10-21 22:26:43 +0000 | [diff] [blame^] | 676 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 677 | proxy_str(proxyobject *pp) |
| 678 | { |
| 679 | return PyObject_Str(pp->dict); |
| 680 | } |
| 681 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 682 | static int |
| 683 | proxy_traverse(PyObject *self, visitproc visit, void *arg) |
| 684 | { |
| 685 | proxyobject *pp = (proxyobject *)self; |
| 686 | int err; |
| 687 | |
| 688 | if (pp->dict) { |
| 689 | err = visit(pp->dict, arg); |
| 690 | if (err) |
| 691 | return err; |
| 692 | } |
| 693 | return 0; |
| 694 | } |
| 695 | |
Neil Schemenauer | 2677512 | 2001-10-21 22:26:43 +0000 | [diff] [blame^] | 696 | static PyTypeObject proxytype = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 697 | PyObject_HEAD_INIT(&PyType_Type) |
| 698 | 0, /* ob_size */ |
| 699 | "dict-proxy", /* tp_name */ |
| 700 | sizeof(proxyobject), /* tp_basicsize */ |
| 701 | 0, /* tp_itemsize */ |
| 702 | /* methods */ |
| 703 | (destructor)proxy_dealloc, /* tp_dealloc */ |
| 704 | 0, /* tp_print */ |
| 705 | 0, /* tp_getattr */ |
| 706 | 0, /* tp_setattr */ |
| 707 | 0, /* tp_compare */ |
| 708 | 0, /* tp_repr */ |
| 709 | 0, /* tp_as_number */ |
| 710 | &proxy_as_sequence, /* tp_as_sequence */ |
| 711 | &proxy_as_mapping, /* tp_as_mapping */ |
| 712 | 0, /* tp_hash */ |
| 713 | 0, /* tp_call */ |
| 714 | (reprfunc)proxy_str, /* tp_str */ |
| 715 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 716 | 0, /* tp_setattro */ |
| 717 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 718 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 719 | 0, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 720 | proxy_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 721 | 0, /* tp_clear */ |
| 722 | 0, /* tp_richcompare */ |
| 723 | 0, /* tp_weaklistoffset */ |
| 724 | (getiterfunc)proxy_getiter, /* tp_iter */ |
| 725 | 0, /* tp_iternext */ |
| 726 | proxy_methods, /* tp_methods */ |
| 727 | 0, /* tp_members */ |
| 728 | 0, /* tp_getset */ |
| 729 | 0, /* tp_base */ |
| 730 | 0, /* tp_dict */ |
| 731 | 0, /* tp_descr_get */ |
| 732 | 0, /* tp_descr_set */ |
| 733 | }; |
| 734 | |
| 735 | PyObject * |
| 736 | PyDictProxy_New(PyObject *dict) |
| 737 | { |
| 738 | proxyobject *pp; |
| 739 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 740 | pp = PyObject_GC_New(proxyobject, &proxytype); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 741 | if (pp != NULL) { |
| 742 | Py_INCREF(dict); |
| 743 | pp->dict = dict; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 744 | _PyObject_GC_TRACK(pp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 745 | } |
| 746 | return (PyObject *)pp; |
| 747 | } |
| 748 | |
| 749 | |
| 750 | /* --- Wrapper object for "slot" methods --- */ |
| 751 | |
| 752 | /* This has no reason to be in this file except that adding new files is a |
| 753 | bit of a pain */ |
| 754 | |
| 755 | typedef struct { |
| 756 | PyObject_HEAD |
| 757 | PyWrapperDescrObject *descr; |
| 758 | PyObject *self; |
| 759 | } wrapperobject; |
| 760 | |
| 761 | static void |
| 762 | wrapper_dealloc(wrapperobject *wp) |
| 763 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 764 | _PyObject_GC_UNTRACK(wp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 765 | Py_XDECREF(wp->descr); |
| 766 | Py_XDECREF(wp->self); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 767 | PyObject_GC_Del(wp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static PyMethodDef wrapper_methods[] = { |
| 771 | {0} |
| 772 | }; |
| 773 | |
| 774 | static PyObject * |
| 775 | wrapper_name(wrapperobject *wp) |
| 776 | { |
| 777 | char *s = wp->descr->d_base->name; |
| 778 | |
| 779 | return PyString_FromString(s); |
| 780 | } |
| 781 | |
| 782 | static PyObject * |
| 783 | wrapper_doc(wrapperobject *wp) |
| 784 | { |
| 785 | char *s = wp->descr->d_base->doc; |
| 786 | |
| 787 | if (s == NULL) { |
| 788 | Py_INCREF(Py_None); |
| 789 | return Py_None; |
| 790 | } |
| 791 | else { |
| 792 | return PyString_FromString(s); |
| 793 | } |
| 794 | } |
| 795 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 796 | static PyGetSetDef wrapper_getsets[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 797 | {"__name__", (getter)wrapper_name}, |
| 798 | {"__doc__", (getter)wrapper_doc}, |
| 799 | {0} |
| 800 | }; |
| 801 | |
| 802 | static PyObject * |
| 803 | wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds) |
| 804 | { |
| 805 | wrapperfunc wrapper = wp->descr->d_base->wrapper; |
| 806 | PyObject *self = wp->self; |
| 807 | |
| 808 | return (*wrapper)(self, args, wp->descr->d_wrapped); |
| 809 | } |
| 810 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 811 | static int |
| 812 | wrapper_traverse(PyObject *self, visitproc visit, void *arg) |
| 813 | { |
| 814 | wrapperobject *wp = (wrapperobject *)self; |
| 815 | int err; |
| 816 | |
| 817 | if (wp->descr) { |
| 818 | err = visit((PyObject *)(wp->descr), arg); |
| 819 | if (err) |
| 820 | return err; |
| 821 | } |
| 822 | if (wp->self) { |
| 823 | err = visit(wp->self, arg); |
| 824 | if (err) |
| 825 | return err; |
| 826 | } |
| 827 | return 0; |
| 828 | } |
| 829 | |
Neil Schemenauer | 2677512 | 2001-10-21 22:26:43 +0000 | [diff] [blame^] | 830 | static PyTypeObject wrappertype = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 831 | PyObject_HEAD_INIT(&PyType_Type) |
| 832 | 0, /* ob_size */ |
| 833 | "method-wrapper", /* tp_name */ |
| 834 | sizeof(wrapperobject), /* tp_basicsize */ |
| 835 | 0, /* tp_itemsize */ |
| 836 | /* methods */ |
| 837 | (destructor)wrapper_dealloc, /* tp_dealloc */ |
| 838 | 0, /* tp_print */ |
| 839 | 0, /* tp_getattr */ |
| 840 | 0, /* tp_setattr */ |
| 841 | 0, /* tp_compare */ |
| 842 | 0, /* tp_repr */ |
| 843 | 0, /* tp_as_number */ |
| 844 | 0, /* tp_as_sequence */ |
| 845 | 0, /* tp_as_mapping */ |
| 846 | 0, /* tp_hash */ |
| 847 | (ternaryfunc)wrapper_call, /* tp_call */ |
| 848 | 0, /* tp_str */ |
| 849 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 850 | 0, /* tp_setattro */ |
| 851 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 852 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 853 | 0, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 854 | wrapper_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 855 | 0, /* tp_clear */ |
| 856 | 0, /* tp_richcompare */ |
| 857 | 0, /* tp_weaklistoffset */ |
| 858 | 0, /* tp_iter */ |
| 859 | 0, /* tp_iternext */ |
| 860 | wrapper_methods, /* tp_methods */ |
| 861 | 0, /* tp_members */ |
| 862 | wrapper_getsets, /* tp_getset */ |
| 863 | 0, /* tp_base */ |
| 864 | 0, /* tp_dict */ |
| 865 | 0, /* tp_descr_get */ |
| 866 | 0, /* tp_descr_set */ |
| 867 | }; |
| 868 | |
| 869 | PyObject * |
| 870 | PyWrapper_New(PyObject *d, PyObject *self) |
| 871 | { |
| 872 | wrapperobject *wp; |
| 873 | PyWrapperDescrObject *descr; |
| 874 | |
| 875 | assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type)); |
| 876 | descr = (PyWrapperDescrObject *)d; |
| 877 | assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type))); |
| 878 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 879 | wp = PyObject_GC_New(wrapperobject, &wrappertype); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 880 | if (wp != NULL) { |
| 881 | Py_INCREF(descr); |
| 882 | wp->descr = descr; |
| 883 | Py_INCREF(self); |
| 884 | wp->self = self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 885 | _PyObject_GC_TRACK(wp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 886 | } |
| 887 | return (PyObject *)wp; |
| 888 | } |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 889 | |
| 890 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 891 | /* A built-in 'property' type */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 892 | |
| 893 | /* |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 894 | class property(object): |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 895 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 896 | def __init__(self, fget=None, fset=None, fdel=None, doc=None): |
| 897 | self.__get = fget |
| 898 | self.__set = fset |
| 899 | self.__del = fdel |
| 900 | self.__doc__ = doc |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 901 | |
| 902 | def __get__(self, inst, type=None): |
| 903 | if self.__get is None: |
| 904 | raise AttributeError, "unreadable attribute" |
| 905 | if inst is None: |
| 906 | return self |
| 907 | return self.__get(inst) |
| 908 | |
| 909 | def __set__(self, inst, value): |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 910 | if value is None: |
| 911 | if self.__del is None: |
| 912 | raise AttributeError, "can't delete attribute" |
| 913 | return self.__del(inst) |
| 914 | else: |
| 915 | if self.__set is None: |
| 916 | raise AttributeError, "can't set attribute" |
| 917 | return self.__set(inst, value) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 918 | */ |
| 919 | |
| 920 | typedef struct { |
| 921 | PyObject_HEAD |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 922 | PyObject *prop_get; |
| 923 | PyObject *prop_set; |
| 924 | PyObject *prop_del; |
| 925 | PyObject *prop_doc; |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 926 | } propertyobject; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 927 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 928 | static PyMemberDef property_members[] = { |
| 929 | {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY}, |
| 930 | {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY}, |
| 931 | {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY}, |
| 932 | {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY}, |
| 933 | {0} |
| 934 | }; |
| 935 | |
| 936 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 937 | static void |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 938 | property_dealloc(PyObject *self) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 939 | { |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 940 | propertyobject *gs = (propertyobject *)self; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 941 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 942 | _PyObject_GC_UNTRACK(self); |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 943 | Py_XDECREF(gs->prop_get); |
| 944 | Py_XDECREF(gs->prop_set); |
| 945 | Py_XDECREF(gs->prop_del); |
| 946 | Py_XDECREF(gs->prop_doc); |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 947 | self->ob_type->tp_free(self); |
| 948 | } |
| 949 | |
| 950 | static PyObject * |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 951 | property_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 952 | { |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 953 | propertyobject *gs = (propertyobject *)self; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 954 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 955 | if (gs->prop_get == NULL) { |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 956 | PyErr_SetString(PyExc_AttributeError, "unreadable attribute"); |
| 957 | return NULL; |
| 958 | } |
| 959 | if (obj == NULL || obj == Py_None) { |
| 960 | Py_INCREF(self); |
| 961 | return self; |
| 962 | } |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 963 | return PyObject_CallFunction(gs->prop_get, "(O)", obj); |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | static int |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 967 | property_descr_set(PyObject *self, PyObject *obj, PyObject *value) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 968 | { |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 969 | propertyobject *gs = (propertyobject *)self; |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 970 | PyObject *func, *res; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 971 | |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 972 | if (value == NULL) |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 973 | func = gs->prop_del; |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 974 | else |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 975 | func = gs->prop_set; |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 976 | if (func == NULL) { |
| 977 | PyErr_SetString(PyExc_AttributeError, |
| 978 | value == NULL ? |
| 979 | "can't delete attribute" : |
| 980 | "can't set attribute"); |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 981 | return -1; |
| 982 | } |
Guido van Rossum | 845fc48 | 2001-08-24 10:17:36 +0000 | [diff] [blame] | 983 | if (value == NULL) |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 984 | res = PyObject_CallFunction(func, "(O)", obj); |
Guido van Rossum | 845fc48 | 2001-08-24 10:17:36 +0000 | [diff] [blame] | 985 | else |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 986 | res = PyObject_CallFunction(func, "(OO)", obj, value); |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 987 | if (res == NULL) |
| 988 | return -1; |
| 989 | Py_DECREF(res); |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | static int |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 994 | property_init(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 995 | { |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 996 | PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL; |
| 997 | static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0}; |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 998 | propertyobject *gs = (propertyobject *)self; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 999 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1000 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property", |
| 1001 | kwlist, &get, &set, &del, &doc)) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1002 | return -1; |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1003 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1004 | if (get == Py_None) |
| 1005 | get = NULL; |
| 1006 | if (set == Py_None) |
| 1007 | set = NULL; |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1008 | if (del == Py_None) |
| 1009 | del = NULL; |
| 1010 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1011 | Py_XINCREF(get); |
| 1012 | Py_XINCREF(set); |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1013 | Py_XINCREF(del); |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1014 | Py_XINCREF(doc); |
| 1015 | |
| 1016 | gs->prop_get = get; |
| 1017 | gs->prop_set = set; |
| 1018 | gs->prop_del = del; |
| 1019 | gs->prop_doc = doc; |
| 1020 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1024 | static char property_doc[] = |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1025 | "property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n" |
| 1026 | "\n" |
| 1027 | "fget is a function to be used for getting an attribute value, and likewise\n" |
| 1028 | "fset is a function for setting, and fdel a function for del'ing, an\n" |
| 1029 | "attribute. Typical use is to define a managed attribute x:\n" |
Guido van Rossum | 91c0d8a | 2001-08-24 09:55:51 +0000 | [diff] [blame] | 1030 | "class C(object):\n" |
| 1031 | " def getx(self): return self.__x\n" |
| 1032 | " def setx(self, value): self.__x = value\n" |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1033 | " def delx(self): del self.__x\n" |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1034 | " x = property(getx, setx, delx, \"I'm the 'x' property.\")"; |
Guido van Rossum | 91c0d8a | 2001-08-24 09:55:51 +0000 | [diff] [blame] | 1035 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1036 | static int |
| 1037 | property_traverse(PyObject *self, visitproc visit, void *arg) |
| 1038 | { |
| 1039 | propertyobject *pp = (propertyobject *)self; |
| 1040 | int err; |
| 1041 | |
| 1042 | #define VISIT(SLOT) \ |
| 1043 | if (pp->SLOT) { \ |
| 1044 | err = visit((PyObject *)(pp->SLOT), arg); \ |
| 1045 | if (err) \ |
| 1046 | return err; \ |
| 1047 | } |
| 1048 | |
| 1049 | VISIT(prop_get); |
| 1050 | VISIT(prop_set); |
| 1051 | VISIT(prop_del); |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1056 | PyTypeObject PyProperty_Type = { |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1057 | PyObject_HEAD_INIT(&PyType_Type) |
| 1058 | 0, /* ob_size */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1059 | "property", /* tp_name */ |
| 1060 | sizeof(propertyobject), /* tp_basicsize */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1061 | 0, /* tp_itemsize */ |
| 1062 | /* methods */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1063 | property_dealloc, /* tp_dealloc */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1064 | 0, /* tp_print */ |
| 1065 | 0, /* tp_getattr */ |
| 1066 | 0, /* tp_setattr */ |
| 1067 | 0, /* tp_compare */ |
| 1068 | 0, /* tp_repr */ |
| 1069 | 0, /* tp_as_number */ |
| 1070 | 0, /* tp_as_sequence */ |
| 1071 | 0, /* tp_as_mapping */ |
| 1072 | 0, /* tp_hash */ |
| 1073 | 0, /* tp_call */ |
| 1074 | 0, /* tp_str */ |
| 1075 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1076 | 0, /* tp_setattro */ |
| 1077 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1078 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1079 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1080 | property_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1081 | property_traverse, /* tp_traverse */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1082 | 0, /* tp_clear */ |
| 1083 | 0, /* tp_richcompare */ |
| 1084 | 0, /* tp_weaklistoffset */ |
| 1085 | 0, /* tp_iter */ |
| 1086 | 0, /* tp_iternext */ |
| 1087 | 0, /* tp_methods */ |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1088 | property_members, /* tp_members */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1089 | 0, /* tp_getset */ |
| 1090 | 0, /* tp_base */ |
| 1091 | 0, /* tp_dict */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1092 | property_descr_get, /* tp_descr_get */ |
| 1093 | property_descr_set, /* tp_descr_set */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1094 | 0, /* tp_dictoffset */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1095 | property_init, /* tp_init */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1096 | PyType_GenericAlloc, /* tp_alloc */ |
| 1097 | PyType_GenericNew, /* tp_new */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1098 | _PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1099 | }; |