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