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); |
Barry Warsaw | c8d907c | 2005-04-19 23:43:40 +0000 | [diff] [blame] | 147 | PyErr_Format(PyExc_AttributeError, |
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); |
Barry Warsaw | c8d907c | 2005-04-19 23:43:40 +0000 | [diff] [blame] | 202 | PyErr_Format(PyExc_AttributeError, |
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 | { |
Raymond Hettinger | d3ae672 | 2003-12-13 11:58:56 +0000 | [diff] [blame] | 693 | return PyDict_Contains(pp->dict, key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 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 | { |
Raymond Hettinger | d3ae672 | 2003-12-13 11:58:56 +0000 | [diff] [blame] | 712 | int res = PyDict_Contains(pp->dict, key); |
Guido van Rossum | 98c65be | 2003-10-09 03:47:08 +0000 | [diff] [blame] | 713 | if (res < 0) |
| 714 | return NULL; |
| 715 | return PyBool_FromLong(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static PyObject * |
| 719 | proxy_get(proxyobject *pp, PyObject *args) |
| 720 | { |
| 721 | PyObject *key, *def = Py_None; |
| 722 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 723 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 724 | return NULL; |
| 725 | return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def); |
| 726 | } |
| 727 | |
| 728 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 729 | proxy_keys(proxyobject *pp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 730 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 731 | return PyMapping_Keys(pp->dict); |
| 732 | } |
| 733 | |
| 734 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 735 | proxy_values(proxyobject *pp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 736 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 737 | return PyMapping_Values(pp->dict); |
| 738 | } |
| 739 | |
| 740 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 741 | proxy_items(proxyobject *pp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 742 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 743 | return PyMapping_Items(pp->dict); |
| 744 | } |
| 745 | |
| 746 | static PyObject * |
Walter Dörwald | e990c79 | 2002-03-25 17:43:22 +0000 | [diff] [blame] | 747 | proxy_iterkeys(proxyobject *pp) |
| 748 | { |
| 749 | return PyObject_CallMethod(pp->dict, "iterkeys", NULL); |
| 750 | } |
| 751 | |
| 752 | static PyObject * |
| 753 | proxy_itervalues(proxyobject *pp) |
| 754 | { |
| 755 | return PyObject_CallMethod(pp->dict, "itervalues", NULL); |
| 756 | } |
| 757 | |
| 758 | static PyObject * |
| 759 | proxy_iteritems(proxyobject *pp) |
| 760 | { |
| 761 | return PyObject_CallMethod(pp->dict, "iteritems", NULL); |
| 762 | } |
| 763 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 764 | proxy_copy(proxyobject *pp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 765 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 766 | return PyObject_CallMethod(pp->dict, "copy", NULL); |
| 767 | } |
| 768 | |
| 769 | static PyMethodDef proxy_methods[] = { |
Guido van Rossum | 5961042 | 2002-04-13 14:06:36 +0000 | [diff] [blame] | 770 | {"has_key", (PyCFunction)proxy_has_key, METH_O, |
Raymond Hettinger | 574aa32 | 2003-09-01 22:12:08 +0000 | [diff] [blame] | 771 | PyDoc_STR("D.has_key(k) -> True if D has a key k, else False")}, |
Guido van Rossum | 5961042 | 2002-04-13 14:06:36 +0000 | [diff] [blame] | 772 | {"get", (PyCFunction)proxy_get, METH_VARARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 773 | PyDoc_STR("D.get(k[,d]) -> D[k] if D.has_key(k), else d." |
| 774 | " d defaults to None.")}, |
Guido van Rossum | 5961042 | 2002-04-13 14:06:36 +0000 | [diff] [blame] | 775 | {"keys", (PyCFunction)proxy_keys, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 776 | PyDoc_STR("D.keys() -> list of D's keys")}, |
Guido van Rossum | 5961042 | 2002-04-13 14:06:36 +0000 | [diff] [blame] | 777 | {"values", (PyCFunction)proxy_values, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 778 | PyDoc_STR("D.values() -> list of D's values")}, |
Guido van Rossum | 5961042 | 2002-04-13 14:06:36 +0000 | [diff] [blame] | 779 | {"items", (PyCFunction)proxy_items, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 780 | 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] | 781 | {"iterkeys", (PyCFunction)proxy_iterkeys, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 782 | PyDoc_STR("D.iterkeys() -> an iterator over the keys of D")}, |
Guido van Rossum | 5961042 | 2002-04-13 14:06:36 +0000 | [diff] [blame] | 783 | {"itervalues",(PyCFunction)proxy_itervalues, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 784 | PyDoc_STR("D.itervalues() -> an iterator over the values of D")}, |
Guido van Rossum | 5961042 | 2002-04-13 14:06:36 +0000 | [diff] [blame] | 785 | {"iteritems", (PyCFunction)proxy_iteritems, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 786 | PyDoc_STR("D.iteritems() ->" |
| 787 | " an iterator over the (key, value) items of D")}, |
Guido van Rossum | 5961042 | 2002-04-13 14:06:36 +0000 | [diff] [blame] | 788 | {"copy", (PyCFunction)proxy_copy, METH_NOARGS, |
Neal Norwitz | 5dc2a37 | 2002-08-13 22:19:13 +0000 | [diff] [blame] | 789 | PyDoc_STR("D.copy() -> a shallow copy of D")}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 790 | {0} |
| 791 | }; |
| 792 | |
| 793 | static void |
| 794 | proxy_dealloc(proxyobject *pp) |
| 795 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 796 | _PyObject_GC_UNTRACK(pp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 797 | Py_DECREF(pp->dict); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 798 | PyObject_GC_Del(pp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | static PyObject * |
| 802 | proxy_getiter(proxyobject *pp) |
| 803 | { |
| 804 | return PyObject_GetIter(pp->dict); |
| 805 | } |
| 806 | |
Neil Schemenauer | 2677512 | 2001-10-21 22:26:43 +0000 | [diff] [blame] | 807 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 808 | proxy_str(proxyobject *pp) |
| 809 | { |
| 810 | return PyObject_Str(pp->dict); |
| 811 | } |
| 812 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 813 | static int |
| 814 | proxy_traverse(PyObject *self, visitproc visit, void *arg) |
| 815 | { |
| 816 | proxyobject *pp = (proxyobject *)self; |
| 817 | int err; |
| 818 | |
| 819 | if (pp->dict) { |
| 820 | err = visit(pp->dict, arg); |
| 821 | if (err) |
| 822 | return err; |
| 823 | } |
| 824 | return 0; |
| 825 | } |
| 826 | |
Raymond Hettinger | 29a6d44 | 2002-08-31 15:51:04 +0000 | [diff] [blame] | 827 | static int |
| 828 | proxy_compare(proxyobject *v, PyObject *w) |
| 829 | { |
| 830 | return PyObject_Compare(v->dict, w); |
| 831 | } |
| 832 | |
| 833 | static PyObject * |
| 834 | proxy_richcompare(proxyobject *v, PyObject *w, int op) |
| 835 | { |
| 836 | return PyObject_RichCompare(v->dict, w, op); |
| 837 | } |
| 838 | |
Neil Schemenauer | 2677512 | 2001-10-21 22:26:43 +0000 | [diff] [blame] | 839 | static PyTypeObject proxytype = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 840 | PyObject_HEAD_INIT(&PyType_Type) |
| 841 | 0, /* ob_size */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 842 | "dictproxy", /* tp_name */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 843 | sizeof(proxyobject), /* tp_basicsize */ |
| 844 | 0, /* tp_itemsize */ |
| 845 | /* methods */ |
| 846 | (destructor)proxy_dealloc, /* tp_dealloc */ |
| 847 | 0, /* tp_print */ |
| 848 | 0, /* tp_getattr */ |
| 849 | 0, /* tp_setattr */ |
Raymond Hettinger | 29a6d44 | 2002-08-31 15:51:04 +0000 | [diff] [blame] | 850 | (cmpfunc)proxy_compare, /* tp_compare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 851 | 0, /* tp_repr */ |
| 852 | 0, /* tp_as_number */ |
| 853 | &proxy_as_sequence, /* tp_as_sequence */ |
| 854 | &proxy_as_mapping, /* tp_as_mapping */ |
| 855 | 0, /* tp_hash */ |
| 856 | 0, /* tp_call */ |
| 857 | (reprfunc)proxy_str, /* tp_str */ |
| 858 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 859 | 0, /* tp_setattro */ |
| 860 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 861 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 862 | 0, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 863 | proxy_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 864 | 0, /* tp_clear */ |
Raymond Hettinger | 29a6d44 | 2002-08-31 15:51:04 +0000 | [diff] [blame] | 865 | (richcmpfunc)proxy_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 866 | 0, /* tp_weaklistoffset */ |
| 867 | (getiterfunc)proxy_getiter, /* tp_iter */ |
| 868 | 0, /* tp_iternext */ |
| 869 | proxy_methods, /* tp_methods */ |
| 870 | 0, /* tp_members */ |
| 871 | 0, /* tp_getset */ |
| 872 | 0, /* tp_base */ |
| 873 | 0, /* tp_dict */ |
| 874 | 0, /* tp_descr_get */ |
| 875 | 0, /* tp_descr_set */ |
| 876 | }; |
| 877 | |
| 878 | PyObject * |
| 879 | PyDictProxy_New(PyObject *dict) |
| 880 | { |
| 881 | proxyobject *pp; |
| 882 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 883 | pp = PyObject_GC_New(proxyobject, &proxytype); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 884 | if (pp != NULL) { |
| 885 | Py_INCREF(dict); |
| 886 | pp->dict = dict; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 887 | _PyObject_GC_TRACK(pp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 888 | } |
| 889 | return (PyObject *)pp; |
| 890 | } |
| 891 | |
| 892 | |
| 893 | /* --- Wrapper object for "slot" methods --- */ |
| 894 | |
| 895 | /* This has no reason to be in this file except that adding new files is a |
| 896 | bit of a pain */ |
| 897 | |
| 898 | typedef struct { |
| 899 | PyObject_HEAD |
| 900 | PyWrapperDescrObject *descr; |
| 901 | PyObject *self; |
| 902 | } wrapperobject; |
| 903 | |
| 904 | static void |
| 905 | wrapper_dealloc(wrapperobject *wp) |
| 906 | { |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 907 | _PyObject_GC_UNTRACK(wp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 908 | Py_XDECREF(wp->descr); |
| 909 | Py_XDECREF(wp->self); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 910 | PyObject_GC_Del(wp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static PyMethodDef wrapper_methods[] = { |
| 914 | {0} |
| 915 | }; |
| 916 | |
| 917 | static PyObject * |
| 918 | wrapper_name(wrapperobject *wp) |
| 919 | { |
| 920 | char *s = wp->descr->d_base->name; |
| 921 | |
| 922 | return PyString_FromString(s); |
| 923 | } |
| 924 | |
| 925 | static PyObject * |
| 926 | wrapper_doc(wrapperobject *wp) |
| 927 | { |
| 928 | char *s = wp->descr->d_base->doc; |
| 929 | |
| 930 | if (s == NULL) { |
| 931 | Py_INCREF(Py_None); |
| 932 | return Py_None; |
| 933 | } |
| 934 | else { |
| 935 | return PyString_FromString(s); |
| 936 | } |
| 937 | } |
| 938 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 939 | static PyGetSetDef wrapper_getsets[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 940 | {"__name__", (getter)wrapper_name}, |
| 941 | {"__doc__", (getter)wrapper_doc}, |
| 942 | {0} |
| 943 | }; |
| 944 | |
| 945 | static PyObject * |
| 946 | wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds) |
| 947 | { |
| 948 | wrapperfunc wrapper = wp->descr->d_base->wrapper; |
| 949 | PyObject *self = wp->self; |
| 950 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 951 | if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) { |
| 952 | wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper; |
| 953 | return (*wk)(self, args, wp->descr->d_wrapped, kwds); |
| 954 | } |
| 955 | |
| 956 | if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) { |
| 957 | PyErr_Format(PyExc_TypeError, |
| 958 | "wrapper %s doesn't take keyword arguments", |
| 959 | wp->descr->d_base->name); |
| 960 | return NULL; |
| 961 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 962 | return (*wrapper)(self, args, wp->descr->d_wrapped); |
| 963 | } |
| 964 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 965 | static int |
| 966 | wrapper_traverse(PyObject *self, visitproc visit, void *arg) |
| 967 | { |
| 968 | wrapperobject *wp = (wrapperobject *)self; |
| 969 | int err; |
| 970 | |
| 971 | if (wp->descr) { |
| 972 | err = visit((PyObject *)(wp->descr), arg); |
| 973 | if (err) |
| 974 | return err; |
| 975 | } |
| 976 | if (wp->self) { |
| 977 | err = visit(wp->self, arg); |
| 978 | if (err) |
| 979 | return err; |
| 980 | } |
| 981 | return 0; |
| 982 | } |
| 983 | |
Neil Schemenauer | 2677512 | 2001-10-21 22:26:43 +0000 | [diff] [blame] | 984 | static PyTypeObject wrappertype = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 985 | PyObject_HEAD_INIT(&PyType_Type) |
| 986 | 0, /* ob_size */ |
| 987 | "method-wrapper", /* tp_name */ |
| 988 | sizeof(wrapperobject), /* tp_basicsize */ |
| 989 | 0, /* tp_itemsize */ |
| 990 | /* methods */ |
| 991 | (destructor)wrapper_dealloc, /* tp_dealloc */ |
| 992 | 0, /* tp_print */ |
| 993 | 0, /* tp_getattr */ |
| 994 | 0, /* tp_setattr */ |
| 995 | 0, /* tp_compare */ |
| 996 | 0, /* tp_repr */ |
| 997 | 0, /* tp_as_number */ |
| 998 | 0, /* tp_as_sequence */ |
| 999 | 0, /* tp_as_mapping */ |
| 1000 | 0, /* tp_hash */ |
| 1001 | (ternaryfunc)wrapper_call, /* tp_call */ |
| 1002 | 0, /* tp_str */ |
| 1003 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1004 | 0, /* tp_setattro */ |
| 1005 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1006 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1007 | 0, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1008 | wrapper_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1009 | 0, /* tp_clear */ |
| 1010 | 0, /* tp_richcompare */ |
| 1011 | 0, /* tp_weaklistoffset */ |
| 1012 | 0, /* tp_iter */ |
| 1013 | 0, /* tp_iternext */ |
| 1014 | wrapper_methods, /* tp_methods */ |
| 1015 | 0, /* tp_members */ |
| 1016 | wrapper_getsets, /* tp_getset */ |
| 1017 | 0, /* tp_base */ |
| 1018 | 0, /* tp_dict */ |
| 1019 | 0, /* tp_descr_get */ |
| 1020 | 0, /* tp_descr_set */ |
| 1021 | }; |
| 1022 | |
| 1023 | PyObject * |
| 1024 | PyWrapper_New(PyObject *d, PyObject *self) |
| 1025 | { |
| 1026 | wrapperobject *wp; |
| 1027 | PyWrapperDescrObject *descr; |
| 1028 | |
| 1029 | assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type)); |
| 1030 | descr = (PyWrapperDescrObject *)d; |
| 1031 | assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type))); |
| 1032 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1033 | wp = PyObject_GC_New(wrapperobject, &wrappertype); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1034 | if (wp != NULL) { |
| 1035 | Py_INCREF(descr); |
| 1036 | wp->descr = descr; |
| 1037 | Py_INCREF(self); |
| 1038 | wp->self = self; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1039 | _PyObject_GC_TRACK(wp); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1040 | } |
| 1041 | return (PyObject *)wp; |
| 1042 | } |
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 | /* A built-in 'property' type */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1046 | |
| 1047 | /* |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1048 | class property(object): |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1049 | |
Guido van Rossum | ba2485f | 2001-12-10 18:03:34 +0000 | [diff] [blame] | 1050 | def __init__(self, fget=None, fset=None, fdel=None, doc=None): |
| 1051 | self.__get = fget |
| 1052 | self.__set = fset |
| 1053 | self.__del = fdel |
| 1054 | self.__doc__ = doc |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1055 | |
Guido van Rossum | ba2485f | 2001-12-10 18:03:34 +0000 | [diff] [blame] | 1056 | def __get__(self, inst, type=None): |
Guido van Rossum | ba2485f | 2001-12-10 18:03:34 +0000 | [diff] [blame] | 1057 | if inst is None: |
| 1058 | return self |
Guido van Rossum | 7171f1c | 2001-12-10 18:06:21 +0000 | [diff] [blame] | 1059 | if self.__get is None: |
| 1060 | raise AttributeError, "unreadable attribute" |
Guido van Rossum | ba2485f | 2001-12-10 18:03:34 +0000 | [diff] [blame] | 1061 | return self.__get(inst) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1062 | |
Guido van Rossum | ba2485f | 2001-12-10 18:03:34 +0000 | [diff] [blame] | 1063 | def __set__(self, inst, value): |
| 1064 | if self.__set is None: |
| 1065 | raise AttributeError, "can't set attribute" |
| 1066 | return self.__set(inst, value) |
| 1067 | |
| 1068 | def __delete__(self, inst): |
| 1069 | if self.__del is None: |
| 1070 | raise AttributeError, "can't delete attribute" |
| 1071 | return self.__del(inst) |
| 1072 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1073 | */ |
| 1074 | |
| 1075 | typedef struct { |
| 1076 | PyObject_HEAD |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1077 | PyObject *prop_get; |
| 1078 | PyObject *prop_set; |
| 1079 | PyObject *prop_del; |
| 1080 | PyObject *prop_doc; |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1081 | } propertyobject; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1082 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1083 | static PyMemberDef property_members[] = { |
| 1084 | {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY}, |
| 1085 | {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY}, |
| 1086 | {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY}, |
| 1087 | {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY}, |
| 1088 | {0} |
| 1089 | }; |
| 1090 | |
| 1091 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1092 | static void |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1093 | property_dealloc(PyObject *self) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1094 | { |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1095 | propertyobject *gs = (propertyobject *)self; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1096 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1097 | _PyObject_GC_UNTRACK(self); |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1098 | Py_XDECREF(gs->prop_get); |
| 1099 | Py_XDECREF(gs->prop_set); |
| 1100 | Py_XDECREF(gs->prop_del); |
| 1101 | Py_XDECREF(gs->prop_doc); |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1102 | self->ob_type->tp_free(self); |
| 1103 | } |
| 1104 | |
| 1105 | static PyObject * |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1106 | property_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1107 | { |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1108 | propertyobject *gs = (propertyobject *)self; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1109 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1110 | if (obj == NULL || obj == Py_None) { |
| 1111 | Py_INCREF(self); |
| 1112 | return self; |
| 1113 | } |
Guido van Rossum | b75ba91 | 2001-12-10 18:00:15 +0000 | [diff] [blame] | 1114 | if (gs->prop_get == NULL) { |
| 1115 | PyErr_SetString(PyExc_AttributeError, "unreadable attribute"); |
| 1116 | return NULL; |
| 1117 | } |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1118 | return PyObject_CallFunction(gs->prop_get, "(O)", obj); |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | static int |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1122 | property_descr_set(PyObject *self, PyObject *obj, PyObject *value) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1123 | { |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1124 | propertyobject *gs = (propertyobject *)self; |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1125 | PyObject *func, *res; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1126 | |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1127 | if (value == NULL) |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1128 | func = gs->prop_del; |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1129 | else |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1130 | func = gs->prop_set; |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1131 | if (func == NULL) { |
| 1132 | PyErr_SetString(PyExc_AttributeError, |
| 1133 | value == NULL ? |
| 1134 | "can't delete attribute" : |
| 1135 | "can't set attribute"); |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1136 | return -1; |
| 1137 | } |
Guido van Rossum | 845fc48 | 2001-08-24 10:17:36 +0000 | [diff] [blame] | 1138 | if (value == NULL) |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1139 | res = PyObject_CallFunction(func, "(O)", obj); |
Guido van Rossum | 845fc48 | 2001-08-24 10:17:36 +0000 | [diff] [blame] | 1140 | else |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1141 | res = PyObject_CallFunction(func, "(OO)", obj, value); |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1142 | if (res == NULL) |
| 1143 | return -1; |
| 1144 | Py_DECREF(res); |
| 1145 | return 0; |
| 1146 | } |
| 1147 | |
| 1148 | static int |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1149 | property_init(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1150 | { |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1151 | PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL; |
| 1152 | static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0}; |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1153 | propertyobject *gs = (propertyobject *)self; |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1154 | |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1155 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property", |
| 1156 | kwlist, &get, &set, &del, &doc)) |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1157 | return -1; |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1158 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1159 | if (get == Py_None) |
| 1160 | get = NULL; |
| 1161 | if (set == Py_None) |
| 1162 | set = NULL; |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1163 | if (del == Py_None) |
| 1164 | del = NULL; |
| 1165 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1166 | Py_XINCREF(get); |
| 1167 | Py_XINCREF(set); |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1168 | Py_XINCREF(del); |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1169 | Py_XINCREF(doc); |
| 1170 | |
| 1171 | gs->prop_get = get; |
| 1172 | gs->prop_set = set; |
| 1173 | gs->prop_del = del; |
| 1174 | gs->prop_doc = doc; |
| 1175 | |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1179 | PyDoc_STRVAR(property_doc, |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1180 | "property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n" |
| 1181 | "\n" |
| 1182 | "fget is a function to be used for getting an attribute value, and likewise\n" |
| 1183 | "fset is a function for setting, and fdel a function for del'ing, an\n" |
| 1184 | "attribute. Typical use is to define a managed attribute x:\n" |
Guido van Rossum | 91c0d8a | 2001-08-24 09:55:51 +0000 | [diff] [blame] | 1185 | "class C(object):\n" |
| 1186 | " def getx(self): return self.__x\n" |
| 1187 | " def setx(self, value): self.__x = value\n" |
Guido van Rossum | 271410a | 2001-08-24 15:23:20 +0000 | [diff] [blame] | 1188 | " def delx(self): del self.__x\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1189 | " x = property(getx, setx, delx, \"I'm the 'x' property.\")"); |
Guido van Rossum | 91c0d8a | 2001-08-24 09:55:51 +0000 | [diff] [blame] | 1190 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1191 | static int |
| 1192 | property_traverse(PyObject *self, visitproc visit, void *arg) |
| 1193 | { |
| 1194 | propertyobject *pp = (propertyobject *)self; |
| 1195 | int err; |
| 1196 | |
| 1197 | #define VISIT(SLOT) \ |
| 1198 | if (pp->SLOT) { \ |
| 1199 | err = visit((PyObject *)(pp->SLOT), arg); \ |
| 1200 | if (err) \ |
| 1201 | return err; \ |
| 1202 | } |
| 1203 | |
| 1204 | VISIT(prop_get); |
| 1205 | VISIT(prop_set); |
| 1206 | VISIT(prop_del); |
Guido van Rossum | 00bf828 | 2003-04-09 17:05:33 +0000 | [diff] [blame] | 1207 | VISIT(prop_doc); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1208 | |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1212 | PyTypeObject PyProperty_Type = { |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1213 | PyObject_HEAD_INIT(&PyType_Type) |
| 1214 | 0, /* ob_size */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1215 | "property", /* tp_name */ |
| 1216 | sizeof(propertyobject), /* tp_basicsize */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1217 | 0, /* tp_itemsize */ |
| 1218 | /* methods */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1219 | property_dealloc, /* tp_dealloc */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1220 | 0, /* tp_print */ |
| 1221 | 0, /* tp_getattr */ |
| 1222 | 0, /* tp_setattr */ |
| 1223 | 0, /* tp_compare */ |
| 1224 | 0, /* tp_repr */ |
| 1225 | 0, /* tp_as_number */ |
| 1226 | 0, /* tp_as_sequence */ |
| 1227 | 0, /* tp_as_mapping */ |
| 1228 | 0, /* tp_hash */ |
| 1229 | 0, /* tp_call */ |
| 1230 | 0, /* tp_str */ |
| 1231 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1232 | 0, /* tp_setattro */ |
| 1233 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1234 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1235 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1236 | property_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1237 | property_traverse, /* tp_traverse */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1238 | 0, /* tp_clear */ |
| 1239 | 0, /* tp_richcompare */ |
| 1240 | 0, /* tp_weaklistoffset */ |
| 1241 | 0, /* tp_iter */ |
| 1242 | 0, /* tp_iternext */ |
| 1243 | 0, /* tp_methods */ |
Tim Peters | 66c1a52 | 2001-09-24 21:17:50 +0000 | [diff] [blame] | 1244 | property_members, /* tp_members */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1245 | 0, /* tp_getset */ |
| 1246 | 0, /* tp_base */ |
| 1247 | 0, /* tp_dict */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1248 | property_descr_get, /* tp_descr_get */ |
| 1249 | property_descr_set, /* tp_descr_set */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1250 | 0, /* tp_dictoffset */ |
Guido van Rossum | 8bce4ac | 2001-09-06 21:56:42 +0000 | [diff] [blame] | 1251 | property_init, /* tp_init */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1252 | PyType_GenericAlloc, /* tp_alloc */ |
| 1253 | PyType_GenericNew, /* tp_new */ |
Neil Schemenauer | 99b5d28 | 2002-04-12 02:44:22 +0000 | [diff] [blame] | 1254 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 29a62dd | 2001-08-23 21:40:38 +0000 | [diff] [blame] | 1255 | }; |