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