blob: 71d522433a2d10869e74d2f4ecd3a3ae1bc764be [file] [log] [blame]
Tim Peters6d6c1a32001-08-02 04:15:00 +00001/* Descriptors -- a new, flexible way to describe attributes */
2
3#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06004#include "internal/pystate.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00005#include "structmember.h" /* Why is this not included in Python.h? */
6
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02007/*[clinic input]
8class mappingproxy "mappingproxyobject *" "&PyDictProxy_Type"
9class property "propertyobject *" "&PyProperty_Type"
10[clinic start generated code]*/
11/*[clinic end generated code: output=da39a3ee5e6b4b0d input=556352653fd4c02e]*/
12
Tim Peters6d6c1a32001-08-02 04:15:00 +000013static void
14descr_dealloc(PyDescrObject *descr)
15{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 _PyObject_GC_UNTRACK(descr);
17 Py_XDECREF(descr->d_type);
18 Py_XDECREF(descr->d_name);
Antoine Pitrou9d574812011-12-12 13:47:25 +010019 Py_XDECREF(descr->d_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 PyObject_GC_Del(descr);
Tim Peters6d6c1a32001-08-02 04:15:00 +000021}
22
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000023static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000024descr_name(PyDescrObject *descr)
25{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
27 return descr->d_name;
28 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000029}
30
31static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020032descr_repr(PyDescrObject *descr, const char *format)
Tim Peters6d6c1a32001-08-02 04:15:00 +000033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 PyObject *name = NULL;
35 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
36 name = descr->d_name;
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 return PyUnicode_FromFormat(format, name, "?", descr->d_type->tp_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +000039}
40
41static PyObject *
42method_repr(PyMethodDescrObject *descr)
43{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 return descr_repr((PyDescrObject *)descr,
45 "<method '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000046}
47
48static PyObject *
49member_repr(PyMemberDescrObject *descr)
50{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 return descr_repr((PyDescrObject *)descr,
52 "<member '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000053}
54
55static PyObject *
56getset_repr(PyGetSetDescrObject *descr)
57{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 return descr_repr((PyDescrObject *)descr,
59 "<attribute '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000060}
61
62static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +000063wrapperdescr_repr(PyWrapperDescrObject *descr)
Tim Peters6d6c1a32001-08-02 04:15:00 +000064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return descr_repr((PyDescrObject *)descr,
66 "<slot wrapper '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000067}
68
69static int
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000070descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +000071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (obj == NULL) {
73 Py_INCREF(descr);
74 *pres = (PyObject *)descr;
75 return 1;
76 }
77 if (!PyObject_TypeCheck(obj, descr->d_type)) {
78 PyErr_Format(PyExc_TypeError,
79 "descriptor '%V' for '%s' objects "
80 "doesn't apply to '%s' object",
81 descr_name((PyDescrObject *)descr), "?",
82 descr->d_type->tp_name,
83 obj->ob_type->tp_name);
84 *pres = NULL;
85 return 1;
86 }
87 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +000088}
89
90static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000091classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Petersbca1cbc2002-12-09 22:56:13 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 /* Ensure a valid type. Class methods ignore obj. */
94 if (type == NULL) {
95 if (obj != NULL)
96 type = (PyObject *)obj->ob_type;
97 else {
98 /* Wot - no type?! */
99 PyErr_Format(PyExc_TypeError,
100 "descriptor '%V' for type '%s' "
101 "needs either an object or a type",
102 descr_name((PyDescrObject *)descr), "?",
103 PyDescr_TYPE(descr)->tp_name);
104 return NULL;
105 }
106 }
107 if (!PyType_Check(type)) {
108 PyErr_Format(PyExc_TypeError,
109 "descriptor '%V' for type '%s' "
110 "needs a type, not a '%s' as arg 2",
111 descr_name((PyDescrObject *)descr), "?",
112 PyDescr_TYPE(descr)->tp_name,
113 type->ob_type->tp_name);
114 return NULL;
115 }
116 if (!PyType_IsSubtype((PyTypeObject *)type, PyDescr_TYPE(descr))) {
117 PyErr_Format(PyExc_TypeError,
118 "descriptor '%V' for type '%s' "
119 "doesn't apply to type '%s'",
120 descr_name((PyDescrObject *)descr), "?",
121 PyDescr_TYPE(descr)->tp_name,
122 ((PyTypeObject *)type)->tp_name);
123 return NULL;
124 }
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200125 return PyCFunction_NewEx(descr->d_method, type, NULL);
Tim Petersbca1cbc2002-12-09 22:56:13 +0000126}
127
128static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000129method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 if (descr_check((PyDescrObject *)descr, obj, &res))
134 return res;
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200135 return PyCFunction_NewEx(descr->d_method, obj, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000136}
137
138static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000139member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 if (descr_check((PyDescrObject *)descr, obj, &res))
144 return res;
145 return PyMember_GetOne((char *)obj, descr->d_member);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000146}
147
148static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000149getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (descr_check((PyDescrObject *)descr, obj, &res))
154 return res;
155 if (descr->d_getset->get != NULL)
156 return descr->d_getset->get(obj, descr->d_getset->closure);
157 PyErr_Format(PyExc_AttributeError,
158 "attribute '%V' of '%.100s' objects is not readable",
159 descr_name((PyDescrObject *)descr), "?",
160 PyDescr_TYPE(descr)->tp_name);
161 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000162}
163
164static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000165wrapperdescr_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 if (descr_check((PyDescrObject *)descr, obj, &res))
170 return res;
171 return PyWrapper_New((PyObject *)descr, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000172}
173
174static int
175descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 int *pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 assert(obj != NULL);
179 if (!PyObject_TypeCheck(obj, descr->d_type)) {
180 PyErr_Format(PyExc_TypeError,
181 "descriptor '%V' for '%.100s' objects "
182 "doesn't apply to '%.100s' object",
183 descr_name(descr), "?",
184 descr->d_type->tp_name,
185 obj->ob_type->tp_name);
186 *pres = -1;
187 return 1;
188 }
189 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000190}
191
192static int
193member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
198 return res;
199 return PyMember_SetOne((char *)obj, descr->d_member, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200}
201
202static int
203getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
208 return res;
209 if (descr->d_getset->set != NULL)
210 return descr->d_getset->set(obj, value,
211 descr->d_getset->closure);
212 PyErr_Format(PyExc_AttributeError,
213 "attribute '%V' of '%.100s' objects is not writable",
214 descr_name((PyDescrObject *)descr), "?",
215 PyDescr_TYPE(descr)->tp_name);
216 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000217}
218
219static PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100220methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000221{
Victor Stinnerc5257232017-01-18 10:38:09 +0100222 Py_ssize_t nargs;
223 PyObject *self, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 /* Make sure that the first argument is acceptable as 'self' */
226 assert(PyTuple_Check(args));
Victor Stinnerc5257232017-01-18 10:38:09 +0100227 nargs = PyTuple_GET_SIZE(args);
228 if (nargs < 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 PyErr_Format(PyExc_TypeError,
230 "descriptor '%V' of '%.100s' "
231 "object needs an argument",
232 descr_name((PyDescrObject *)descr), "?",
233 PyDescr_TYPE(descr)->tp_name);
234 return NULL;
235 }
236 self = PyTuple_GET_ITEM(args, 0);
Victor Stinner3249dec2011-05-01 23:19:15 +0200237 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +0200238 (PyObject *)PyDescr_TYPE(descr))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyErr_Format(PyExc_TypeError,
240 "descriptor '%V' "
241 "requires a '%.100s' object "
242 "but received a '%.100s'",
243 descr_name((PyDescrObject *)descr), "?",
244 PyDescr_TYPE(descr)->tp_name,
245 self->ob_type->tp_name);
246 return NULL;
247 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000248
Victor Stinnerc5257232017-01-18 10:38:09 +0100249 result = _PyMethodDef_RawFastCallDict(descr->d_method, self,
250 &PyTuple_GET_ITEM(args, 1), nargs - 1,
251 kwargs);
252 result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000254}
255
INADA Naoki5566bbb2017-02-03 07:43:03 +0900256// same to methoddescr_call(), but use FASTCALL convention.
257PyObject *
258_PyMethodDescr_FastCallKeywords(PyObject *descrobj,
259 PyObject **args, Py_ssize_t nargs,
260 PyObject *kwnames)
261{
262 assert(Py_TYPE(descrobj) == &PyMethodDescr_Type);
263 PyMethodDescrObject *descr = (PyMethodDescrObject *)descrobj;
264 PyObject *self, *result;
265
266 /* Make sure that the first argument is acceptable as 'self' */
267 if (nargs < 1) {
268 PyErr_Format(PyExc_TypeError,
269 "descriptor '%V' of '%.100s' "
270 "object needs an argument",
271 descr_name((PyDescrObject *)descr), "?",
272 PyDescr_TYPE(descr)->tp_name);
273 return NULL;
274 }
275 self = args[0];
276 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
277 (PyObject *)PyDescr_TYPE(descr))) {
278 PyErr_Format(PyExc_TypeError,
279 "descriptor '%V' "
280 "requires a '%.100s' object "
281 "but received a '%.100s'",
282 descr_name((PyDescrObject *)descr), "?",
283 PyDescr_TYPE(descr)->tp_name,
284 self->ob_type->tp_name);
285 return NULL;
286 }
287
288 result = _PyMethodDef_RawFastCallKeywords(descr->d_method, self,
289 args+1, nargs-1, kwnames);
290 result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
291 return result;
292}
293
Tim Peters6d6c1a32001-08-02 04:15:00 +0000294static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000295classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyObject *kwds)
Tim Petersbca1cbc2002-12-09 22:56:13 +0000297{
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400298 Py_ssize_t argc;
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300299 PyObject *self, *result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000300
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400301 /* Make sure that the first argument is acceptable as 'self' */
302 assert(PyTuple_Check(args));
303 argc = PyTuple_GET_SIZE(args);
304 if (argc < 1) {
305 PyErr_Format(PyExc_TypeError,
306 "descriptor '%V' of '%.100s' "
307 "object needs an argument",
308 descr_name((PyDescrObject *)descr), "?",
309 PyDescr_TYPE(descr)->tp_name);
310 return NULL;
311 }
312 self = PyTuple_GET_ITEM(args, 0);
313 if (!PyType_Check(self)) {
314 PyErr_Format(PyExc_TypeError,
315 "descriptor '%V' requires a type "
316 "but received a '%.100s'",
317 descr_name((PyDescrObject *)descr), "?",
318 PyDescr_TYPE(descr)->tp_name,
319 self->ob_type->tp_name);
320 return NULL;
321 }
322 if (!PyType_IsSubtype((PyTypeObject *)self, PyDescr_TYPE(descr))) {
323 PyErr_Format(PyExc_TypeError,
324 "descriptor '%V' "
325 "requires a subtype of '%.100s' "
326 "but received '%.100s",
327 descr_name((PyDescrObject *)descr), "?",
328 PyDescr_TYPE(descr)->tp_name,
329 self->ob_type->tp_name);
330 return NULL;
331 }
332
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300333 result = _PyMethodDef_RawFastCallDict(descr->d_method, self,
334 &PyTuple_GET_ITEM(args, 1), argc - 1,
335 kwds);
336 result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 return result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000338}
339
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300340Py_LOCAL_INLINE(PyObject *)
341wrapperdescr_raw_call(PyWrapperDescrObject *descr, PyObject *self,
342 PyObject *args, PyObject *kwds)
343{
344 wrapperfunc wrapper = descr->d_base->wrapper;
345
346 if (descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
347 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
348 return (*wk)(self, args, descr->d_wrapped, kwds);
349 }
350
351 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_GET_SIZE(kwds) != 0)) {
352 PyErr_Format(PyExc_TypeError,
353 "wrapper %s() takes no keyword arguments",
354 descr->d_base->name);
355 return NULL;
356 }
357 return (*wrapper)(self, args, descr->d_wrapped);
358}
359
Tim Petersbca1cbc2002-12-09 22:56:13 +0000360static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000361wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 Py_ssize_t argc;
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300364 PyObject *self, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 /* Make sure that the first argument is acceptable as 'self' */
367 assert(PyTuple_Check(args));
368 argc = PyTuple_GET_SIZE(args);
369 if (argc < 1) {
370 PyErr_Format(PyExc_TypeError,
371 "descriptor '%V' of '%.100s' "
372 "object needs an argument",
373 descr_name((PyDescrObject *)descr), "?",
374 PyDescr_TYPE(descr)->tp_name);
375 return NULL;
376 }
377 self = PyTuple_GET_ITEM(args, 0);
Victor Stinner3249dec2011-05-01 23:19:15 +0200378 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +0200379 (PyObject *)PyDescr_TYPE(descr))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 PyErr_Format(PyExc_TypeError,
381 "descriptor '%V' "
382 "requires a '%.100s' object "
383 "but received a '%.100s'",
384 descr_name((PyDescrObject *)descr), "?",
385 PyDescr_TYPE(descr)->tp_name,
386 self->ob_type->tp_name);
387 return NULL;
388 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000389
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300390 args = PyTuple_GetSlice(args, 1, argc);
391 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 return NULL;
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300393 }
394 result = wrapperdescr_raw_call(descr, self, args, kwds);
395 Py_DECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000397}
398
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300399
Tim Peters6d6c1a32001-08-02 04:15:00 +0000400static PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000401method_get_doc(PyMethodDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000402{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800403 return _PyType_GetDocFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
Larry Hastings5c661892014-01-24 06:17:25 -0800404}
405
406static PyObject *
407method_get_text_signature(PyMethodDescrObject *descr, void *closure)
408{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800409 return _PyType_GetTextSignatureFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000410}
411
Antoine Pitrou9d574812011-12-12 13:47:25 +0100412static PyObject *
413calculate_qualname(PyDescrObject *descr)
414{
415 PyObject *type_qualname, *res;
416 _Py_IDENTIFIER(__qualname__);
417
418 if (descr->d_name == NULL || !PyUnicode_Check(descr->d_name)) {
419 PyErr_SetString(PyExc_TypeError,
420 "<descriptor>.__name__ is not a unicode object");
421 return NULL;
422 }
423
424 type_qualname = _PyObject_GetAttrId((PyObject *)descr->d_type,
425 &PyId___qualname__);
426 if (type_qualname == NULL)
427 return NULL;
428
429 if (!PyUnicode_Check(type_qualname)) {
430 PyErr_SetString(PyExc_TypeError, "<descriptor>.__objclass__."
431 "__qualname__ is not a unicode object");
432 Py_XDECREF(type_qualname);
433 return NULL;
434 }
435
436 res = PyUnicode_FromFormat("%S.%S", type_qualname, descr->d_name);
437 Py_DECREF(type_qualname);
438 return res;
439}
440
441static PyObject *
442descr_get_qualname(PyDescrObject *descr)
443{
444 if (descr->d_qualname == NULL)
445 descr->d_qualname = calculate_qualname(descr);
446 Py_XINCREF(descr->d_qualname);
447 return descr->d_qualname;
448}
449
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100450static PyObject *
451descr_reduce(PyDescrObject *descr)
452{
453 PyObject *builtins;
454 PyObject *getattr;
455 _Py_IDENTIFIER(getattr);
456
457 builtins = PyEval_GetBuiltins();
458 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
459 return Py_BuildValue("O(OO)", getattr, PyDescr_TYPE(descr),
460 PyDescr_NAME(descr));
461}
462
463static PyMethodDef descr_methods[] = {
464 {"__reduce__", (PyCFunction)descr_reduce, METH_NOARGS, NULL},
465 {NULL, NULL}
466};
467
Guido van Rossum6f799372001-09-20 20:46:19 +0000468static PyMemberDef descr_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
470 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
471 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000472};
473
Guido van Rossum32d34c82001-09-20 21:45:26 +0000474static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 {"__doc__", (getter)method_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100476 {"__qualname__", (getter)descr_get_qualname},
Larry Hastings5c661892014-01-24 06:17:25 -0800477 {"__text_signature__", (getter)method_get_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 {0}
Guido van Rossum6f799372001-09-20 20:46:19 +0000479};
480
481static PyObject *
482member_get_doc(PyMemberDescrObject *descr, void *closure)
483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 if (descr->d_member->doc == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200485 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 }
487 return PyUnicode_FromString(descr->d_member->doc);
Guido van Rossum6f799372001-09-20 20:46:19 +0000488}
489
Guido van Rossum32d34c82001-09-20 21:45:26 +0000490static PyGetSetDef member_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 {"__doc__", (getter)member_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100492 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000494};
495
496static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000497getset_get_doc(PyGetSetDescrObject *descr, void *closure)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (descr->d_getset->doc == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200500 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 }
502 return PyUnicode_FromString(descr->d_getset->doc);
Guido van Rossum32d34c82001-09-20 21:45:26 +0000503}
504
505static PyGetSetDef getset_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 {"__doc__", (getter)getset_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100507 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 {0}
Guido van Rossum32d34c82001-09-20 21:45:26 +0000509};
510
511static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000512wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000513{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800514 return _PyType_GetDocFromInternalDoc(descr->d_base->name, descr->d_base->doc);
Larry Hastings5c661892014-01-24 06:17:25 -0800515}
516
517static PyObject *
518wrapperdescr_get_text_signature(PyWrapperDescrObject *descr, void *closure)
519{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800520 return _PyType_GetTextSignatureFromInternalDoc(descr->d_base->name, descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000521}
522
Armin Rigoc6686b72005-11-07 08:38:00 +0000523static PyGetSetDef wrapperdescr_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 {"__doc__", (getter)wrapperdescr_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100525 {"__qualname__", (getter)descr_get_qualname},
Larry Hastings5c661892014-01-24 06:17:25 -0800526 {"__text_signature__", (getter)wrapperdescr_get_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000528};
529
Guido van Rossum048eb752001-10-02 21:24:57 +0000530static int
531descr_traverse(PyObject *self, visitproc visit, void *arg)
532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 PyDescrObject *descr = (PyDescrObject *)self;
534 Py_VISIT(descr->d_type);
535 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000536}
537
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000538PyTypeObject PyMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 PyVarObject_HEAD_INIT(&PyType_Type, 0)
540 "method_descriptor",
541 sizeof(PyMethodDescrObject),
542 0,
543 (destructor)descr_dealloc, /* tp_dealloc */
544 0, /* tp_print */
545 0, /* tp_getattr */
546 0, /* tp_setattr */
547 0, /* tp_reserved */
548 (reprfunc)method_repr, /* tp_repr */
549 0, /* tp_as_number */
550 0, /* tp_as_sequence */
551 0, /* tp_as_mapping */
552 0, /* tp_hash */
553 (ternaryfunc)methoddescr_call, /* tp_call */
554 0, /* tp_str */
555 PyObject_GenericGetAttr, /* tp_getattro */
556 0, /* tp_setattro */
557 0, /* tp_as_buffer */
558 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
559 0, /* tp_doc */
560 descr_traverse, /* tp_traverse */
561 0, /* tp_clear */
562 0, /* tp_richcompare */
563 0, /* tp_weaklistoffset */
564 0, /* tp_iter */
565 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100566 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 descr_members, /* tp_members */
568 method_getset, /* tp_getset */
569 0, /* tp_base */
570 0, /* tp_dict */
571 (descrgetfunc)method_get, /* tp_descr_get */
572 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000573};
574
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000575/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000576PyTypeObject PyClassMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 PyVarObject_HEAD_INIT(&PyType_Type, 0)
578 "classmethod_descriptor",
579 sizeof(PyMethodDescrObject),
580 0,
581 (destructor)descr_dealloc, /* tp_dealloc */
582 0, /* tp_print */
583 0, /* tp_getattr */
584 0, /* tp_setattr */
585 0, /* tp_reserved */
586 (reprfunc)method_repr, /* tp_repr */
587 0, /* tp_as_number */
588 0, /* tp_as_sequence */
589 0, /* tp_as_mapping */
590 0, /* tp_hash */
591 (ternaryfunc)classmethoddescr_call, /* tp_call */
592 0, /* tp_str */
593 PyObject_GenericGetAttr, /* tp_getattro */
594 0, /* tp_setattro */
595 0, /* tp_as_buffer */
596 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
597 0, /* tp_doc */
598 descr_traverse, /* tp_traverse */
599 0, /* tp_clear */
600 0, /* tp_richcompare */
601 0, /* tp_weaklistoffset */
602 0, /* tp_iter */
603 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100604 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 descr_members, /* tp_members */
606 method_getset, /* tp_getset */
607 0, /* tp_base */
608 0, /* tp_dict */
609 (descrgetfunc)classmethod_get, /* tp_descr_get */
610 0, /* tp_descr_set */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000611};
612
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000613PyTypeObject PyMemberDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 PyVarObject_HEAD_INIT(&PyType_Type, 0)
615 "member_descriptor",
616 sizeof(PyMemberDescrObject),
617 0,
618 (destructor)descr_dealloc, /* tp_dealloc */
619 0, /* tp_print */
620 0, /* tp_getattr */
621 0, /* tp_setattr */
622 0, /* tp_reserved */
623 (reprfunc)member_repr, /* tp_repr */
624 0, /* tp_as_number */
625 0, /* tp_as_sequence */
626 0, /* tp_as_mapping */
627 0, /* tp_hash */
628 0, /* tp_call */
629 0, /* tp_str */
630 PyObject_GenericGetAttr, /* tp_getattro */
631 0, /* tp_setattro */
632 0, /* tp_as_buffer */
633 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
634 0, /* tp_doc */
635 descr_traverse, /* tp_traverse */
636 0, /* tp_clear */
637 0, /* tp_richcompare */
638 0, /* tp_weaklistoffset */
639 0, /* tp_iter */
640 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100641 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 descr_members, /* tp_members */
643 member_getset, /* tp_getset */
644 0, /* tp_base */
645 0, /* tp_dict */
646 (descrgetfunc)member_get, /* tp_descr_get */
647 (descrsetfunc)member_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000648};
649
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000650PyTypeObject PyGetSetDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 PyVarObject_HEAD_INIT(&PyType_Type, 0)
652 "getset_descriptor",
653 sizeof(PyGetSetDescrObject),
654 0,
655 (destructor)descr_dealloc, /* tp_dealloc */
656 0, /* tp_print */
657 0, /* tp_getattr */
658 0, /* tp_setattr */
659 0, /* tp_reserved */
660 (reprfunc)getset_repr, /* tp_repr */
661 0, /* tp_as_number */
662 0, /* tp_as_sequence */
663 0, /* tp_as_mapping */
664 0, /* tp_hash */
665 0, /* tp_call */
666 0, /* tp_str */
667 PyObject_GenericGetAttr, /* tp_getattro */
668 0, /* tp_setattro */
669 0, /* tp_as_buffer */
670 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
671 0, /* tp_doc */
672 descr_traverse, /* tp_traverse */
673 0, /* tp_clear */
674 0, /* tp_richcompare */
675 0, /* tp_weaklistoffset */
676 0, /* tp_iter */
677 0, /* tp_iternext */
678 0, /* tp_methods */
679 descr_members, /* tp_members */
680 getset_getset, /* tp_getset */
681 0, /* tp_base */
682 0, /* tp_dict */
683 (descrgetfunc)getset_get, /* tp_descr_get */
684 (descrsetfunc)getset_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685};
686
Guido van Rossumf4593e02001-10-03 12:09:30 +0000687PyTypeObject PyWrapperDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyVarObject_HEAD_INIT(&PyType_Type, 0)
689 "wrapper_descriptor",
690 sizeof(PyWrapperDescrObject),
691 0,
692 (destructor)descr_dealloc, /* tp_dealloc */
693 0, /* tp_print */
694 0, /* tp_getattr */
695 0, /* tp_setattr */
696 0, /* tp_reserved */
697 (reprfunc)wrapperdescr_repr, /* tp_repr */
698 0, /* tp_as_number */
699 0, /* tp_as_sequence */
700 0, /* tp_as_mapping */
701 0, /* tp_hash */
702 (ternaryfunc)wrapperdescr_call, /* tp_call */
703 0, /* tp_str */
704 PyObject_GenericGetAttr, /* tp_getattro */
705 0, /* tp_setattro */
706 0, /* tp_as_buffer */
707 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
708 0, /* tp_doc */
709 descr_traverse, /* tp_traverse */
710 0, /* tp_clear */
711 0, /* tp_richcompare */
712 0, /* tp_weaklistoffset */
713 0, /* tp_iter */
714 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100715 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 descr_members, /* tp_members */
717 wrapperdescr_getset, /* tp_getset */
718 0, /* tp_base */
719 0, /* tp_dict */
720 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
721 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722};
723
724static PyDescrObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000725descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
730 if (descr != NULL) {
731 Py_XINCREF(type);
732 descr->d_type = type;
733 descr->d_name = PyUnicode_InternFromString(name);
734 if (descr->d_name == NULL) {
735 Py_DECREF(descr);
736 descr = NULL;
737 }
Benjamin Petersonf2fe7f02011-12-17 08:02:20 -0500738 else {
739 descr->d_qualname = NULL;
740 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 }
742 return descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000743}
744
745PyObject *
746PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 PyMethodDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
751 type, method->ml_name);
752 if (descr != NULL)
753 descr->d_method = method;
754 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755}
756
757PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000758PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 PyMethodDescrObject *descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
763 type, method->ml_name);
764 if (descr != NULL)
765 descr->d_method = method;
766 return (PyObject *)descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000767}
768
769PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000770PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 PyMemberDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
775 type, member->name);
776 if (descr != NULL)
777 descr->d_member = member;
778 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000779}
780
781PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000782PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 PyGetSetDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
787 type, getset->name);
788 if (descr != NULL)
789 descr->d_getset = getset;
790 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000791}
792
793PyObject *
794PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
799 type, base->name);
800 if (descr != NULL) {
801 descr->d_base = base;
802 descr->d_wrapped = wrapped;
803 }
804 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805}
806
Tim Peters6d6c1a32001-08-02 04:15:00 +0000807
Victor Stinner0db176f2012-04-16 00:16:30 +0200808/* --- mappingproxy: read-only proxy for mappings --- */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000809
810/* This has no reason to be in this file except that adding new files is a
811 bit of a pain */
812
813typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyObject_HEAD
Victor Stinner0db176f2012-04-16 00:16:30 +0200815 PyObject *mapping;
816} mappingproxyobject;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000817
Martin v. Löwis18e16552006-02-15 17:27:45 +0000818static Py_ssize_t
Victor Stinner0db176f2012-04-16 00:16:30 +0200819mappingproxy_len(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000820{
Victor Stinner0db176f2012-04-16 00:16:30 +0200821 return PyObject_Size(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000822}
823
824static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200825mappingproxy_getitem(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000826{
Victor Stinner0db176f2012-04-16 00:16:30 +0200827 return PyObject_GetItem(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000828}
829
Victor Stinner0db176f2012-04-16 00:16:30 +0200830static PyMappingMethods mappingproxy_as_mapping = {
831 (lenfunc)mappingproxy_len, /* mp_length */
832 (binaryfunc)mappingproxy_getitem, /* mp_subscript */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 0, /* mp_ass_subscript */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000834};
835
836static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200837mappingproxy_contains(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000838{
Victor Stinner0db176f2012-04-16 00:16:30 +0200839 if (PyDict_CheckExact(pp->mapping))
840 return PyDict_Contains(pp->mapping, key);
841 else
842 return PySequence_Contains(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000843}
844
Victor Stinner0db176f2012-04-16 00:16:30 +0200845static PySequenceMethods mappingproxy_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 0, /* sq_length */
847 0, /* sq_concat */
848 0, /* sq_repeat */
849 0, /* sq_item */
850 0, /* sq_slice */
851 0, /* sq_ass_item */
852 0, /* sq_ass_slice */
Victor Stinner0db176f2012-04-16 00:16:30 +0200853 (objobjproc)mappingproxy_contains, /* sq_contains */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 0, /* sq_inplace_concat */
855 0, /* sq_inplace_repeat */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000856};
857
858static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200859mappingproxy_get(mappingproxyobject *pp, PyObject *args)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 PyObject *key, *def = Py_None;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200862 _Py_IDENTIFIER(get);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
865 return NULL;
Victor Stinner7e425412016-12-09 00:36:19 +0100866 return _PyObject_CallMethodIdObjArgs(pp->mapping, &PyId_get,
867 key, def, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000868}
869
870static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200871mappingproxy_keys(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200873 _Py_IDENTIFIER(keys);
Victor Stinner0db176f2012-04-16 00:16:30 +0200874 return _PyObject_CallMethodId(pp->mapping, &PyId_keys, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000875}
876
877static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200878mappingproxy_values(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000879{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200880 _Py_IDENTIFIER(values);
Victor Stinner0db176f2012-04-16 00:16:30 +0200881 return _PyObject_CallMethodId(pp->mapping, &PyId_values, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882}
883
884static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200885mappingproxy_items(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000886{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200887 _Py_IDENTIFIER(items);
Victor Stinner0db176f2012-04-16 00:16:30 +0200888 return _PyObject_CallMethodId(pp->mapping, &PyId_items, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000889}
890
891static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200892mappingproxy_copy(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000893{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200894 _Py_IDENTIFIER(copy);
Victor Stinner0db176f2012-04-16 00:16:30 +0200895 return _PyObject_CallMethodId(pp->mapping, &PyId_copy, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896}
897
Victor Stinner0db176f2012-04-16 00:16:30 +0200898/* WARNING: mappingproxy methods must not give access
899 to the underlying mapping */
900
901static PyMethodDef mappingproxy_methods[] = {
902 {"get", (PyCFunction)mappingproxy_get, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
Victor Stinner0db176f2012-04-16 00:16:30 +0200904 " d defaults to None.")},
905 {"keys", (PyCFunction)mappingproxy_keys, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 PyDoc_STR("D.keys() -> list of D's keys")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200907 {"values", (PyCFunction)mappingproxy_values, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 PyDoc_STR("D.values() -> list of D's values")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200909 {"items", (PyCFunction)mappingproxy_items, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200911 {"copy", (PyCFunction)mappingproxy_copy, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 PyDoc_STR("D.copy() -> a shallow copy of D")},
913 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000914};
915
916static void
Victor Stinner0db176f2012-04-16 00:16:30 +0200917mappingproxy_dealloc(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 _PyObject_GC_UNTRACK(pp);
Victor Stinner0db176f2012-04-16 00:16:30 +0200920 Py_DECREF(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000922}
923
924static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200925mappingproxy_getiter(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926{
Victor Stinner0db176f2012-04-16 00:16:30 +0200927 return PyObject_GetIter(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000928}
929
Neil Schemenauer26775122001-10-21 22:26:43 +0000930static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200931mappingproxy_str(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000932{
Victor Stinner0db176f2012-04-16 00:16:30 +0200933 return PyObject_Str(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000934}
935
Ezio Melottiac53ab62010-12-18 14:59:43 +0000936static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200937mappingproxy_repr(mappingproxyobject *pp)
Ezio Melottiac53ab62010-12-18 14:59:43 +0000938{
Victor Stinner0db176f2012-04-16 00:16:30 +0200939 return PyUnicode_FromFormat("mappingproxy(%R)", pp->mapping);
Ezio Melottiac53ab62010-12-18 14:59:43 +0000940}
941
Guido van Rossum048eb752001-10-02 21:24:57 +0000942static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200943mappingproxy_traverse(PyObject *self, visitproc visit, void *arg)
Guido van Rossum048eb752001-10-02 21:24:57 +0000944{
Victor Stinner0db176f2012-04-16 00:16:30 +0200945 mappingproxyobject *pp = (mappingproxyobject *)self;
946 Py_VISIT(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000948}
949
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000950static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200951mappingproxy_richcompare(mappingproxyobject *v, PyObject *w, int op)
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000952{
Victor Stinner0db176f2012-04-16 00:16:30 +0200953 return PyObject_RichCompare(v->mapping, w, op);
954}
955
956static int
957mappingproxy_check_mapping(PyObject *mapping)
958{
959 if (!PyMapping_Check(mapping)
960 || PyList_Check(mapping)
961 || PyTuple_Check(mapping)) {
962 PyErr_Format(PyExc_TypeError,
963 "mappingproxy() argument must be a mapping, not %s",
964 Py_TYPE(mapping)->tp_name);
965 return -1;
966 }
967 return 0;
968}
969
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200970/*[clinic input]
971@classmethod
972mappingproxy.__new__ as mappingproxy_new
Victor Stinner0db176f2012-04-16 00:16:30 +0200973
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200974 mapping: object
975
976[clinic start generated code]*/
977
978static PyObject *
979mappingproxy_new_impl(PyTypeObject *type, PyObject *mapping)
980/*[clinic end generated code: output=65f27f02d5b68fa7 input=d2d620d4f598d4f8]*/
981{
982 mappingproxyobject *mappingproxy;
Victor Stinner0db176f2012-04-16 00:16:30 +0200983
984 if (mappingproxy_check_mapping(mapping) == -1)
985 return NULL;
986
987 mappingproxy = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
988 if (mappingproxy == NULL)
989 return NULL;
990 Py_INCREF(mapping);
991 mappingproxy->mapping = mapping;
992 _PyObject_GC_TRACK(mappingproxy);
993 return (PyObject *)mappingproxy;
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000994}
995
Tim Peters6d6c1a32001-08-02 04:15:00 +0000996PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200997PyDictProxy_New(PyObject *mapping)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998{
Victor Stinner0db176f2012-04-16 00:16:30 +0200999 mappingproxyobject *pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001000
Victor Stinner0db176f2012-04-16 00:16:30 +02001001 if (mappingproxy_check_mapping(mapping) == -1)
1002 return NULL;
1003
1004 pp = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 if (pp != NULL) {
Victor Stinner0db176f2012-04-16 00:16:30 +02001006 Py_INCREF(mapping);
1007 pp->mapping = mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 _PyObject_GC_TRACK(pp);
1009 }
1010 return (PyObject *)pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001011}
1012
1013
1014/* --- Wrapper object for "slot" methods --- */
1015
1016/* This has no reason to be in this file except that adding new files is a
1017 bit of a pain */
1018
1019typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 PyObject_HEAD
1021 PyWrapperDescrObject *descr;
1022 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001023} wrapperobject;
1024
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001025#define Wrapper_Check(v) (Py_TYPE(v) == &_PyMethodWrapper_Type)
Mark Dickinson211c6252009-02-01 10:28:51 +00001026
Tim Peters6d6c1a32001-08-02 04:15:00 +00001027static void
1028wrapper_dealloc(wrapperobject *wp)
1029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 PyObject_GC_UnTrack(wp);
1031 Py_TRASHCAN_SAFE_BEGIN(wp)
1032 Py_XDECREF(wp->descr);
1033 Py_XDECREF(wp->self);
1034 PyObject_GC_Del(wp);
1035 Py_TRASHCAN_SAFE_END(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001036}
1037
Mark Dickinson211c6252009-02-01 10:28:51 +00001038static PyObject *
1039wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +00001040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 PyWrapperDescrObject *a_descr, *b_descr;
Mark Dickinson211c6252009-02-01 10:28:51 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 assert(a != NULL && b != NULL);
Mark Dickinson211c6252009-02-01 10:28:51 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 /* both arguments should be wrapperobjects */
1046 if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
stratakise8b19652017-11-02 11:32:54 +01001047 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 /* compare by descriptor address; if the descriptors are the same,
1051 compare by the objects they're bound to */
1052 a_descr = ((wrapperobject *)a)->descr;
1053 b_descr = ((wrapperobject *)b)->descr;
1054 if (a_descr == b_descr) {
1055 a = ((wrapperobject *)a)->self;
1056 b = ((wrapperobject *)b)->self;
1057 return PyObject_RichCompare(a, b, op);
1058 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001059
stratakise8b19652017-11-02 11:32:54 +01001060 Py_RETURN_RICHCOMPARE(a_descr, b_descr, op);
Armin Rigoc6686b72005-11-07 08:38:00 +00001061}
1062
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001063static Py_hash_t
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001064wrapper_hash(wrapperobject *wp)
1065{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001066 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 x = _Py_HashPointer(wp->descr);
1068 if (x == -1)
1069 return -1;
1070 y = PyObject_Hash(wp->self);
1071 if (y == -1)
1072 return -1;
1073 x = x ^ y;
1074 if (x == -1)
1075 x = -2;
1076 return x;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001077}
1078
Armin Rigoc6686b72005-11-07 08:38:00 +00001079static PyObject *
1080wrapper_repr(wrapperobject *wp)
1081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 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 Rigoc6686b72005-11-07 08:38:00 +00001086}
1087
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001088static PyObject *
1089wrapper_reduce(wrapperobject *wp)
1090{
1091 PyObject *builtins;
1092 PyObject *getattr;
1093 _Py_IDENTIFIER(getattr);
1094
1095 builtins = PyEval_GetBuiltins();
1096 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
1097 return Py_BuildValue("O(OO)", getattr, wp->self, PyDescr_NAME(wp->descr));
1098}
1099
1100static PyMethodDef wrapper_methods[] = {
1101 {"__reduce__", (PyCFunction)wrapper_reduce, METH_NOARGS, NULL},
1102 {NULL, NULL}
1103};
1104
Armin Rigoc6686b72005-11-07 08:38:00 +00001105static PyMemberDef wrapper_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
1107 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001108};
1109
1110static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +00001111wrapper_objclass(wrapperobject *wp)
1112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 PyObject *c = (PyObject *)PyDescr_TYPE(wp->descr);
Armin Rigoc6686b72005-11-07 08:38:00 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 Py_INCREF(c);
1116 return c;
Armin Rigoc6686b72005-11-07 08:38:00 +00001117}
1118
1119static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001120wrapper_name(wrapperobject *wp)
1121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001125}
1126
1127static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -08001128wrapper_doc(wrapperobject *wp, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001129{
Larry Hastings2623c8c2014-02-08 22:15:29 -08001130 return _PyType_GetDocFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
Larry Hastings5c661892014-01-24 06:17:25 -08001131}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001132
Larry Hastings5c661892014-01-24 06:17:25 -08001133static PyObject *
1134wrapper_text_signature(wrapperobject *wp, void *closure)
1135{
Larry Hastings2623c8c2014-02-08 22:15:29 -08001136 return _PyType_GetTextSignatureFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001137}
1138
Antoine Pitrou9d574812011-12-12 13:47:25 +01001139static PyObject *
1140wrapper_qualname(wrapperobject *wp)
1141{
1142 return descr_get_qualname((PyDescrObject *)wp->descr);
1143}
1144
Guido van Rossum32d34c82001-09-20 21:45:26 +00001145static PyGetSetDef wrapper_getsets[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 {"__objclass__", (getter)wrapper_objclass},
1147 {"__name__", (getter)wrapper_name},
Antoine Pitrou9d574812011-12-12 13:47:25 +01001148 {"__qualname__", (getter)wrapper_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 {"__doc__", (getter)wrapper_doc},
Larry Hastings5c661892014-01-24 06:17:25 -08001150 {"__text_signature__", (getter)wrapper_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001152};
1153
1154static PyObject *
1155wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
1156{
Serhiy Storchaka5e02c782017-09-21 14:25:36 +03001157 return wrapperdescr_raw_call(wp->descr, wp->self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001158}
1159
Guido van Rossum048eb752001-10-02 21:24:57 +00001160static int
1161wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 wrapperobject *wp = (wrapperobject *)self;
1164 Py_VISIT(wp->descr);
1165 Py_VISIT(wp->self);
1166 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001167}
1168
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001169PyTypeObject _PyMethodWrapper_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1171 "method-wrapper", /* tp_name */
1172 sizeof(wrapperobject), /* tp_basicsize */
1173 0, /* tp_itemsize */
1174 /* methods */
1175 (destructor)wrapper_dealloc, /* tp_dealloc */
1176 0, /* tp_print */
1177 0, /* tp_getattr */
1178 0, /* tp_setattr */
1179 0, /* tp_reserved */
1180 (reprfunc)wrapper_repr, /* tp_repr */
1181 0, /* tp_as_number */
1182 0, /* tp_as_sequence */
1183 0, /* tp_as_mapping */
1184 (hashfunc)wrapper_hash, /* tp_hash */
1185 (ternaryfunc)wrapper_call, /* tp_call */
1186 0, /* tp_str */
1187 PyObject_GenericGetAttr, /* tp_getattro */
1188 0, /* tp_setattro */
1189 0, /* tp_as_buffer */
1190 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1191 0, /* tp_doc */
1192 wrapper_traverse, /* tp_traverse */
1193 0, /* tp_clear */
1194 wrapper_richcompare, /* tp_richcompare */
1195 0, /* tp_weaklistoffset */
1196 0, /* tp_iter */
1197 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001198 wrapper_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 wrapper_members, /* tp_members */
1200 wrapper_getsets, /* tp_getset */
1201 0, /* tp_base */
1202 0, /* tp_dict */
1203 0, /* tp_descr_get */
1204 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001205};
1206
1207PyObject *
1208PyWrapper_New(PyObject *d, PyObject *self)
1209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 wrapperobject *wp;
1211 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1214 descr = (PyWrapperDescrObject *)d;
Victor Stinner3249dec2011-05-01 23:19:15 +02001215 assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +02001216 (PyObject *)PyDescr_TYPE(descr)));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001217
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001218 wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (wp != NULL) {
1220 Py_INCREF(descr);
1221 wp->descr = descr;
1222 Py_INCREF(self);
1223 wp->self = self;
1224 _PyObject_GC_TRACK(wp);
1225 }
1226 return (PyObject *)wp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001227}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001228
1229
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001230/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001231
1232/*
Serhiy Storchakad741a882015-06-11 00:06:39 +03001233class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1236 if doc is None and fget is not None and hasattr(fget, "__doc__"):
Serhiy Storchakad741a882015-06-11 00:06:39 +03001237 doc = fget.__doc__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 self.__get = fget
1239 self.__set = fset
1240 self.__del = fdel
1241 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 def __get__(self, inst, type=None):
1244 if inst is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001245 return self
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 if self.__get is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001247 raise AttributeError, "unreadable attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 def __set__(self, inst, value):
1251 if self.__set is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001252 raise AttributeError, "can't set attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 return self.__set(inst, value)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 def __delete__(self, inst):
1256 if self.__del is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001257 raise AttributeError, "can't delete attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 return self.__del(inst)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001259
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001260*/
1261
1262typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 PyObject_HEAD
1264 PyObject *prop_get;
1265 PyObject *prop_set;
1266 PyObject *prop_del;
1267 PyObject *prop_doc;
1268 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001269} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001270
Christian Heimes0449f632007-12-15 01:27:15 +00001271static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
Benjamin Peterson93964832010-06-28 03:07:10 +00001272 PyObject *);
Christian Heimes0449f632007-12-15 01:27:15 +00001273
Tim Peters66c1a522001-09-24 21:17:50 +00001274static PyMemberDef property_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1276 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1277 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
Raymond Hettingereac503a2015-05-13 01:09:59 -07001278 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 {0}
Tim Peters66c1a522001-09-24 21:17:50 +00001280};
1281
Christian Heimes0449f632007-12-15 01:27:15 +00001282
Guido van Rossum58da9312007-11-10 23:39:45 +00001283PyDoc_STRVAR(getter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 "Descriptor to change the getter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001285
Neal Norwitz32dde222008-04-15 06:43:13 +00001286static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001287property_getter(PyObject *self, PyObject *getter)
1288{
Benjamin Peterson93964832010-06-28 03:07:10 +00001289 return property_copy(self, getter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001290}
1291
Christian Heimes0449f632007-12-15 01:27:15 +00001292
Guido van Rossum58da9312007-11-10 23:39:45 +00001293PyDoc_STRVAR(setter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001295
Neal Norwitz32dde222008-04-15 06:43:13 +00001296static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001297property_setter(PyObject *self, PyObject *setter)
1298{
Benjamin Peterson93964832010-06-28 03:07:10 +00001299 return property_copy(self, NULL, setter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001300}
1301
Christian Heimes0449f632007-12-15 01:27:15 +00001302
Guido van Rossum58da9312007-11-10 23:39:45 +00001303PyDoc_STRVAR(deleter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 "Descriptor to change the deleter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001305
Neal Norwitz32dde222008-04-15 06:43:13 +00001306static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001307property_deleter(PyObject *self, PyObject *deleter)
1308{
Benjamin Peterson93964832010-06-28 03:07:10 +00001309 return property_copy(self, NULL, NULL, deleter);
Guido van Rossum58da9312007-11-10 23:39:45 +00001310}
1311
1312
Guido van Rossum58da9312007-11-10 23:39:45 +00001313static PyMethodDef property_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 {"getter", property_getter, METH_O, getter_doc},
1315 {"setter", property_setter, METH_O, setter_doc},
1316 {"deleter", property_deleter, METH_O, deleter_doc},
1317 {0}
Guido van Rossum58da9312007-11-10 23:39:45 +00001318};
1319
Tim Peters66c1a522001-09-24 21:17:50 +00001320
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001321static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001322property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 _PyObject_GC_UNTRACK(self);
1327 Py_XDECREF(gs->prop_get);
1328 Py_XDECREF(gs->prop_set);
1329 Py_XDECREF(gs->prop_del);
1330 Py_XDECREF(gs->prop_doc);
1331 self->ob_type->tp_free(self);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001332}
1333
1334static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001335property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001336{
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001337 static PyObject * volatile cached_args = NULL;
1338 PyObject *args;
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001339 PyObject *ret;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (obj == NULL || obj == Py_None) {
1343 Py_INCREF(self);
1344 return self;
1345 }
1346 if (gs->prop_get == NULL) {
1347 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1348 return NULL;
1349 }
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001350 args = cached_args;
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001351 cached_args = NULL;
1352 if (!args) {
1353 args = PyTuple_New(1);
1354 if (!args)
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001355 return NULL;
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001356 _PyObject_GC_UNTRACK(args);
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001357 }
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001358 Py_INCREF(obj);
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001359 PyTuple_SET_ITEM(args, 0, obj);
1360 ret = PyObject_Call(gs->prop_get, args, NULL);
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001361 if (cached_args == NULL && Py_REFCNT(args) == 1) {
Serhiy Storchakafff9a312017-03-21 08:53:25 +02001362 assert(PyTuple_GET_SIZE(args) == 1);
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001363 assert(PyTuple_GET_ITEM(args, 0) == obj);
1364 cached_args = args;
1365 Py_DECREF(obj);
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001366 }
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001367 else {
1368 assert(Py_REFCNT(args) >= 1);
1369 _PyObject_GC_TRACK(args);
1370 Py_DECREF(args);
1371 }
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001372 return ret;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001373}
1374
1375static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001376property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 propertyobject *gs = (propertyobject *)self;
1379 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if (value == NULL)
1382 func = gs->prop_del;
1383 else
1384 func = gs->prop_set;
1385 if (func == NULL) {
1386 PyErr_SetString(PyExc_AttributeError,
1387 value == NULL ?
1388 "can't delete attribute" :
1389 "can't set attribute");
1390 return -1;
1391 }
1392 if (value == NULL)
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001393 res = PyObject_CallFunctionObjArgs(func, obj, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 else
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001395 res = PyObject_CallFunctionObjArgs(func, obj, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 if (res == NULL)
1397 return -1;
1398 Py_DECREF(res);
1399 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001400}
1401
Christian Heimes0449f632007-12-15 01:27:15 +00001402static PyObject *
Benjamin Peterson93964832010-06-28 03:07:10 +00001403property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
Christian Heimes0449f632007-12-15 01:27:15 +00001404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 propertyobject *pold = (propertyobject *)old;
Benjamin Peterson93964832010-06-28 03:07:10 +00001406 PyObject *new, *type, *doc;
Christian Heimes0449f632007-12-15 01:27:15 +00001407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 type = PyObject_Type(old);
1409 if (type == NULL)
1410 return NULL;
Christian Heimes0449f632007-12-15 01:27:15 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (get == NULL || get == Py_None) {
1413 Py_XDECREF(get);
1414 get = pold->prop_get ? pold->prop_get : Py_None;
1415 }
1416 if (set == NULL || set == Py_None) {
1417 Py_XDECREF(set);
1418 set = pold->prop_set ? pold->prop_set : Py_None;
1419 }
1420 if (del == NULL || del == Py_None) {
1421 Py_XDECREF(del);
1422 del = pold->prop_del ? pold->prop_del : Py_None;
1423 }
Benjamin Peterson93964832010-06-28 03:07:10 +00001424 if (pold->getter_doc && get != Py_None) {
1425 /* make _init use __doc__ from getter */
1426 doc = Py_None;
1427 }
1428 else {
1429 doc = pold->prop_doc ? pold->prop_doc : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 }
R. David Murrayb18500d2009-05-04 22:59:07 +00001431
Victor Stinner5abaa2b2016-12-09 16:22:32 +01001432 new = PyObject_CallFunctionObjArgs(type, get, set, del, doc, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 Py_DECREF(type);
1434 if (new == NULL)
1435 return NULL;
1436 return new;
Christian Heimes0449f632007-12-15 01:27:15 +00001437}
1438
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001439/*[clinic input]
1440property.__init__ as property_init
1441
1442 fget: object(c_default="NULL") = None
1443 function to be used for getting an attribute value
1444 fset: object(c_default="NULL") = None
1445 function to be used for setting an attribute value
1446 fdel: object(c_default="NULL") = None
1447 function to be used for del'ing an attribute
1448 doc: object(c_default="NULL") = None
1449 docstring
1450
1451Property attribute.
1452
1453Typical use is to define a managed attribute x:
1454
1455class C(object):
1456 def getx(self): return self._x
1457 def setx(self, value): self._x = value
1458 def delx(self): del self._x
1459 x = property(getx, setx, delx, "I'm the 'x' property.")
1460
1461Decorators make defining new properties or modifying existing ones easy:
1462
1463class C(object):
1464 @property
1465 def x(self):
1466 "I am the 'x' property."
1467 return self._x
1468 @x.setter
1469 def x(self, value):
1470 self._x = value
1471 @x.deleter
1472 def x(self):
1473 del self._x
1474[clinic start generated code]*/
1475
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001476static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001477property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
1478 PyObject *fdel, PyObject *doc)
1479/*[clinic end generated code: output=01a960742b692b57 input=dfb5dbbffc6932d5]*/
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001480{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001481 if (fget == Py_None)
1482 fget = NULL;
1483 if (fset == Py_None)
1484 fset = NULL;
1485 if (fdel == Py_None)
1486 fdel = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001487
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001488 Py_XINCREF(fget);
1489 Py_XINCREF(fset);
1490 Py_XINCREF(fdel);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 Py_XINCREF(doc);
Christian Heimes0449f632007-12-15 01:27:15 +00001492
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001493 self->prop_get = fget;
1494 self->prop_set = fset;
1495 self->prop_del = fdel;
1496 self->prop_doc = doc;
1497 self->getter_doc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 /* if no docstring given and the getter has one, use that one */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001500 if ((doc == NULL || doc == Py_None) && fget != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001501 _Py_IDENTIFIER(__doc__);
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001502 PyObject *get_doc = _PyObject_GetAttrId(fget, &PyId___doc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 if (get_doc) {
1504 if (Py_TYPE(self) == &PyProperty_Type) {
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001505 Py_XSETREF(self->prop_doc, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 }
1507 else {
1508 /* If this is a property subclass, put __doc__
1509 in dict of the subclass instance instead,
1510 otherwise it gets shadowed by __doc__ in the
1511 class's dict. */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001512 int err = _PyObject_SetAttrId((PyObject *)self, &PyId___doc__, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 Py_DECREF(get_doc);
1514 if (err < 0)
1515 return -1;
1516 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001517 self->getter_doc = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 }
1519 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1520 PyErr_Clear();
1521 }
1522 else {
1523 return -1;
1524 }
1525 }
1526
1527 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001528}
1529
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001530static PyObject *
1531property_get___isabstractmethod__(propertyobject *prop, void *closure)
1532{
1533 int res = _PyObject_IsAbstract(prop->prop_get);
1534 if (res == -1) {
1535 return NULL;
1536 }
1537 else if (res) {
1538 Py_RETURN_TRUE;
1539 }
1540
1541 res = _PyObject_IsAbstract(prop->prop_set);
1542 if (res == -1) {
1543 return NULL;
1544 }
1545 else if (res) {
1546 Py_RETURN_TRUE;
1547 }
1548
1549 res = _PyObject_IsAbstract(prop->prop_del);
1550 if (res == -1) {
1551 return NULL;
1552 }
1553 else if (res) {
1554 Py_RETURN_TRUE;
1555 }
1556 Py_RETURN_FALSE;
1557}
1558
1559static PyGetSetDef property_getsetlist[] = {
1560 {"__isabstractmethod__",
1561 (getter)property_get___isabstractmethod__, NULL,
1562 NULL,
1563 NULL},
1564 {NULL} /* Sentinel */
1565};
1566
Guido van Rossum048eb752001-10-02 21:24:57 +00001567static int
1568property_traverse(PyObject *self, visitproc visit, void *arg)
1569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 propertyobject *pp = (propertyobject *)self;
1571 Py_VISIT(pp->prop_get);
1572 Py_VISIT(pp->prop_set);
1573 Py_VISIT(pp->prop_del);
1574 Py_VISIT(pp->prop_doc);
1575 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001576}
1577
Raymond Hettingerd4be6912015-05-13 11:12:33 -07001578static int
1579property_clear(PyObject *self)
1580{
1581 propertyobject *pp = (propertyobject *)self;
1582 Py_CLEAR(pp->prop_doc);
1583 return 0;
1584}
1585
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001586#include "clinic/descrobject.c.h"
1587
1588PyTypeObject PyDictProxy_Type = {
1589 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1590 "mappingproxy", /* tp_name */
1591 sizeof(mappingproxyobject), /* tp_basicsize */
1592 0, /* tp_itemsize */
1593 /* methods */
1594 (destructor)mappingproxy_dealloc, /* tp_dealloc */
1595 0, /* tp_print */
1596 0, /* tp_getattr */
1597 0, /* tp_setattr */
1598 0, /* tp_reserved */
1599 (reprfunc)mappingproxy_repr, /* tp_repr */
1600 0, /* tp_as_number */
1601 &mappingproxy_as_sequence, /* tp_as_sequence */
1602 &mappingproxy_as_mapping, /* tp_as_mapping */
1603 0, /* tp_hash */
1604 0, /* tp_call */
1605 (reprfunc)mappingproxy_str, /* tp_str */
1606 PyObject_GenericGetAttr, /* tp_getattro */
1607 0, /* tp_setattro */
1608 0, /* tp_as_buffer */
1609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1610 0, /* tp_doc */
1611 mappingproxy_traverse, /* tp_traverse */
1612 0, /* tp_clear */
1613 (richcmpfunc)mappingproxy_richcompare, /* tp_richcompare */
1614 0, /* tp_weaklistoffset */
1615 (getiterfunc)mappingproxy_getiter, /* tp_iter */
1616 0, /* tp_iternext */
1617 mappingproxy_methods, /* tp_methods */
1618 0, /* tp_members */
1619 0, /* tp_getset */
1620 0, /* tp_base */
1621 0, /* tp_dict */
1622 0, /* tp_descr_get */
1623 0, /* tp_descr_set */
1624 0, /* tp_dictoffset */
1625 0, /* tp_init */
1626 0, /* tp_alloc */
1627 mappingproxy_new, /* tp_new */
1628};
1629
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001630PyTypeObject PyProperty_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1632 "property", /* tp_name */
1633 sizeof(propertyobject), /* tp_basicsize */
1634 0, /* tp_itemsize */
1635 /* methods */
1636 property_dealloc, /* tp_dealloc */
1637 0, /* tp_print */
1638 0, /* tp_getattr */
1639 0, /* tp_setattr */
1640 0, /* tp_reserved */
1641 0, /* tp_repr */
1642 0, /* tp_as_number */
1643 0, /* tp_as_sequence */
1644 0, /* tp_as_mapping */
1645 0, /* tp_hash */
1646 0, /* tp_call */
1647 0, /* tp_str */
1648 PyObject_GenericGetAttr, /* tp_getattro */
1649 0, /* tp_setattro */
1650 0, /* tp_as_buffer */
1651 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1652 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001653 property_init__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 property_traverse, /* tp_traverse */
Raymond Hettingerd4be6912015-05-13 11:12:33 -07001655 (inquiry)property_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 0, /* tp_richcompare */
1657 0, /* tp_weaklistoffset */
1658 0, /* tp_iter */
1659 0, /* tp_iternext */
1660 property_methods, /* tp_methods */
1661 property_members, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001662 property_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 0, /* tp_base */
1664 0, /* tp_dict */
1665 property_descr_get, /* tp_descr_get */
1666 property_descr_set, /* tp_descr_set */
1667 0, /* tp_dictoffset */
1668 property_init, /* tp_init */
1669 PyType_GenericAlloc, /* tp_alloc */
1670 PyType_GenericNew, /* tp_new */
1671 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001672};