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