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