blob: 5dc27ef67278a41c8d2d595c2e2c86d60e0cf8bc [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 +00001038#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
1039
1040static PyObject *
1041wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +00001042{
Benjamin Petersonca470632016-09-06 13:47:26 -07001043 intptr_t result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyObject *v;
1045 PyWrapperDescrObject *a_descr, *b_descr;
Mark Dickinson211c6252009-02-01 10:28:51 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 assert(a != NULL && b != NULL);
Mark Dickinson211c6252009-02-01 10:28:51 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 /* both arguments should be wrapperobjects */
1050 if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
1051 v = Py_NotImplemented;
1052 Py_INCREF(v);
1053 return v;
1054 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 /* compare by descriptor address; if the descriptors are the same,
1057 compare by the objects they're bound to */
1058 a_descr = ((wrapperobject *)a)->descr;
1059 b_descr = ((wrapperobject *)b)->descr;
1060 if (a_descr == b_descr) {
1061 a = ((wrapperobject *)a)->self;
1062 b = ((wrapperobject *)b)->self;
1063 return PyObject_RichCompare(a, b, op);
1064 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 result = a_descr - b_descr;
1067 switch (op) {
1068 case Py_EQ:
1069 v = TEST_COND(result == 0);
1070 break;
1071 case Py_NE:
1072 v = TEST_COND(result != 0);
1073 break;
1074 case Py_LE:
1075 v = TEST_COND(result <= 0);
1076 break;
1077 case Py_GE:
1078 v = TEST_COND(result >= 0);
1079 break;
1080 case Py_LT:
1081 v = TEST_COND(result < 0);
1082 break;
1083 case Py_GT:
1084 v = TEST_COND(result > 0);
1085 break;
1086 default:
1087 PyErr_BadArgument();
1088 return NULL;
1089 }
1090 Py_INCREF(v);
1091 return v;
Armin Rigoc6686b72005-11-07 08:38:00 +00001092}
1093
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001094static Py_hash_t
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001095wrapper_hash(wrapperobject *wp)
1096{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001097 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 x = _Py_HashPointer(wp->descr);
1099 if (x == -1)
1100 return -1;
1101 y = PyObject_Hash(wp->self);
1102 if (y == -1)
1103 return -1;
1104 x = x ^ y;
1105 if (x == -1)
1106 x = -2;
1107 return x;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001108}
1109
Armin Rigoc6686b72005-11-07 08:38:00 +00001110static PyObject *
1111wrapper_repr(wrapperobject *wp)
1112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
1114 wp->descr->d_base->name,
1115 wp->self->ob_type->tp_name,
1116 wp->self);
Armin Rigoc6686b72005-11-07 08:38:00 +00001117}
1118
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001119static PyObject *
1120wrapper_reduce(wrapperobject *wp)
1121{
1122 PyObject *builtins;
1123 PyObject *getattr;
1124 _Py_IDENTIFIER(getattr);
1125
1126 builtins = PyEval_GetBuiltins();
1127 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
1128 return Py_BuildValue("O(OO)", getattr, wp->self, PyDescr_NAME(wp->descr));
1129}
1130
1131static PyMethodDef wrapper_methods[] = {
1132 {"__reduce__", (PyCFunction)wrapper_reduce, METH_NOARGS, NULL},
1133 {NULL, NULL}
1134};
1135
Armin Rigoc6686b72005-11-07 08:38:00 +00001136static PyMemberDef wrapper_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
1138 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001139};
1140
1141static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +00001142wrapper_objclass(wrapperobject *wp)
1143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 PyObject *c = (PyObject *)PyDescr_TYPE(wp->descr);
Armin Rigoc6686b72005-11-07 08:38:00 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 Py_INCREF(c);
1147 return c;
Armin Rigoc6686b72005-11-07 08:38:00 +00001148}
1149
1150static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001151wrapper_name(wrapperobject *wp)
1152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001156}
1157
1158static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -08001159wrapper_doc(wrapperobject *wp, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001160{
Larry Hastings2623c8c2014-02-08 22:15:29 -08001161 return _PyType_GetDocFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
Larry Hastings5c661892014-01-24 06:17:25 -08001162}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001163
Larry Hastings5c661892014-01-24 06:17:25 -08001164static PyObject *
1165wrapper_text_signature(wrapperobject *wp, void *closure)
1166{
Larry Hastings2623c8c2014-02-08 22:15:29 -08001167 return _PyType_GetTextSignatureFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001168}
1169
Antoine Pitrou9d574812011-12-12 13:47:25 +01001170static PyObject *
1171wrapper_qualname(wrapperobject *wp)
1172{
1173 return descr_get_qualname((PyDescrObject *)wp->descr);
1174}
1175
Guido van Rossum32d34c82001-09-20 21:45:26 +00001176static PyGetSetDef wrapper_getsets[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 {"__objclass__", (getter)wrapper_objclass},
1178 {"__name__", (getter)wrapper_name},
Antoine Pitrou9d574812011-12-12 13:47:25 +01001179 {"__qualname__", (getter)wrapper_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 {"__doc__", (getter)wrapper_doc},
Larry Hastings5c661892014-01-24 06:17:25 -08001181 {"__text_signature__", (getter)wrapper_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183};
1184
1185static PyObject *
1186wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
1187{
Serhiy Storchaka5e02c782017-09-21 14:25:36 +03001188 return wrapperdescr_raw_call(wp->descr, wp->self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001189}
1190
Guido van Rossum048eb752001-10-02 21:24:57 +00001191static int
1192wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 wrapperobject *wp = (wrapperobject *)self;
1195 Py_VISIT(wp->descr);
1196 Py_VISIT(wp->self);
1197 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001198}
1199
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001200PyTypeObject _PyMethodWrapper_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1202 "method-wrapper", /* tp_name */
1203 sizeof(wrapperobject), /* tp_basicsize */
1204 0, /* tp_itemsize */
1205 /* methods */
1206 (destructor)wrapper_dealloc, /* tp_dealloc */
1207 0, /* tp_print */
1208 0, /* tp_getattr */
1209 0, /* tp_setattr */
1210 0, /* tp_reserved */
1211 (reprfunc)wrapper_repr, /* tp_repr */
1212 0, /* tp_as_number */
1213 0, /* tp_as_sequence */
1214 0, /* tp_as_mapping */
1215 (hashfunc)wrapper_hash, /* tp_hash */
1216 (ternaryfunc)wrapper_call, /* tp_call */
1217 0, /* tp_str */
1218 PyObject_GenericGetAttr, /* tp_getattro */
1219 0, /* tp_setattro */
1220 0, /* tp_as_buffer */
1221 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1222 0, /* tp_doc */
1223 wrapper_traverse, /* tp_traverse */
1224 0, /* tp_clear */
1225 wrapper_richcompare, /* tp_richcompare */
1226 0, /* tp_weaklistoffset */
1227 0, /* tp_iter */
1228 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001229 wrapper_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 wrapper_members, /* tp_members */
1231 wrapper_getsets, /* tp_getset */
1232 0, /* tp_base */
1233 0, /* tp_dict */
1234 0, /* tp_descr_get */
1235 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001236};
1237
1238PyObject *
1239PyWrapper_New(PyObject *d, PyObject *self)
1240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 wrapperobject *wp;
1242 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1245 descr = (PyWrapperDescrObject *)d;
Victor Stinner3249dec2011-05-01 23:19:15 +02001246 assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +02001247 (PyObject *)PyDescr_TYPE(descr)));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001248
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001249 wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (wp != NULL) {
1251 Py_INCREF(descr);
1252 wp->descr = descr;
1253 Py_INCREF(self);
1254 wp->self = self;
1255 _PyObject_GC_TRACK(wp);
1256 }
1257 return (PyObject *)wp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001258}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001259
1260
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001261/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001262
1263/*
Serhiy Storchakad741a882015-06-11 00:06:39 +03001264class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1267 if doc is None and fget is not None and hasattr(fget, "__doc__"):
Serhiy Storchakad741a882015-06-11 00:06:39 +03001268 doc = fget.__doc__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 self.__get = fget
1270 self.__set = fset
1271 self.__del = fdel
1272 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 def __get__(self, inst, type=None):
1275 if inst is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001276 return self
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if self.__get is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001278 raise AttributeError, "unreadable attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 def __set__(self, inst, value):
1282 if self.__set is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001283 raise AttributeError, "can't set attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 return self.__set(inst, value)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 def __delete__(self, inst):
1287 if self.__del is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001288 raise AttributeError, "can't delete attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 return self.__del(inst)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001290
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001291*/
1292
1293typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 PyObject_HEAD
1295 PyObject *prop_get;
1296 PyObject *prop_set;
1297 PyObject *prop_del;
1298 PyObject *prop_doc;
1299 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001300} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001301
Christian Heimes0449f632007-12-15 01:27:15 +00001302static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
Benjamin Peterson93964832010-06-28 03:07:10 +00001303 PyObject *);
Christian Heimes0449f632007-12-15 01:27:15 +00001304
Tim Peters66c1a522001-09-24 21:17:50 +00001305static PyMemberDef property_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1307 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1308 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
Raymond Hettingereac503a2015-05-13 01:09:59 -07001309 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 {0}
Tim Peters66c1a522001-09-24 21:17:50 +00001311};
1312
Christian Heimes0449f632007-12-15 01:27:15 +00001313
Guido van Rossum58da9312007-11-10 23:39:45 +00001314PyDoc_STRVAR(getter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 "Descriptor to change the getter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001316
Neal Norwitz32dde222008-04-15 06:43:13 +00001317static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001318property_getter(PyObject *self, PyObject *getter)
1319{
Benjamin Peterson93964832010-06-28 03:07:10 +00001320 return property_copy(self, getter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001321}
1322
Christian Heimes0449f632007-12-15 01:27:15 +00001323
Guido van Rossum58da9312007-11-10 23:39:45 +00001324PyDoc_STRVAR(setter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001326
Neal Norwitz32dde222008-04-15 06:43:13 +00001327static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001328property_setter(PyObject *self, PyObject *setter)
1329{
Benjamin Peterson93964832010-06-28 03:07:10 +00001330 return property_copy(self, NULL, setter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001331}
1332
Christian Heimes0449f632007-12-15 01:27:15 +00001333
Guido van Rossum58da9312007-11-10 23:39:45 +00001334PyDoc_STRVAR(deleter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 "Descriptor to change the deleter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001336
Neal Norwitz32dde222008-04-15 06:43:13 +00001337static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001338property_deleter(PyObject *self, PyObject *deleter)
1339{
Benjamin Peterson93964832010-06-28 03:07:10 +00001340 return property_copy(self, NULL, NULL, deleter);
Guido van Rossum58da9312007-11-10 23:39:45 +00001341}
1342
1343
Guido van Rossum58da9312007-11-10 23:39:45 +00001344static PyMethodDef property_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 {"getter", property_getter, METH_O, getter_doc},
1346 {"setter", property_setter, METH_O, setter_doc},
1347 {"deleter", property_deleter, METH_O, deleter_doc},
1348 {0}
Guido van Rossum58da9312007-11-10 23:39:45 +00001349};
1350
Tim Peters66c1a522001-09-24 21:17:50 +00001351
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001352static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001353property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 _PyObject_GC_UNTRACK(self);
1358 Py_XDECREF(gs->prop_get);
1359 Py_XDECREF(gs->prop_set);
1360 Py_XDECREF(gs->prop_del);
1361 Py_XDECREF(gs->prop_doc);
1362 self->ob_type->tp_free(self);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001363}
1364
1365static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001366property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001367{
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001368 static PyObject * volatile cached_args = NULL;
1369 PyObject *args;
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001370 PyObject *ret;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 if (obj == NULL || obj == Py_None) {
1374 Py_INCREF(self);
1375 return self;
1376 }
1377 if (gs->prop_get == NULL) {
1378 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1379 return NULL;
1380 }
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001381 args = cached_args;
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001382 cached_args = NULL;
1383 if (!args) {
1384 args = PyTuple_New(1);
1385 if (!args)
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001386 return NULL;
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001387 _PyObject_GC_UNTRACK(args);
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001388 }
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001389 Py_INCREF(obj);
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001390 PyTuple_SET_ITEM(args, 0, obj);
1391 ret = PyObject_Call(gs->prop_get, args, NULL);
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001392 if (cached_args == NULL && Py_REFCNT(args) == 1) {
Serhiy Storchakafff9a312017-03-21 08:53:25 +02001393 assert(PyTuple_GET_SIZE(args) == 1);
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001394 assert(PyTuple_GET_ITEM(args, 0) == obj);
1395 cached_args = args;
1396 Py_DECREF(obj);
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001397 }
Serhiy Storchaka7822f152016-05-04 21:42:05 +03001398 else {
1399 assert(Py_REFCNT(args) >= 1);
1400 _PyObject_GC_TRACK(args);
1401 Py_DECREF(args);
1402 }
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001403 return ret;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001404}
1405
1406static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001407property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 propertyobject *gs = (propertyobject *)self;
1410 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (value == NULL)
1413 func = gs->prop_del;
1414 else
1415 func = gs->prop_set;
1416 if (func == NULL) {
1417 PyErr_SetString(PyExc_AttributeError,
1418 value == NULL ?
1419 "can't delete attribute" :
1420 "can't set attribute");
1421 return -1;
1422 }
1423 if (value == NULL)
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001424 res = PyObject_CallFunctionObjArgs(func, obj, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 else
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001426 res = PyObject_CallFunctionObjArgs(func, obj, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 if (res == NULL)
1428 return -1;
1429 Py_DECREF(res);
1430 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001431}
1432
Christian Heimes0449f632007-12-15 01:27:15 +00001433static PyObject *
Benjamin Peterson93964832010-06-28 03:07:10 +00001434property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
Christian Heimes0449f632007-12-15 01:27:15 +00001435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 propertyobject *pold = (propertyobject *)old;
Benjamin Peterson93964832010-06-28 03:07:10 +00001437 PyObject *new, *type, *doc;
Christian Heimes0449f632007-12-15 01:27:15 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 type = PyObject_Type(old);
1440 if (type == NULL)
1441 return NULL;
Christian Heimes0449f632007-12-15 01:27:15 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 if (get == NULL || get == Py_None) {
1444 Py_XDECREF(get);
1445 get = pold->prop_get ? pold->prop_get : Py_None;
1446 }
1447 if (set == NULL || set == Py_None) {
1448 Py_XDECREF(set);
1449 set = pold->prop_set ? pold->prop_set : Py_None;
1450 }
1451 if (del == NULL || del == Py_None) {
1452 Py_XDECREF(del);
1453 del = pold->prop_del ? pold->prop_del : Py_None;
1454 }
Benjamin Peterson93964832010-06-28 03:07:10 +00001455 if (pold->getter_doc && get != Py_None) {
1456 /* make _init use __doc__ from getter */
1457 doc = Py_None;
1458 }
1459 else {
1460 doc = pold->prop_doc ? pold->prop_doc : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 }
R. David Murrayb18500d2009-05-04 22:59:07 +00001462
Victor Stinner5abaa2b2016-12-09 16:22:32 +01001463 new = PyObject_CallFunctionObjArgs(type, get, set, del, doc, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 Py_DECREF(type);
1465 if (new == NULL)
1466 return NULL;
1467 return new;
Christian Heimes0449f632007-12-15 01:27:15 +00001468}
1469
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001470/*[clinic input]
1471property.__init__ as property_init
1472
1473 fget: object(c_default="NULL") = None
1474 function to be used for getting an attribute value
1475 fset: object(c_default="NULL") = None
1476 function to be used for setting an attribute value
1477 fdel: object(c_default="NULL") = None
1478 function to be used for del'ing an attribute
1479 doc: object(c_default="NULL") = None
1480 docstring
1481
1482Property attribute.
1483
1484Typical use is to define a managed attribute x:
1485
1486class C(object):
1487 def getx(self): return self._x
1488 def setx(self, value): self._x = value
1489 def delx(self): del self._x
1490 x = property(getx, setx, delx, "I'm the 'x' property.")
1491
1492Decorators make defining new properties or modifying existing ones easy:
1493
1494class C(object):
1495 @property
1496 def x(self):
1497 "I am the 'x' property."
1498 return self._x
1499 @x.setter
1500 def x(self, value):
1501 self._x = value
1502 @x.deleter
1503 def x(self):
1504 del self._x
1505[clinic start generated code]*/
1506
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001507static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001508property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
1509 PyObject *fdel, PyObject *doc)
1510/*[clinic end generated code: output=01a960742b692b57 input=dfb5dbbffc6932d5]*/
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001511{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001512 if (fget == Py_None)
1513 fget = NULL;
1514 if (fset == Py_None)
1515 fset = NULL;
1516 if (fdel == Py_None)
1517 fdel = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001518
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001519 Py_XINCREF(fget);
1520 Py_XINCREF(fset);
1521 Py_XINCREF(fdel);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 Py_XINCREF(doc);
Christian Heimes0449f632007-12-15 01:27:15 +00001523
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001524 self->prop_get = fget;
1525 self->prop_set = fset;
1526 self->prop_del = fdel;
1527 self->prop_doc = doc;
1528 self->getter_doc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 /* if no docstring given and the getter has one, use that one */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001531 if ((doc == NULL || doc == Py_None) && fget != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001532 _Py_IDENTIFIER(__doc__);
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001533 PyObject *get_doc = _PyObject_GetAttrId(fget, &PyId___doc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (get_doc) {
1535 if (Py_TYPE(self) == &PyProperty_Type) {
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001536 Py_XSETREF(self->prop_doc, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 }
1538 else {
1539 /* If this is a property subclass, put __doc__
1540 in dict of the subclass instance instead,
1541 otherwise it gets shadowed by __doc__ in the
1542 class's dict. */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001543 int err = _PyObject_SetAttrId((PyObject *)self, &PyId___doc__, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 Py_DECREF(get_doc);
1545 if (err < 0)
1546 return -1;
1547 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001548 self->getter_doc = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 }
1550 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1551 PyErr_Clear();
1552 }
1553 else {
1554 return -1;
1555 }
1556 }
1557
1558 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001559}
1560
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001561static PyObject *
1562property_get___isabstractmethod__(propertyobject *prop, void *closure)
1563{
1564 int res = _PyObject_IsAbstract(prop->prop_get);
1565 if (res == -1) {
1566 return NULL;
1567 }
1568 else if (res) {
1569 Py_RETURN_TRUE;
1570 }
1571
1572 res = _PyObject_IsAbstract(prop->prop_set);
1573 if (res == -1) {
1574 return NULL;
1575 }
1576 else if (res) {
1577 Py_RETURN_TRUE;
1578 }
1579
1580 res = _PyObject_IsAbstract(prop->prop_del);
1581 if (res == -1) {
1582 return NULL;
1583 }
1584 else if (res) {
1585 Py_RETURN_TRUE;
1586 }
1587 Py_RETURN_FALSE;
1588}
1589
1590static PyGetSetDef property_getsetlist[] = {
1591 {"__isabstractmethod__",
1592 (getter)property_get___isabstractmethod__, NULL,
1593 NULL,
1594 NULL},
1595 {NULL} /* Sentinel */
1596};
1597
Guido van Rossum048eb752001-10-02 21:24:57 +00001598static int
1599property_traverse(PyObject *self, visitproc visit, void *arg)
1600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 propertyobject *pp = (propertyobject *)self;
1602 Py_VISIT(pp->prop_get);
1603 Py_VISIT(pp->prop_set);
1604 Py_VISIT(pp->prop_del);
1605 Py_VISIT(pp->prop_doc);
1606 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001607}
1608
Raymond Hettingerd4be6912015-05-13 11:12:33 -07001609static int
1610property_clear(PyObject *self)
1611{
1612 propertyobject *pp = (propertyobject *)self;
1613 Py_CLEAR(pp->prop_doc);
1614 return 0;
1615}
1616
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001617#include "clinic/descrobject.c.h"
1618
1619PyTypeObject PyDictProxy_Type = {
1620 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1621 "mappingproxy", /* tp_name */
1622 sizeof(mappingproxyobject), /* tp_basicsize */
1623 0, /* tp_itemsize */
1624 /* methods */
1625 (destructor)mappingproxy_dealloc, /* tp_dealloc */
1626 0, /* tp_print */
1627 0, /* tp_getattr */
1628 0, /* tp_setattr */
1629 0, /* tp_reserved */
1630 (reprfunc)mappingproxy_repr, /* tp_repr */
1631 0, /* tp_as_number */
1632 &mappingproxy_as_sequence, /* tp_as_sequence */
1633 &mappingproxy_as_mapping, /* tp_as_mapping */
1634 0, /* tp_hash */
1635 0, /* tp_call */
1636 (reprfunc)mappingproxy_str, /* tp_str */
1637 PyObject_GenericGetAttr, /* tp_getattro */
1638 0, /* tp_setattro */
1639 0, /* tp_as_buffer */
1640 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1641 0, /* tp_doc */
1642 mappingproxy_traverse, /* tp_traverse */
1643 0, /* tp_clear */
1644 (richcmpfunc)mappingproxy_richcompare, /* tp_richcompare */
1645 0, /* tp_weaklistoffset */
1646 (getiterfunc)mappingproxy_getiter, /* tp_iter */
1647 0, /* tp_iternext */
1648 mappingproxy_methods, /* tp_methods */
1649 0, /* tp_members */
1650 0, /* tp_getset */
1651 0, /* tp_base */
1652 0, /* tp_dict */
1653 0, /* tp_descr_get */
1654 0, /* tp_descr_set */
1655 0, /* tp_dictoffset */
1656 0, /* tp_init */
1657 0, /* tp_alloc */
1658 mappingproxy_new, /* tp_new */
1659};
1660
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001661PyTypeObject PyProperty_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1663 "property", /* tp_name */
1664 sizeof(propertyobject), /* tp_basicsize */
1665 0, /* tp_itemsize */
1666 /* methods */
1667 property_dealloc, /* tp_dealloc */
1668 0, /* tp_print */
1669 0, /* tp_getattr */
1670 0, /* tp_setattr */
1671 0, /* tp_reserved */
1672 0, /* tp_repr */
1673 0, /* tp_as_number */
1674 0, /* tp_as_sequence */
1675 0, /* tp_as_mapping */
1676 0, /* tp_hash */
1677 0, /* tp_call */
1678 0, /* tp_str */
1679 PyObject_GenericGetAttr, /* tp_getattro */
1680 0, /* tp_setattro */
1681 0, /* tp_as_buffer */
1682 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1683 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001684 property_init__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 property_traverse, /* tp_traverse */
Raymond Hettingerd4be6912015-05-13 11:12:33 -07001686 (inquiry)property_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 0, /* tp_richcompare */
1688 0, /* tp_weaklistoffset */
1689 0, /* tp_iter */
1690 0, /* tp_iternext */
1691 property_methods, /* tp_methods */
1692 property_members, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001693 property_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 0, /* tp_base */
1695 0, /* tp_dict */
1696 property_descr_get, /* tp_descr_get */
1697 property_descr_set, /* tp_descr_set */
1698 0, /* tp_dictoffset */
1699 property_init, /* tp_init */
1700 PyType_GenericAlloc, /* tp_alloc */
1701 PyType_GenericNew, /* tp_new */
1702 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001703};