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