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