blob: 6c99f9b211b93b27d0ee136b4b8702dd85e25abf [file] [log] [blame]
Tim Peters6d6c1a32001-08-02 04:15:00 +00001/* Descriptors -- a new, flexible way to describe attributes */
2
3#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01006#include "pycore_tupleobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00007#include "structmember.h" /* Why is this not included in Python.h? */
8
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02009/*[clinic input]
10class mappingproxy "mappingproxyobject *" "&PyDictProxy_Type"
11class property "propertyobject *" "&PyProperty_Type"
12[clinic start generated code]*/
13/*[clinic end generated code: output=da39a3ee5e6b4b0d input=556352653fd4c02e]*/
14
Tim Peters6d6c1a32001-08-02 04:15:00 +000015static void
16descr_dealloc(PyDescrObject *descr)
17{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 _PyObject_GC_UNTRACK(descr);
19 Py_XDECREF(descr->d_type);
20 Py_XDECREF(descr->d_name);
Antoine Pitrou9d574812011-12-12 13:47:25 +010021 Py_XDECREF(descr->d_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 PyObject_GC_Del(descr);
Tim Peters6d6c1a32001-08-02 04:15:00 +000023}
24
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000025static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000026descr_name(PyDescrObject *descr)
27{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
29 return descr->d_name;
30 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000031}
32
33static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020034descr_repr(PyDescrObject *descr, const char *format)
Tim Peters6d6c1a32001-08-02 04:15:00 +000035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 PyObject *name = NULL;
37 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
38 name = descr->d_name;
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 return PyUnicode_FromFormat(format, name, "?", descr->d_type->tp_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +000041}
42
43static PyObject *
44method_repr(PyMethodDescrObject *descr)
45{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 return descr_repr((PyDescrObject *)descr,
47 "<method '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000048}
49
50static PyObject *
51member_repr(PyMemberDescrObject *descr)
52{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 return descr_repr((PyDescrObject *)descr,
54 "<member '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000055}
56
57static PyObject *
58getset_repr(PyGetSetDescrObject *descr)
59{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 return descr_repr((PyDescrObject *)descr,
61 "<attribute '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000062}
63
64static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +000065wrapperdescr_repr(PyWrapperDescrObject *descr)
Tim Peters6d6c1a32001-08-02 04:15:00 +000066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 return descr_repr((PyDescrObject *)descr,
68 "<slot wrapper '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000069}
70
71static int
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000072descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +000073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 if (obj == NULL) {
75 Py_INCREF(descr);
76 *pres = (PyObject *)descr;
77 return 1;
78 }
79 if (!PyObject_TypeCheck(obj, descr->d_type)) {
80 PyErr_Format(PyExc_TypeError,
Inada Naoki62f95882019-04-01 17:56:11 +090081 "descriptor '%V' for '%.100s' objects "
82 "doesn't apply to a '%.100s' object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 descr_name((PyDescrObject *)descr), "?",
84 descr->d_type->tp_name,
85 obj->ob_type->tp_name);
86 *pres = NULL;
87 return 1;
88 }
89 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +000090}
91
92static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000093classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Petersbca1cbc2002-12-09 22:56:13 +000094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 /* Ensure a valid type. Class methods ignore obj. */
96 if (type == NULL) {
97 if (obj != NULL)
98 type = (PyObject *)obj->ob_type;
99 else {
100 /* Wot - no type?! */
101 PyErr_Format(PyExc_TypeError,
Inada Naoki62f95882019-04-01 17:56:11 +0900102 "descriptor '%V' for type '%.100s' "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 "needs either an object or a type",
104 descr_name((PyDescrObject *)descr), "?",
105 PyDescr_TYPE(descr)->tp_name);
106 return NULL;
107 }
108 }
109 if (!PyType_Check(type)) {
110 PyErr_Format(PyExc_TypeError,
Inada Naoki62f95882019-04-01 17:56:11 +0900111 "descriptor '%V' for type '%.100s' "
112 "needs a type, not a '%.100s' as arg 2",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 descr_name((PyDescrObject *)descr), "?",
114 PyDescr_TYPE(descr)->tp_name,
115 type->ob_type->tp_name);
116 return NULL;
117 }
118 if (!PyType_IsSubtype((PyTypeObject *)type, PyDescr_TYPE(descr))) {
119 PyErr_Format(PyExc_TypeError,
Inada Naoki62f95882019-04-01 17:56:11 +0900120 "descriptor '%V' requires a subtype of '%.100s' "
121 "but received '%.100s'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 descr_name((PyDescrObject *)descr), "?",
123 PyDescr_TYPE(descr)->tp_name,
124 ((PyTypeObject *)type)->tp_name);
125 return NULL;
126 }
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200127 return PyCFunction_NewEx(descr->d_method, type, NULL);
Tim Petersbca1cbc2002-12-09 22:56:13 +0000128}
129
130static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000131method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 if (descr_check((PyDescrObject *)descr, obj, &res))
136 return res;
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200137 return PyCFunction_NewEx(descr->d_method, obj, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000138}
139
140static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000141member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 if (descr_check((PyDescrObject *)descr, obj, &res))
146 return res;
Steve Dowerb82e17e2019-05-23 08:45:22 -0700147
148 if (descr->d_member->flags & READ_RESTRICTED) {
149 if (PySys_Audit("object.__getattr__", "Os",
150 obj ? obj : Py_None, descr->d_member->name) < 0) {
151 return NULL;
152 }
153 }
154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 return PyMember_GetOne((char *)obj, descr->d_member);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000156}
157
158static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000159getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 if (descr_check((PyDescrObject *)descr, obj, &res))
164 return res;
165 if (descr->d_getset->get != NULL)
166 return descr->d_getset->get(obj, descr->d_getset->closure);
167 PyErr_Format(PyExc_AttributeError,
168 "attribute '%V' of '%.100s' objects is not readable",
169 descr_name((PyDescrObject *)descr), "?",
170 PyDescr_TYPE(descr)->tp_name);
171 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000172}
173
174static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000175wrapperdescr_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 if (descr_check((PyDescrObject *)descr, obj, &res))
180 return res;
181 return PyWrapper_New((PyObject *)descr, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000182}
183
184static int
185descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 int *pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 assert(obj != NULL);
189 if (!PyObject_TypeCheck(obj, descr->d_type)) {
190 PyErr_Format(PyExc_TypeError,
191 "descriptor '%V' for '%.100s' objects "
Inada Naoki62f95882019-04-01 17:56:11 +0900192 "doesn't apply to a '%.100s' object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 descr_name(descr), "?",
194 descr->d_type->tp_name,
195 obj->ob_type->tp_name);
196 *pres = -1;
197 return 1;
198 }
199 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200}
201
202static int
203member_set(PyMemberDescrObject *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 return PyMember_SetOne((char *)obj, descr->d_member, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000210}
211
212static int
213getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
218 return res;
219 if (descr->d_getset->set != NULL)
220 return descr->d_getset->set(obj, value,
221 descr->d_getset->closure);
222 PyErr_Format(PyExc_AttributeError,
223 "attribute '%V' of '%.100s' objects is not writable",
224 descr_name((PyDescrObject *)descr), "?",
225 PyDescr_TYPE(descr)->tp_name);
226 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000227}
228
229static PyObject *
Victor Stinnerc5257232017-01-18 10:38:09 +0100230methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwargs)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000231{
Victor Stinnerc5257232017-01-18 10:38:09 +0100232 Py_ssize_t nargs;
233 PyObject *self, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 /* Make sure that the first argument is acceptable as 'self' */
236 assert(PyTuple_Check(args));
Victor Stinnerc5257232017-01-18 10:38:09 +0100237 nargs = PyTuple_GET_SIZE(args);
238 if (nargs < 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PyErr_Format(PyExc_TypeError,
240 "descriptor '%V' of '%.100s' "
241 "object needs an argument",
242 descr_name((PyDescrObject *)descr), "?",
243 PyDescr_TYPE(descr)->tp_name);
244 return NULL;
245 }
246 self = PyTuple_GET_ITEM(args, 0);
Victor Stinner3249dec2011-05-01 23:19:15 +0200247 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +0200248 (PyObject *)PyDescr_TYPE(descr))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 PyErr_Format(PyExc_TypeError,
Inada Naoki62f95882019-04-01 17:56:11 +0900250 "descriptor '%V' for '%.100s' objects "
251 "doesn't apply to a '%.100s' object",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 descr_name((PyDescrObject *)descr), "?",
253 PyDescr_TYPE(descr)->tp_name,
254 self->ob_type->tp_name);
255 return NULL;
256 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000257
Victor Stinnerc5257232017-01-18 10:38:09 +0100258 result = _PyMethodDef_RawFastCallDict(descr->d_method, self,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100259 &_PyTuple_ITEMS(args)[1], nargs - 1,
Victor Stinnerc5257232017-01-18 10:38:09 +0100260 kwargs);
261 result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000263}
264
INADA Naoki5566bbb2017-02-03 07:43:03 +0900265// same to methoddescr_call(), but use FASTCALL convention.
266PyObject *
267_PyMethodDescr_FastCallKeywords(PyObject *descrobj,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200268 PyObject *const *args, Py_ssize_t nargs,
INADA Naoki5566bbb2017-02-03 07:43:03 +0900269 PyObject *kwnames)
270{
271 assert(Py_TYPE(descrobj) == &PyMethodDescr_Type);
272 PyMethodDescrObject *descr = (PyMethodDescrObject *)descrobj;
273 PyObject *self, *result;
274
275 /* Make sure that the first argument is acceptable as 'self' */
276 if (nargs < 1) {
277 PyErr_Format(PyExc_TypeError,
278 "descriptor '%V' of '%.100s' "
279 "object needs an argument",
280 descr_name((PyDescrObject *)descr), "?",
281 PyDescr_TYPE(descr)->tp_name);
282 return NULL;
283 }
284 self = args[0];
285 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
286 (PyObject *)PyDescr_TYPE(descr))) {
287 PyErr_Format(PyExc_TypeError,
Inada Naoki62f95882019-04-01 17:56:11 +0900288 "descriptor '%V' for '%.100s' objects "
289 "doesn't apply to a '%.100s' object",
INADA Naoki5566bbb2017-02-03 07:43:03 +0900290 descr_name((PyDescrObject *)descr), "?",
291 PyDescr_TYPE(descr)->tp_name,
292 self->ob_type->tp_name);
293 return NULL;
294 }
295
296 result = _PyMethodDef_RawFastCallKeywords(descr->d_method, self,
297 args+1, nargs-1, kwnames);
298 result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
299 return result;
300}
301
Tim Peters6d6c1a32001-08-02 04:15:00 +0000302static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000303classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 PyObject *kwds)
Tim Petersbca1cbc2002-12-09 22:56:13 +0000305{
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400306 Py_ssize_t argc;
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300307 PyObject *self, *result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000308
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400309 /* Make sure that the first argument is acceptable as 'self' */
310 assert(PyTuple_Check(args));
311 argc = PyTuple_GET_SIZE(args);
312 if (argc < 1) {
313 PyErr_Format(PyExc_TypeError,
314 "descriptor '%V' of '%.100s' "
315 "object needs an argument",
316 descr_name((PyDescrObject *)descr), "?",
317 PyDescr_TYPE(descr)->tp_name);
318 return NULL;
319 }
320 self = PyTuple_GET_ITEM(args, 0);
321 if (!PyType_Check(self)) {
322 PyErr_Format(PyExc_TypeError,
323 "descriptor '%V' requires a type "
Inada Naoki871309c2019-03-26 18:26:33 +0900324 "but received a '%.100s' instance",
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400325 descr_name((PyDescrObject *)descr), "?",
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400326 self->ob_type->tp_name);
327 return NULL;
328 }
329 if (!PyType_IsSubtype((PyTypeObject *)self, PyDescr_TYPE(descr))) {
330 PyErr_Format(PyExc_TypeError,
Inada Naoki871309c2019-03-26 18:26:33 +0900331 "descriptor '%V' requires a subtype of '%.100s' "
332 "but received '%.100s'",
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400333 descr_name((PyDescrObject *)descr), "?",
334 PyDescr_TYPE(descr)->tp_name,
Inada Naoki871309c2019-03-26 18:26:33 +0900335 ((PyTypeObject*)self)->tp_name);
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400336 return NULL;
337 }
338
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300339 result = _PyMethodDef_RawFastCallDict(descr->d_method, self,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100340 &_PyTuple_ITEMS(args)[1], argc - 1,
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300341 kwds);
342 result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 return result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000344}
345
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300346Py_LOCAL_INLINE(PyObject *)
347wrapperdescr_raw_call(PyWrapperDescrObject *descr, PyObject *self,
348 PyObject *args, PyObject *kwds)
349{
350 wrapperfunc wrapper = descr->d_base->wrapper;
351
352 if (descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
Serhiy Storchaka1c607152018-11-27 21:34:27 +0200353 wrapperfunc_kwds wk = (wrapperfunc_kwds)(void(*)(void))wrapper;
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300354 return (*wk)(self, args, descr->d_wrapped, kwds);
355 }
356
357 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_GET_SIZE(kwds) != 0)) {
358 PyErr_Format(PyExc_TypeError,
359 "wrapper %s() takes no keyword arguments",
360 descr->d_base->name);
361 return NULL;
362 }
363 return (*wrapper)(self, args, descr->d_wrapped);
364}
365
Tim Petersbca1cbc2002-12-09 22:56:13 +0000366static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000367wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 Py_ssize_t argc;
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300370 PyObject *self, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* Make sure that the first argument is acceptable as 'self' */
373 assert(PyTuple_Check(args));
374 argc = PyTuple_GET_SIZE(args);
375 if (argc < 1) {
376 PyErr_Format(PyExc_TypeError,
377 "descriptor '%V' of '%.100s' "
378 "object needs an argument",
379 descr_name((PyDescrObject *)descr), "?",
380 PyDescr_TYPE(descr)->tp_name);
381 return NULL;
382 }
383 self = PyTuple_GET_ITEM(args, 0);
Victor Stinner3249dec2011-05-01 23:19:15 +0200384 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +0200385 (PyObject *)PyDescr_TYPE(descr))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyErr_Format(PyExc_TypeError,
387 "descriptor '%V' "
388 "requires a '%.100s' object "
389 "but received a '%.100s'",
390 descr_name((PyDescrObject *)descr), "?",
391 PyDescr_TYPE(descr)->tp_name,
392 self->ob_type->tp_name);
393 return NULL;
394 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000395
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300396 args = PyTuple_GetSlice(args, 1, argc);
397 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 return NULL;
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300399 }
400 result = wrapperdescr_raw_call(descr, self, args, kwds);
401 Py_DECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000403}
404
Serhiy Storchaka5e02c782017-09-21 14:25:36 +0300405
Tim Peters6d6c1a32001-08-02 04:15:00 +0000406static PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000407method_get_doc(PyMethodDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000408{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800409 return _PyType_GetDocFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
Larry Hastings5c661892014-01-24 06:17:25 -0800410}
411
412static PyObject *
413method_get_text_signature(PyMethodDescrObject *descr, void *closure)
414{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800415 return _PyType_GetTextSignatureFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000416}
417
Antoine Pitrou9d574812011-12-12 13:47:25 +0100418static PyObject *
419calculate_qualname(PyDescrObject *descr)
420{
421 PyObject *type_qualname, *res;
422 _Py_IDENTIFIER(__qualname__);
423
424 if (descr->d_name == NULL || !PyUnicode_Check(descr->d_name)) {
425 PyErr_SetString(PyExc_TypeError,
426 "<descriptor>.__name__ is not a unicode object");
427 return NULL;
428 }
429
430 type_qualname = _PyObject_GetAttrId((PyObject *)descr->d_type,
431 &PyId___qualname__);
432 if (type_qualname == NULL)
433 return NULL;
434
435 if (!PyUnicode_Check(type_qualname)) {
436 PyErr_SetString(PyExc_TypeError, "<descriptor>.__objclass__."
437 "__qualname__ is not a unicode object");
438 Py_XDECREF(type_qualname);
439 return NULL;
440 }
441
442 res = PyUnicode_FromFormat("%S.%S", type_qualname, descr->d_name);
443 Py_DECREF(type_qualname);
444 return res;
445}
446
447static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200448descr_get_qualname(PyDescrObject *descr, void *Py_UNUSED(ignored))
Antoine Pitrou9d574812011-12-12 13:47:25 +0100449{
450 if (descr->d_qualname == NULL)
451 descr->d_qualname = calculate_qualname(descr);
452 Py_XINCREF(descr->d_qualname);
453 return descr->d_qualname;
454}
455
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100456static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530457descr_reduce(PyDescrObject *descr, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100458{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100459 _Py_IDENTIFIER(getattr);
Serhiy Storchakabb86bf42018-12-11 08:28:18 +0200460 return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_getattr),
461 PyDescr_TYPE(descr), PyDescr_NAME(descr));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100462}
463
464static PyMethodDef descr_methods[] = {
465 {"__reduce__", (PyCFunction)descr_reduce, METH_NOARGS, NULL},
466 {NULL, NULL}
467};
468
Guido van Rossum6f799372001-09-20 20:46:19 +0000469static PyMemberDef descr_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
471 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
472 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000473};
474
Guido van Rossum32d34c82001-09-20 21:45:26 +0000475static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 {"__doc__", (getter)method_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100477 {"__qualname__", (getter)descr_get_qualname},
Larry Hastings5c661892014-01-24 06:17:25 -0800478 {"__text_signature__", (getter)method_get_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 {0}
Guido van Rossum6f799372001-09-20 20:46:19 +0000480};
481
482static PyObject *
483member_get_doc(PyMemberDescrObject *descr, void *closure)
484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (descr->d_member->doc == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200486 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 }
488 return PyUnicode_FromString(descr->d_member->doc);
Guido van Rossum6f799372001-09-20 20:46:19 +0000489}
490
Guido van Rossum32d34c82001-09-20 21:45:26 +0000491static PyGetSetDef member_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 {"__doc__", (getter)member_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100493 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000495};
496
497static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000498getset_get_doc(PyGetSetDescrObject *descr, void *closure)
499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (descr->d_getset->doc == NULL) {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200501 Py_RETURN_NONE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 }
503 return PyUnicode_FromString(descr->d_getset->doc);
Guido van Rossum32d34c82001-09-20 21:45:26 +0000504}
505
506static PyGetSetDef getset_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 {"__doc__", (getter)getset_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100508 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 {0}
Guido van Rossum32d34c82001-09-20 21:45:26 +0000510};
511
512static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000513wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000514{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800515 return _PyType_GetDocFromInternalDoc(descr->d_base->name, descr->d_base->doc);
Larry Hastings5c661892014-01-24 06:17:25 -0800516}
517
518static PyObject *
519wrapperdescr_get_text_signature(PyWrapperDescrObject *descr, void *closure)
520{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800521 return _PyType_GetTextSignatureFromInternalDoc(descr->d_base->name, descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000522}
523
Armin Rigoc6686b72005-11-07 08:38:00 +0000524static PyGetSetDef wrapperdescr_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 {"__doc__", (getter)wrapperdescr_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100526 {"__qualname__", (getter)descr_get_qualname},
Larry Hastings5c661892014-01-24 06:17:25 -0800527 {"__text_signature__", (getter)wrapperdescr_get_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000529};
530
Guido van Rossum048eb752001-10-02 21:24:57 +0000531static int
532descr_traverse(PyObject *self, visitproc visit, void *arg)
533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 PyDescrObject *descr = (PyDescrObject *)self;
535 Py_VISIT(descr->d_type);
536 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000537}
538
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000539PyTypeObject PyMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 PyVarObject_HEAD_INIT(&PyType_Type, 0)
541 "method_descriptor",
542 sizeof(PyMethodDescrObject),
543 0,
544 (destructor)descr_dealloc, /* tp_dealloc */
545 0, /* tp_print */
546 0, /* tp_getattr */
547 0, /* tp_setattr */
548 0, /* tp_reserved */
549 (reprfunc)method_repr, /* tp_repr */
550 0, /* tp_as_number */
551 0, /* tp_as_sequence */
552 0, /* tp_as_mapping */
553 0, /* tp_hash */
554 (ternaryfunc)methoddescr_call, /* tp_call */
555 0, /* tp_str */
556 PyObject_GenericGetAttr, /* tp_getattro */
557 0, /* tp_setattro */
558 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200559 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
560 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 0, /* tp_doc */
562 descr_traverse, /* tp_traverse */
563 0, /* tp_clear */
564 0, /* tp_richcompare */
565 0, /* tp_weaklistoffset */
566 0, /* tp_iter */
567 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100568 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 descr_members, /* tp_members */
570 method_getset, /* tp_getset */
571 0, /* tp_base */
572 0, /* tp_dict */
573 (descrgetfunc)method_get, /* tp_descr_get */
574 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000575};
576
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000577/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000578PyTypeObject PyClassMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 PyVarObject_HEAD_INIT(&PyType_Type, 0)
580 "classmethod_descriptor",
581 sizeof(PyMethodDescrObject),
582 0,
583 (destructor)descr_dealloc, /* tp_dealloc */
584 0, /* tp_print */
585 0, /* tp_getattr */
586 0, /* tp_setattr */
587 0, /* tp_reserved */
588 (reprfunc)method_repr, /* tp_repr */
589 0, /* tp_as_number */
590 0, /* tp_as_sequence */
591 0, /* tp_as_mapping */
592 0, /* tp_hash */
593 (ternaryfunc)classmethoddescr_call, /* tp_call */
594 0, /* tp_str */
595 PyObject_GenericGetAttr, /* tp_getattro */
596 0, /* tp_setattro */
597 0, /* tp_as_buffer */
598 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
599 0, /* tp_doc */
600 descr_traverse, /* tp_traverse */
601 0, /* tp_clear */
602 0, /* tp_richcompare */
603 0, /* tp_weaklistoffset */
604 0, /* tp_iter */
605 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100606 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 descr_members, /* tp_members */
608 method_getset, /* tp_getset */
609 0, /* tp_base */
610 0, /* tp_dict */
611 (descrgetfunc)classmethod_get, /* tp_descr_get */
612 0, /* tp_descr_set */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000613};
614
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000615PyTypeObject PyMemberDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 PyVarObject_HEAD_INIT(&PyType_Type, 0)
617 "member_descriptor",
618 sizeof(PyMemberDescrObject),
619 0,
620 (destructor)descr_dealloc, /* tp_dealloc */
621 0, /* tp_print */
622 0, /* tp_getattr */
623 0, /* tp_setattr */
624 0, /* tp_reserved */
625 (reprfunc)member_repr, /* tp_repr */
626 0, /* tp_as_number */
627 0, /* tp_as_sequence */
628 0, /* tp_as_mapping */
629 0, /* tp_hash */
630 0, /* tp_call */
631 0, /* tp_str */
632 PyObject_GenericGetAttr, /* tp_getattro */
633 0, /* tp_setattro */
634 0, /* tp_as_buffer */
635 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
636 0, /* tp_doc */
637 descr_traverse, /* tp_traverse */
638 0, /* tp_clear */
639 0, /* tp_richcompare */
640 0, /* tp_weaklistoffset */
641 0, /* tp_iter */
642 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100643 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 descr_members, /* tp_members */
645 member_getset, /* tp_getset */
646 0, /* tp_base */
647 0, /* tp_dict */
648 (descrgetfunc)member_get, /* tp_descr_get */
649 (descrsetfunc)member_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650};
651
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000652PyTypeObject PyGetSetDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 PyVarObject_HEAD_INIT(&PyType_Type, 0)
654 "getset_descriptor",
655 sizeof(PyGetSetDescrObject),
656 0,
657 (destructor)descr_dealloc, /* tp_dealloc */
658 0, /* tp_print */
659 0, /* tp_getattr */
660 0, /* tp_setattr */
661 0, /* tp_reserved */
662 (reprfunc)getset_repr, /* tp_repr */
663 0, /* tp_as_number */
664 0, /* tp_as_sequence */
665 0, /* tp_as_mapping */
666 0, /* tp_hash */
667 0, /* tp_call */
668 0, /* tp_str */
669 PyObject_GenericGetAttr, /* tp_getattro */
670 0, /* tp_setattro */
671 0, /* tp_as_buffer */
672 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
673 0, /* tp_doc */
674 descr_traverse, /* tp_traverse */
675 0, /* tp_clear */
676 0, /* tp_richcompare */
677 0, /* tp_weaklistoffset */
678 0, /* tp_iter */
679 0, /* tp_iternext */
680 0, /* tp_methods */
681 descr_members, /* tp_members */
682 getset_getset, /* tp_getset */
683 0, /* tp_base */
684 0, /* tp_dict */
685 (descrgetfunc)getset_get, /* tp_descr_get */
686 (descrsetfunc)getset_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000687};
688
Guido van Rossumf4593e02001-10-03 12:09:30 +0000689PyTypeObject PyWrapperDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 PyVarObject_HEAD_INIT(&PyType_Type, 0)
691 "wrapper_descriptor",
692 sizeof(PyWrapperDescrObject),
693 0,
694 (destructor)descr_dealloc, /* tp_dealloc */
695 0, /* tp_print */
696 0, /* tp_getattr */
697 0, /* tp_setattr */
698 0, /* tp_reserved */
699 (reprfunc)wrapperdescr_repr, /* tp_repr */
700 0, /* tp_as_number */
701 0, /* tp_as_sequence */
702 0, /* tp_as_mapping */
703 0, /* tp_hash */
704 (ternaryfunc)wrapperdescr_call, /* tp_call */
705 0, /* tp_str */
706 PyObject_GenericGetAttr, /* tp_getattro */
707 0, /* tp_setattro */
708 0, /* tp_as_buffer */
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200709 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
710 Py_TPFLAGS_METHOD_DESCRIPTOR, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 0, /* tp_doc */
712 descr_traverse, /* tp_traverse */
713 0, /* tp_clear */
714 0, /* tp_richcompare */
715 0, /* tp_weaklistoffset */
716 0, /* tp_iter */
717 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100718 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 descr_members, /* tp_members */
720 wrapperdescr_getset, /* tp_getset */
721 0, /* tp_base */
722 0, /* tp_dict */
723 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
724 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725};
726
727static PyDescrObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000728descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 PyDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
733 if (descr != NULL) {
734 Py_XINCREF(type);
735 descr->d_type = type;
736 descr->d_name = PyUnicode_InternFromString(name);
737 if (descr->d_name == NULL) {
738 Py_DECREF(descr);
739 descr = NULL;
740 }
Benjamin Petersonf2fe7f02011-12-17 08:02:20 -0500741 else {
742 descr->d_qualname = NULL;
743 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 }
745 return descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746}
747
748PyObject *
749PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyMethodDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
754 type, method->ml_name);
755 if (descr != NULL)
756 descr->d_method = method;
757 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000758}
759
760PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000761PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 PyMethodDescrObject *descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
766 type, method->ml_name);
767 if (descr != NULL)
768 descr->d_method = method;
769 return (PyObject *)descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000770}
771
772PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000773PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 PyMemberDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
778 type, member->name);
779 if (descr != NULL)
780 descr->d_member = member;
781 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000782}
783
784PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000785PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyGetSetDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
790 type, getset->name);
791 if (descr != NULL)
792 descr->d_getset = getset;
793 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000794}
795
796PyObject *
797PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
802 type, base->name);
803 if (descr != NULL) {
804 descr->d_base = base;
805 descr->d_wrapped = wrapped;
806 }
807 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000808}
809
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810
Victor Stinner0db176f2012-04-16 00:16:30 +0200811/* --- mappingproxy: read-only proxy for mappings --- */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000812
813/* This has no reason to be in this file except that adding new files is a
814 bit of a pain */
815
816typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 PyObject_HEAD
Victor Stinner0db176f2012-04-16 00:16:30 +0200818 PyObject *mapping;
819} mappingproxyobject;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000820
Martin v. Löwis18e16552006-02-15 17:27:45 +0000821static Py_ssize_t
Victor Stinner0db176f2012-04-16 00:16:30 +0200822mappingproxy_len(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000823{
Victor Stinner0db176f2012-04-16 00:16:30 +0200824 return PyObject_Size(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000825}
826
827static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200828mappingproxy_getitem(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000829{
Victor Stinner0db176f2012-04-16 00:16:30 +0200830 return PyObject_GetItem(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000831}
832
Victor Stinner0db176f2012-04-16 00:16:30 +0200833static PyMappingMethods mappingproxy_as_mapping = {
834 (lenfunc)mappingproxy_len, /* mp_length */
835 (binaryfunc)mappingproxy_getitem, /* mp_subscript */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 0, /* mp_ass_subscript */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000837};
838
839static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200840mappingproxy_contains(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000841{
Victor Stinner0db176f2012-04-16 00:16:30 +0200842 if (PyDict_CheckExact(pp->mapping))
843 return PyDict_Contains(pp->mapping, key);
844 else
845 return PySequence_Contains(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000846}
847
Victor Stinner0db176f2012-04-16 00:16:30 +0200848static PySequenceMethods mappingproxy_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 0, /* sq_length */
850 0, /* sq_concat */
851 0, /* sq_repeat */
852 0, /* sq_item */
853 0, /* sq_slice */
854 0, /* sq_ass_item */
855 0, /* sq_ass_slice */
Victor Stinner0db176f2012-04-16 00:16:30 +0200856 (objobjproc)mappingproxy_contains, /* sq_contains */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 0, /* sq_inplace_concat */
858 0, /* sq_inplace_repeat */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000859};
860
861static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200862mappingproxy_get(mappingproxyobject *pp, PyObject *args)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 PyObject *key, *def = Py_None;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200865 _Py_IDENTIFIER(get);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
868 return NULL;
Victor Stinner7e425412016-12-09 00:36:19 +0100869 return _PyObject_CallMethodIdObjArgs(pp->mapping, &PyId_get,
870 key, def, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000871}
872
873static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530874mappingproxy_keys(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000875{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200876 _Py_IDENTIFIER(keys);
Victor Stinner0db176f2012-04-16 00:16:30 +0200877 return _PyObject_CallMethodId(pp->mapping, &PyId_keys, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000878}
879
880static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530881mappingproxy_values(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200883 _Py_IDENTIFIER(values);
Victor Stinner0db176f2012-04-16 00:16:30 +0200884 return _PyObject_CallMethodId(pp->mapping, &PyId_values, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000885}
886
887static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530888mappingproxy_items(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000889{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200890 _Py_IDENTIFIER(items);
Victor Stinner0db176f2012-04-16 00:16:30 +0200891 return _PyObject_CallMethodId(pp->mapping, &PyId_items, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000892}
893
894static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530895mappingproxy_copy(mappingproxyobject *pp, PyObject *Py_UNUSED(ignored))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200897 _Py_IDENTIFIER(copy);
Victor Stinner0db176f2012-04-16 00:16:30 +0200898 return _PyObject_CallMethodId(pp->mapping, &PyId_copy, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000899}
900
Victor Stinner0db176f2012-04-16 00:16:30 +0200901/* WARNING: mappingproxy methods must not give access
902 to the underlying mapping */
903
904static PyMethodDef mappingproxy_methods[] = {
905 {"get", (PyCFunction)mappingproxy_get, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
Victor Stinner0db176f2012-04-16 00:16:30 +0200907 " d defaults to None.")},
908 {"keys", (PyCFunction)mappingproxy_keys, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 PyDoc_STR("D.keys() -> list of D's keys")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200910 {"values", (PyCFunction)mappingproxy_values, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 PyDoc_STR("D.values() -> list of D's values")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200912 {"items", (PyCFunction)mappingproxy_items, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200914 {"copy", (PyCFunction)mappingproxy_copy, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 PyDoc_STR("D.copy() -> a shallow copy of D")},
916 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000917};
918
919static void
Victor Stinner0db176f2012-04-16 00:16:30 +0200920mappingproxy_dealloc(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 _PyObject_GC_UNTRACK(pp);
Victor Stinner0db176f2012-04-16 00:16:30 +0200923 Py_DECREF(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000925}
926
927static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200928mappingproxy_getiter(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000929{
Victor Stinner0db176f2012-04-16 00:16:30 +0200930 return PyObject_GetIter(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000931}
932
Neil Schemenauer26775122001-10-21 22:26:43 +0000933static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200934mappingproxy_str(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000935{
Victor Stinner0db176f2012-04-16 00:16:30 +0200936 return PyObject_Str(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000937}
938
Ezio Melottiac53ab62010-12-18 14:59:43 +0000939static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200940mappingproxy_repr(mappingproxyobject *pp)
Ezio Melottiac53ab62010-12-18 14:59:43 +0000941{
Victor Stinner0db176f2012-04-16 00:16:30 +0200942 return PyUnicode_FromFormat("mappingproxy(%R)", pp->mapping);
Ezio Melottiac53ab62010-12-18 14:59:43 +0000943}
944
Guido van Rossum048eb752001-10-02 21:24:57 +0000945static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200946mappingproxy_traverse(PyObject *self, visitproc visit, void *arg)
Guido van Rossum048eb752001-10-02 21:24:57 +0000947{
Victor Stinner0db176f2012-04-16 00:16:30 +0200948 mappingproxyobject *pp = (mappingproxyobject *)self;
949 Py_VISIT(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000951}
952
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000953static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200954mappingproxy_richcompare(mappingproxyobject *v, PyObject *w, int op)
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000955{
Victor Stinner0db176f2012-04-16 00:16:30 +0200956 return PyObject_RichCompare(v->mapping, w, op);
957}
958
959static int
960mappingproxy_check_mapping(PyObject *mapping)
961{
962 if (!PyMapping_Check(mapping)
963 || PyList_Check(mapping)
964 || PyTuple_Check(mapping)) {
965 PyErr_Format(PyExc_TypeError,
966 "mappingproxy() argument must be a mapping, not %s",
967 Py_TYPE(mapping)->tp_name);
968 return -1;
969 }
970 return 0;
971}
972
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200973/*[clinic input]
974@classmethod
975mappingproxy.__new__ as mappingproxy_new
Victor Stinner0db176f2012-04-16 00:16:30 +0200976
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200977 mapping: object
978
979[clinic start generated code]*/
980
981static PyObject *
982mappingproxy_new_impl(PyTypeObject *type, PyObject *mapping)
983/*[clinic end generated code: output=65f27f02d5b68fa7 input=d2d620d4f598d4f8]*/
984{
985 mappingproxyobject *mappingproxy;
Victor Stinner0db176f2012-04-16 00:16:30 +0200986
987 if (mappingproxy_check_mapping(mapping) == -1)
988 return NULL;
989
990 mappingproxy = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
991 if (mappingproxy == NULL)
992 return NULL;
993 Py_INCREF(mapping);
994 mappingproxy->mapping = mapping;
995 _PyObject_GC_TRACK(mappingproxy);
996 return (PyObject *)mappingproxy;
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000997}
998
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +02001000PyDictProxy_New(PyObject *mapping)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001001{
Victor Stinner0db176f2012-04-16 00:16:30 +02001002 mappingproxyobject *pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003
Victor Stinner0db176f2012-04-16 00:16:30 +02001004 if (mappingproxy_check_mapping(mapping) == -1)
1005 return NULL;
1006
1007 pp = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 if (pp != NULL) {
Victor Stinner0db176f2012-04-16 00:16:30 +02001009 Py_INCREF(mapping);
1010 pp->mapping = mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 _PyObject_GC_TRACK(pp);
1012 }
1013 return (PyObject *)pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014}
1015
1016
1017/* --- Wrapper object for "slot" methods --- */
1018
1019/* This has no reason to be in this file except that adding new files is a
1020 bit of a pain */
1021
1022typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyObject_HEAD
1024 PyWrapperDescrObject *descr;
1025 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001026} wrapperobject;
1027
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001028#define Wrapper_Check(v) (Py_TYPE(v) == &_PyMethodWrapper_Type)
Mark Dickinson211c6252009-02-01 10:28:51 +00001029
Tim Peters6d6c1a32001-08-02 04:15:00 +00001030static void
1031wrapper_dealloc(wrapperobject *wp)
1032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 PyObject_GC_UnTrack(wp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02001034 Py_TRASHCAN_BEGIN(wp, wrapper_dealloc)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 Py_XDECREF(wp->descr);
1036 Py_XDECREF(wp->self);
1037 PyObject_GC_Del(wp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02001038 Py_TRASHCAN_END
Tim Peters6d6c1a32001-08-02 04:15:00 +00001039}
1040
Mark Dickinson211c6252009-02-01 10:28:51 +00001041static PyObject *
1042wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +00001043{
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03001044 wrapperobject *wa, *wb;
1045 int eq;
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 */
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03001050 if ((op != Py_EQ && op != Py_NE)
1051 || !Wrapper_Check(a) || !Wrapper_Check(b))
1052 {
stratakise8b19652017-11-02 11:32:54 +01001053 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001055
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03001056 wa = (wrapperobject *)a;
1057 wb = (wrapperobject *)b;
1058 eq = (wa->descr == wb->descr && wa->self == wb->self);
1059 if (eq == (op == Py_EQ)) {
1060 Py_RETURN_TRUE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 }
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03001062 else {
1063 Py_RETURN_FALSE;
1064 }
Armin Rigoc6686b72005-11-07 08:38:00 +00001065}
1066
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001067static Py_hash_t
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001068wrapper_hash(wrapperobject *wp)
1069{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001070 Py_hash_t x, y;
Serhiy Storchakaac20e0f2018-07-31 09:18:24 +03001071 x = _Py_HashPointer(wp->self);
1072 y = _Py_HashPointer(wp->descr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 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 *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301089wrapper_reduce(wrapperobject *wp, PyObject *Py_UNUSED(ignored))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001090{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001091 _Py_IDENTIFIER(getattr);
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02001092 return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_getattr),
1093 wp->self, PyDescr_NAME(wp->descr));
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001094}
1095
1096static PyMethodDef wrapper_methods[] = {
1097 {"__reduce__", (PyCFunction)wrapper_reduce, METH_NOARGS, NULL},
1098 {NULL, NULL}
1099};
1100
Armin Rigoc6686b72005-11-07 08:38:00 +00001101static PyMemberDef wrapper_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
1103 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001104};
1105
1106static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001107wrapper_objclass(wrapperobject *wp, void *Py_UNUSED(ignored))
Armin Rigoc6686b72005-11-07 08:38:00 +00001108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 PyObject *c = (PyObject *)PyDescr_TYPE(wp->descr);
Armin Rigoc6686b72005-11-07 08:38:00 +00001110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 Py_INCREF(c);
1112 return c;
Armin Rigoc6686b72005-11-07 08:38:00 +00001113}
1114
1115static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001116wrapper_name(wrapperobject *wp, void *Py_UNUSED(ignored))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001121}
1122
1123static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001124wrapper_doc(wrapperobject *wp, void *Py_UNUSED(ignored))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001125{
Larry Hastings2623c8c2014-02-08 22:15:29 -08001126 return _PyType_GetDocFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
Larry Hastings5c661892014-01-24 06:17:25 -08001127}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001128
Larry Hastings5c661892014-01-24 06:17:25 -08001129static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001130wrapper_text_signature(wrapperobject *wp, void *Py_UNUSED(ignored))
Larry Hastings5c661892014-01-24 06:17:25 -08001131{
Larry Hastings2623c8c2014-02-08 22:15:29 -08001132 return _PyType_GetTextSignatureFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001133}
1134
Antoine Pitrou9d574812011-12-12 13:47:25 +01001135static PyObject *
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001136wrapper_qualname(wrapperobject *wp, void *Py_UNUSED(ignored))
Antoine Pitrou9d574812011-12-12 13:47:25 +01001137{
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001138 return descr_get_qualname((PyDescrObject *)wp->descr, NULL);
Antoine Pitrou9d574812011-12-12 13:47:25 +01001139}
1140
Guido van Rossum32d34c82001-09-20 21:45:26 +00001141static PyGetSetDef wrapper_getsets[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 {"__objclass__", (getter)wrapper_objclass},
1143 {"__name__", (getter)wrapper_name},
Antoine Pitrou9d574812011-12-12 13:47:25 +01001144 {"__qualname__", (getter)wrapper_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 {"__doc__", (getter)wrapper_doc},
Larry Hastings5c661892014-01-24 06:17:25 -08001146 {"__text_signature__", (getter)wrapper_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001148};
1149
1150static PyObject *
1151wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
1152{
Serhiy Storchaka5e02c782017-09-21 14:25:36 +03001153 return wrapperdescr_raw_call(wp->descr, wp->self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001154}
1155
Guido van Rossum048eb752001-10-02 21:24:57 +00001156static int
1157wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 wrapperobject *wp = (wrapperobject *)self;
1160 Py_VISIT(wp->descr);
1161 Py_VISIT(wp->self);
1162 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001163}
1164
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001165PyTypeObject _PyMethodWrapper_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1167 "method-wrapper", /* tp_name */
1168 sizeof(wrapperobject), /* tp_basicsize */
1169 0, /* tp_itemsize */
1170 /* methods */
1171 (destructor)wrapper_dealloc, /* tp_dealloc */
1172 0, /* tp_print */
1173 0, /* tp_getattr */
1174 0, /* tp_setattr */
1175 0, /* tp_reserved */
1176 (reprfunc)wrapper_repr, /* tp_repr */
1177 0, /* tp_as_number */
1178 0, /* tp_as_sequence */
1179 0, /* tp_as_mapping */
1180 (hashfunc)wrapper_hash, /* tp_hash */
1181 (ternaryfunc)wrapper_call, /* tp_call */
1182 0, /* tp_str */
1183 PyObject_GenericGetAttr, /* tp_getattro */
1184 0, /* tp_setattro */
1185 0, /* tp_as_buffer */
1186 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1187 0, /* tp_doc */
1188 wrapper_traverse, /* tp_traverse */
1189 0, /* tp_clear */
1190 wrapper_richcompare, /* tp_richcompare */
1191 0, /* tp_weaklistoffset */
1192 0, /* tp_iter */
1193 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001194 wrapper_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 wrapper_members, /* tp_members */
1196 wrapper_getsets, /* tp_getset */
1197 0, /* tp_base */
1198 0, /* tp_dict */
1199 0, /* tp_descr_get */
1200 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001201};
1202
1203PyObject *
1204PyWrapper_New(PyObject *d, PyObject *self)
1205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 wrapperobject *wp;
1207 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1210 descr = (PyWrapperDescrObject *)d;
Victor Stinner3249dec2011-05-01 23:19:15 +02001211 assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +02001212 (PyObject *)PyDescr_TYPE(descr)));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001213
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001214 wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 if (wp != NULL) {
1216 Py_INCREF(descr);
1217 wp->descr = descr;
1218 Py_INCREF(self);
1219 wp->self = self;
1220 _PyObject_GC_TRACK(wp);
1221 }
1222 return (PyObject *)wp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001223}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001224
1225
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001226/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001227
1228/*
Serhiy Storchakad741a882015-06-11 00:06:39 +03001229class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1232 if doc is None and fget is not None and hasattr(fget, "__doc__"):
Serhiy Storchakad741a882015-06-11 00:06:39 +03001233 doc = fget.__doc__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 self.__get = fget
1235 self.__set = fset
1236 self.__del = fdel
1237 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 def __get__(self, inst, type=None):
1240 if inst is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001241 return self
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 if self.__get is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001243 raise AttributeError, "unreadable attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 def __set__(self, inst, value):
1247 if self.__set is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001248 raise AttributeError, "can't set attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 return self.__set(inst, value)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 def __delete__(self, inst):
1252 if self.__del is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001253 raise AttributeError, "can't delete attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 return self.__del(inst)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001255
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001256*/
1257
1258typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 PyObject_HEAD
1260 PyObject *prop_get;
1261 PyObject *prop_set;
1262 PyObject *prop_del;
1263 PyObject *prop_doc;
1264 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001265} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001266
Christian Heimes0449f632007-12-15 01:27:15 +00001267static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
Benjamin Peterson93964832010-06-28 03:07:10 +00001268 PyObject *);
Christian Heimes0449f632007-12-15 01:27:15 +00001269
Tim Peters66c1a522001-09-24 21:17:50 +00001270static PyMemberDef property_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1272 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1273 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
Raymond Hettingereac503a2015-05-13 01:09:59 -07001274 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 {0}
Tim Peters66c1a522001-09-24 21:17:50 +00001276};
1277
Christian Heimes0449f632007-12-15 01:27:15 +00001278
Guido van Rossum58da9312007-11-10 23:39:45 +00001279PyDoc_STRVAR(getter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 "Descriptor to change the getter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001281
Neal Norwitz32dde222008-04-15 06:43:13 +00001282static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001283property_getter(PyObject *self, PyObject *getter)
1284{
Benjamin Peterson93964832010-06-28 03:07:10 +00001285 return property_copy(self, getter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001286}
1287
Christian Heimes0449f632007-12-15 01:27:15 +00001288
Guido van Rossum58da9312007-11-10 23:39:45 +00001289PyDoc_STRVAR(setter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001291
Neal Norwitz32dde222008-04-15 06:43:13 +00001292static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001293property_setter(PyObject *self, PyObject *setter)
1294{
Benjamin Peterson93964832010-06-28 03:07:10 +00001295 return property_copy(self, NULL, setter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001296}
1297
Christian Heimes0449f632007-12-15 01:27:15 +00001298
Guido van Rossum58da9312007-11-10 23:39:45 +00001299PyDoc_STRVAR(deleter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 "Descriptor to change the deleter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001301
Neal Norwitz32dde222008-04-15 06:43:13 +00001302static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001303property_deleter(PyObject *self, PyObject *deleter)
1304{
Benjamin Peterson93964832010-06-28 03:07:10 +00001305 return property_copy(self, NULL, NULL, deleter);
Guido van Rossum58da9312007-11-10 23:39:45 +00001306}
1307
1308
Guido van Rossum58da9312007-11-10 23:39:45 +00001309static PyMethodDef property_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 {"getter", property_getter, METH_O, getter_doc},
1311 {"setter", property_setter, METH_O, setter_doc},
1312 {"deleter", property_deleter, METH_O, deleter_doc},
1313 {0}
Guido van Rossum58da9312007-11-10 23:39:45 +00001314};
1315
Tim Peters66c1a522001-09-24 21:17:50 +00001316
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001317static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001318property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 _PyObject_GC_UNTRACK(self);
1323 Py_XDECREF(gs->prop_get);
1324 Py_XDECREF(gs->prop_set);
1325 Py_XDECREF(gs->prop_del);
1326 Py_XDECREF(gs->prop_doc);
1327 self->ob_type->tp_free(self);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001328}
1329
1330static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001331property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (obj == NULL || obj == Py_None) {
1334 Py_INCREF(self);
1335 return self;
1336 }
Victor Stinnere972c132018-10-01 03:03:22 -07001337
1338 propertyobject *gs = (propertyobject *)self;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (gs->prop_get == NULL) {
1340 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1341 return NULL;
1342 }
Victor Stinnere972c132018-10-01 03:03:22 -07001343
1344 PyObject *args[1] = {obj};
1345 return _PyObject_FastCall(gs->prop_get, args, 1);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001346}
1347
1348static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001349property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 propertyobject *gs = (propertyobject *)self;
1352 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (value == NULL)
1355 func = gs->prop_del;
1356 else
1357 func = gs->prop_set;
1358 if (func == NULL) {
1359 PyErr_SetString(PyExc_AttributeError,
1360 value == NULL ?
1361 "can't delete attribute" :
1362 "can't set attribute");
1363 return -1;
1364 }
1365 if (value == NULL)
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001366 res = PyObject_CallFunctionObjArgs(func, obj, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 else
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001368 res = PyObject_CallFunctionObjArgs(func, obj, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (res == NULL)
1370 return -1;
1371 Py_DECREF(res);
1372 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001373}
1374
Christian Heimes0449f632007-12-15 01:27:15 +00001375static PyObject *
Benjamin Peterson93964832010-06-28 03:07:10 +00001376property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
Christian Heimes0449f632007-12-15 01:27:15 +00001377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 propertyobject *pold = (propertyobject *)old;
Benjamin Peterson93964832010-06-28 03:07:10 +00001379 PyObject *new, *type, *doc;
Christian Heimes0449f632007-12-15 01:27:15 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 type = PyObject_Type(old);
1382 if (type == NULL)
1383 return NULL;
Christian Heimes0449f632007-12-15 01:27:15 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 if (get == NULL || get == Py_None) {
1386 Py_XDECREF(get);
1387 get = pold->prop_get ? pold->prop_get : Py_None;
1388 }
1389 if (set == NULL || set == Py_None) {
1390 Py_XDECREF(set);
1391 set = pold->prop_set ? pold->prop_set : Py_None;
1392 }
1393 if (del == NULL || del == Py_None) {
1394 Py_XDECREF(del);
1395 del = pold->prop_del ? pold->prop_del : Py_None;
1396 }
Benjamin Peterson93964832010-06-28 03:07:10 +00001397 if (pold->getter_doc && get != Py_None) {
1398 /* make _init use __doc__ from getter */
1399 doc = Py_None;
1400 }
1401 else {
1402 doc = pold->prop_doc ? pold->prop_doc : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 }
R. David Murrayb18500d2009-05-04 22:59:07 +00001404
Victor Stinner5abaa2b2016-12-09 16:22:32 +01001405 new = PyObject_CallFunctionObjArgs(type, get, set, del, doc, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 Py_DECREF(type);
1407 if (new == NULL)
1408 return NULL;
1409 return new;
Christian Heimes0449f632007-12-15 01:27:15 +00001410}
1411
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001412/*[clinic input]
1413property.__init__ as property_init
1414
1415 fget: object(c_default="NULL") = None
1416 function to be used for getting an attribute value
1417 fset: object(c_default="NULL") = None
1418 function to be used for setting an attribute value
1419 fdel: object(c_default="NULL") = None
1420 function to be used for del'ing an attribute
1421 doc: object(c_default="NULL") = None
1422 docstring
1423
1424Property attribute.
1425
1426Typical use is to define a managed attribute x:
1427
1428class C(object):
1429 def getx(self): return self._x
1430 def setx(self, value): self._x = value
1431 def delx(self): del self._x
1432 x = property(getx, setx, delx, "I'm the 'x' property.")
1433
1434Decorators make defining new properties or modifying existing ones easy:
1435
1436class C(object):
1437 @property
1438 def x(self):
1439 "I am the 'x' property."
1440 return self._x
1441 @x.setter
1442 def x(self, value):
1443 self._x = value
1444 @x.deleter
1445 def x(self):
1446 del self._x
1447[clinic start generated code]*/
1448
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001449static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001450property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
1451 PyObject *fdel, PyObject *doc)
1452/*[clinic end generated code: output=01a960742b692b57 input=dfb5dbbffc6932d5]*/
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001453{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001454 if (fget == Py_None)
1455 fget = NULL;
1456 if (fset == Py_None)
1457 fset = NULL;
1458 if (fdel == Py_None)
1459 fdel = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001460
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001461 Py_XINCREF(fget);
1462 Py_XINCREF(fset);
1463 Py_XINCREF(fdel);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 Py_XINCREF(doc);
Christian Heimes0449f632007-12-15 01:27:15 +00001465
Oren Milmand019bc82018-02-13 12:28:33 +02001466 Py_XSETREF(self->prop_get, fget);
1467 Py_XSETREF(self->prop_set, fset);
1468 Py_XSETREF(self->prop_del, fdel);
1469 Py_XSETREF(self->prop_doc, doc);
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001470 self->getter_doc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 /* if no docstring given and the getter has one, use that one */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001473 if ((doc == NULL || doc == Py_None) && fget != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001474 _Py_IDENTIFIER(__doc__);
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001475 PyObject *get_doc = _PyObject_GetAttrId(fget, &PyId___doc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (get_doc) {
1477 if (Py_TYPE(self) == &PyProperty_Type) {
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001478 Py_XSETREF(self->prop_doc, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 }
1480 else {
1481 /* If this is a property subclass, put __doc__
1482 in dict of the subclass instance instead,
1483 otherwise it gets shadowed by __doc__ in the
1484 class's dict. */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001485 int err = _PyObject_SetAttrId((PyObject *)self, &PyId___doc__, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 Py_DECREF(get_doc);
1487 if (err < 0)
1488 return -1;
1489 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001490 self->getter_doc = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 }
1492 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1493 PyErr_Clear();
1494 }
1495 else {
1496 return -1;
1497 }
1498 }
1499
1500 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001501}
1502
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001503static PyObject *
1504property_get___isabstractmethod__(propertyobject *prop, void *closure)
1505{
1506 int res = _PyObject_IsAbstract(prop->prop_get);
1507 if (res == -1) {
1508 return NULL;
1509 }
1510 else if (res) {
1511 Py_RETURN_TRUE;
1512 }
1513
1514 res = _PyObject_IsAbstract(prop->prop_set);
1515 if (res == -1) {
1516 return NULL;
1517 }
1518 else if (res) {
1519 Py_RETURN_TRUE;
1520 }
1521
1522 res = _PyObject_IsAbstract(prop->prop_del);
1523 if (res == -1) {
1524 return NULL;
1525 }
1526 else if (res) {
1527 Py_RETURN_TRUE;
1528 }
1529 Py_RETURN_FALSE;
1530}
1531
1532static PyGetSetDef property_getsetlist[] = {
1533 {"__isabstractmethod__",
1534 (getter)property_get___isabstractmethod__, NULL,
1535 NULL,
1536 NULL},
1537 {NULL} /* Sentinel */
1538};
1539
Guido van Rossum048eb752001-10-02 21:24:57 +00001540static int
1541property_traverse(PyObject *self, visitproc visit, void *arg)
1542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 propertyobject *pp = (propertyobject *)self;
1544 Py_VISIT(pp->prop_get);
1545 Py_VISIT(pp->prop_set);
1546 Py_VISIT(pp->prop_del);
1547 Py_VISIT(pp->prop_doc);
1548 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001549}
1550
Raymond Hettingerd4be6912015-05-13 11:12:33 -07001551static int
1552property_clear(PyObject *self)
1553{
1554 propertyobject *pp = (propertyobject *)self;
1555 Py_CLEAR(pp->prop_doc);
1556 return 0;
1557}
1558
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001559#include "clinic/descrobject.c.h"
1560
1561PyTypeObject PyDictProxy_Type = {
1562 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1563 "mappingproxy", /* tp_name */
1564 sizeof(mappingproxyobject), /* tp_basicsize */
1565 0, /* tp_itemsize */
1566 /* methods */
1567 (destructor)mappingproxy_dealloc, /* tp_dealloc */
1568 0, /* tp_print */
1569 0, /* tp_getattr */
1570 0, /* tp_setattr */
1571 0, /* tp_reserved */
1572 (reprfunc)mappingproxy_repr, /* tp_repr */
1573 0, /* tp_as_number */
1574 &mappingproxy_as_sequence, /* tp_as_sequence */
1575 &mappingproxy_as_mapping, /* tp_as_mapping */
1576 0, /* tp_hash */
1577 0, /* tp_call */
1578 (reprfunc)mappingproxy_str, /* tp_str */
1579 PyObject_GenericGetAttr, /* tp_getattro */
1580 0, /* tp_setattro */
1581 0, /* tp_as_buffer */
1582 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1583 0, /* tp_doc */
1584 mappingproxy_traverse, /* tp_traverse */
1585 0, /* tp_clear */
1586 (richcmpfunc)mappingproxy_richcompare, /* tp_richcompare */
1587 0, /* tp_weaklistoffset */
1588 (getiterfunc)mappingproxy_getiter, /* tp_iter */
1589 0, /* tp_iternext */
1590 mappingproxy_methods, /* tp_methods */
1591 0, /* tp_members */
1592 0, /* tp_getset */
1593 0, /* tp_base */
1594 0, /* tp_dict */
1595 0, /* tp_descr_get */
1596 0, /* tp_descr_set */
1597 0, /* tp_dictoffset */
1598 0, /* tp_init */
1599 0, /* tp_alloc */
1600 mappingproxy_new, /* tp_new */
1601};
1602
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001603PyTypeObject PyProperty_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1605 "property", /* tp_name */
1606 sizeof(propertyobject), /* tp_basicsize */
1607 0, /* tp_itemsize */
1608 /* methods */
1609 property_dealloc, /* tp_dealloc */
1610 0, /* tp_print */
1611 0, /* tp_getattr */
1612 0, /* tp_setattr */
1613 0, /* tp_reserved */
1614 0, /* tp_repr */
1615 0, /* tp_as_number */
1616 0, /* tp_as_sequence */
1617 0, /* tp_as_mapping */
1618 0, /* tp_hash */
1619 0, /* tp_call */
1620 0, /* tp_str */
1621 PyObject_GenericGetAttr, /* tp_getattro */
1622 0, /* tp_setattro */
1623 0, /* tp_as_buffer */
1624 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1625 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02001626 property_init__doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 property_traverse, /* tp_traverse */
Raymond Hettingerd4be6912015-05-13 11:12:33 -07001628 (inquiry)property_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 0, /* tp_richcompare */
1630 0, /* tp_weaklistoffset */
1631 0, /* tp_iter */
1632 0, /* tp_iternext */
1633 property_methods, /* tp_methods */
1634 property_members, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001635 property_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 0, /* tp_base */
1637 0, /* tp_dict */
1638 property_descr_get, /* tp_descr_get */
1639 property_descr_set, /* tp_descr_set */
1640 0, /* tp_dictoffset */
1641 property_init, /* tp_init */
1642 PyType_GenericAlloc, /* tp_alloc */
1643 PyType_GenericNew, /* tp_new */
1644 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001645};