blob: ac2752ea061688eb3145b1dc99866e04fd0970a3 [file] [log] [blame]
Tim Peters6d6c1a32001-08-02 04:15:00 +00001/* Descriptors -- a new, flexible way to describe attributes */
2
3#include "Python.h"
4#include "structmember.h" /* Why is this not included in Python.h? */
5
Tim Peters6d6c1a32001-08-02 04:15:00 +00006static void
7descr_dealloc(PyDescrObject *descr)
8{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00009 _PyObject_GC_UNTRACK(descr);
10 Py_XDECREF(descr->d_type);
11 Py_XDECREF(descr->d_name);
Antoine Pitrou9d574812011-12-12 13:47:25 +010012 Py_XDECREF(descr->d_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013 PyObject_GC_Del(descr);
Tim Peters6d6c1a32001-08-02 04:15:00 +000014}
15
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000016static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000017descr_name(PyDescrObject *descr)
18{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
20 return descr->d_name;
21 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000022}
23
24static PyObject *
25descr_repr(PyDescrObject *descr, char *format)
26{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 PyObject *name = NULL;
28 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
29 name = descr->d_name;
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 return PyUnicode_FromFormat(format, name, "?", descr->d_type->tp_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +000032}
33
34static PyObject *
35method_repr(PyMethodDescrObject *descr)
36{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 return descr_repr((PyDescrObject *)descr,
38 "<method '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000039}
40
41static PyObject *
42member_repr(PyMemberDescrObject *descr)
43{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 return descr_repr((PyDescrObject *)descr,
45 "<member '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000046}
47
48static PyObject *
49getset_repr(PyGetSetDescrObject *descr)
50{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 return descr_repr((PyDescrObject *)descr,
52 "<attribute '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000053}
54
55static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +000056wrapperdescr_repr(PyWrapperDescrObject *descr)
Tim Peters6d6c1a32001-08-02 04:15:00 +000057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 return descr_repr((PyDescrObject *)descr,
59 "<slot wrapper '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000060}
61
62static int
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000063descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +000064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (obj == NULL) {
66 Py_INCREF(descr);
67 *pres = (PyObject *)descr;
68 return 1;
69 }
70 if (!PyObject_TypeCheck(obj, descr->d_type)) {
71 PyErr_Format(PyExc_TypeError,
72 "descriptor '%V' for '%s' objects "
73 "doesn't apply to '%s' object",
74 descr_name((PyDescrObject *)descr), "?",
75 descr->d_type->tp_name,
76 obj->ob_type->tp_name);
77 *pres = NULL;
78 return 1;
79 }
80 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +000081}
82
83static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000084classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Petersbca1cbc2002-12-09 22:56:13 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 /* Ensure a valid type. Class methods ignore obj. */
87 if (type == NULL) {
88 if (obj != NULL)
89 type = (PyObject *)obj->ob_type;
90 else {
91 /* Wot - no type?! */
92 PyErr_Format(PyExc_TypeError,
93 "descriptor '%V' for type '%s' "
94 "needs either an object or a type",
95 descr_name((PyDescrObject *)descr), "?",
96 PyDescr_TYPE(descr)->tp_name);
97 return NULL;
98 }
99 }
100 if (!PyType_Check(type)) {
101 PyErr_Format(PyExc_TypeError,
102 "descriptor '%V' for type '%s' "
103 "needs a type, not a '%s' as arg 2",
104 descr_name((PyDescrObject *)descr), "?",
105 PyDescr_TYPE(descr)->tp_name,
106 type->ob_type->tp_name);
107 return NULL;
108 }
109 if (!PyType_IsSubtype((PyTypeObject *)type, PyDescr_TYPE(descr))) {
110 PyErr_Format(PyExc_TypeError,
111 "descriptor '%V' for type '%s' "
112 "doesn't apply to type '%s'",
113 descr_name((PyDescrObject *)descr), "?",
114 PyDescr_TYPE(descr)->tp_name,
115 ((PyTypeObject *)type)->tp_name);
116 return NULL;
117 }
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200118 return PyCFunction_NewEx(descr->d_method, type, NULL);
Tim Petersbca1cbc2002-12-09 22:56:13 +0000119}
120
121static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000122method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 if (descr_check((PyDescrObject *)descr, obj, &res))
127 return res;
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200128 return PyCFunction_NewEx(descr->d_method, obj, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000129}
130
131static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000132member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 if (descr_check((PyDescrObject *)descr, obj, &res))
137 return res;
138 return PyMember_GetOne((char *)obj, descr->d_member);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000139}
140
141static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000142getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (descr_check((PyDescrObject *)descr, obj, &res))
147 return res;
148 if (descr->d_getset->get != NULL)
149 return descr->d_getset->get(obj, descr->d_getset->closure);
150 PyErr_Format(PyExc_AttributeError,
151 "attribute '%V' of '%.100s' objects is not readable",
152 descr_name((PyDescrObject *)descr), "?",
153 PyDescr_TYPE(descr)->tp_name);
154 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000155}
156
157static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000158wrapperdescr_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 if (descr_check((PyDescrObject *)descr, obj, &res))
163 return res;
164 return PyWrapper_New((PyObject *)descr, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000165}
166
167static int
168descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 int *pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 assert(obj != NULL);
172 if (!PyObject_TypeCheck(obj, descr->d_type)) {
173 PyErr_Format(PyExc_TypeError,
174 "descriptor '%V' for '%.100s' objects "
175 "doesn't apply to '%.100s' object",
176 descr_name(descr), "?",
177 descr->d_type->tp_name,
178 obj->ob_type->tp_name);
179 *pres = -1;
180 return 1;
181 }
182 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183}
184
185static int
186member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
191 return res;
192 return PyMember_SetOne((char *)obj, descr->d_member, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000193}
194
195static int
196getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
201 return res;
202 if (descr->d_getset->set != NULL)
203 return descr->d_getset->set(obj, value,
204 descr->d_getset->closure);
205 PyErr_Format(PyExc_AttributeError,
206 "attribute '%V' of '%.100s' objects is not writable",
207 descr_name((PyDescrObject *)descr), "?",
208 PyDescr_TYPE(descr)->tp_name);
209 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000210}
211
212static PyObject *
213methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_ssize_t argc;
216 PyObject *self, *func, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 /* Make sure that the first argument is acceptable as 'self' */
219 assert(PyTuple_Check(args));
220 argc = PyTuple_GET_SIZE(args);
221 if (argc < 1) {
222 PyErr_Format(PyExc_TypeError,
223 "descriptor '%V' of '%.100s' "
224 "object needs an argument",
225 descr_name((PyDescrObject *)descr), "?",
226 PyDescr_TYPE(descr)->tp_name);
227 return NULL;
228 }
229 self = PyTuple_GET_ITEM(args, 0);
Victor Stinner3249dec2011-05-01 23:19:15 +0200230 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +0200231 (PyObject *)PyDescr_TYPE(descr))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 PyErr_Format(PyExc_TypeError,
233 "descriptor '%V' "
234 "requires a '%.100s' object "
235 "but received a '%.100s'",
236 descr_name((PyDescrObject *)descr), "?",
237 PyDescr_TYPE(descr)->tp_name,
238 self->ob_type->tp_name);
239 return NULL;
240 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000241
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200242 func = PyCFunction_NewEx(descr->d_method, self, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (func == NULL)
244 return NULL;
245 args = PyTuple_GetSlice(args, 1, argc);
246 if (args == NULL) {
247 Py_DECREF(func);
248 return NULL;
249 }
250 result = PyEval_CallObjectWithKeywords(func, args, kwds);
251 Py_DECREF(args);
252 Py_DECREF(func);
253 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000254}
255
256static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000257classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 PyObject *kwds)
Tim Petersbca1cbc2002-12-09 22:56:13 +0000259{
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400260 Py_ssize_t argc;
261 PyObject *self, *func, *result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000262
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400263 /* Make sure that the first argument is acceptable as 'self' */
264 assert(PyTuple_Check(args));
265 argc = PyTuple_GET_SIZE(args);
266 if (argc < 1) {
267 PyErr_Format(PyExc_TypeError,
268 "descriptor '%V' of '%.100s' "
269 "object needs an argument",
270 descr_name((PyDescrObject *)descr), "?",
271 PyDescr_TYPE(descr)->tp_name);
272 return NULL;
273 }
274 self = PyTuple_GET_ITEM(args, 0);
275 if (!PyType_Check(self)) {
276 PyErr_Format(PyExc_TypeError,
277 "descriptor '%V' requires a type "
278 "but received a '%.100s'",
279 descr_name((PyDescrObject *)descr), "?",
280 PyDescr_TYPE(descr)->tp_name,
281 self->ob_type->tp_name);
282 return NULL;
283 }
284 if (!PyType_IsSubtype((PyTypeObject *)self, PyDescr_TYPE(descr))) {
285 PyErr_Format(PyExc_TypeError,
286 "descriptor '%V' "
287 "requires a subtype of '%.100s' "
288 "but received '%.100s",
289 descr_name((PyDescrObject *)descr), "?",
290 PyDescr_TYPE(descr)->tp_name,
291 self->ob_type->tp_name);
292 return NULL;
293 }
294
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +0200295 func = PyCFunction_NewEx(descr->d_method, self, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 if (func == NULL)
297 return NULL;
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400298 args = PyTuple_GetSlice(args, 1, argc);
299 if (args == NULL) {
300 Py_DECREF(func);
301 return NULL;
302 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 result = PyEval_CallObjectWithKeywords(func, args, kwds);
304 Py_DECREF(func);
Benjamin Peterson7295c6a2012-05-01 09:51:09 -0400305 Py_DECREF(args);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000307}
308
309static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000310wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 Py_ssize_t argc;
313 PyObject *self, *func, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 /* Make sure that the first argument is acceptable as 'self' */
316 assert(PyTuple_Check(args));
317 argc = PyTuple_GET_SIZE(args);
318 if (argc < 1) {
319 PyErr_Format(PyExc_TypeError,
320 "descriptor '%V' of '%.100s' "
321 "object needs an argument",
322 descr_name((PyDescrObject *)descr), "?",
323 PyDescr_TYPE(descr)->tp_name);
324 return NULL;
325 }
326 self = PyTuple_GET_ITEM(args, 0);
Victor Stinner3249dec2011-05-01 23:19:15 +0200327 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +0200328 (PyObject *)PyDescr_TYPE(descr))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyErr_Format(PyExc_TypeError,
330 "descriptor '%V' "
331 "requires a '%.100s' object "
332 "but received a '%.100s'",
333 descr_name((PyDescrObject *)descr), "?",
334 PyDescr_TYPE(descr)->tp_name,
335 self->ob_type->tp_name);
336 return NULL;
337 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 func = PyWrapper_New((PyObject *)descr, self);
340 if (func == NULL)
341 return NULL;
342 args = PyTuple_GetSlice(args, 1, argc);
343 if (args == NULL) {
344 Py_DECREF(func);
345 return NULL;
346 }
347 result = PyEval_CallObjectWithKeywords(func, args, kwds);
348 Py_DECREF(args);
349 Py_DECREF(func);
350 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000351}
352
353static PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000354method_get_doc(PyMethodDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000355{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800356 return _PyType_GetDocFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
Larry Hastings5c661892014-01-24 06:17:25 -0800357}
358
359static PyObject *
360method_get_text_signature(PyMethodDescrObject *descr, void *closure)
361{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800362 return _PyType_GetTextSignatureFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000363}
364
Antoine Pitrou9d574812011-12-12 13:47:25 +0100365static PyObject *
366calculate_qualname(PyDescrObject *descr)
367{
368 PyObject *type_qualname, *res;
369 _Py_IDENTIFIER(__qualname__);
370
371 if (descr->d_name == NULL || !PyUnicode_Check(descr->d_name)) {
372 PyErr_SetString(PyExc_TypeError,
373 "<descriptor>.__name__ is not a unicode object");
374 return NULL;
375 }
376
377 type_qualname = _PyObject_GetAttrId((PyObject *)descr->d_type,
378 &PyId___qualname__);
379 if (type_qualname == NULL)
380 return NULL;
381
382 if (!PyUnicode_Check(type_qualname)) {
383 PyErr_SetString(PyExc_TypeError, "<descriptor>.__objclass__."
384 "__qualname__ is not a unicode object");
385 Py_XDECREF(type_qualname);
386 return NULL;
387 }
388
389 res = PyUnicode_FromFormat("%S.%S", type_qualname, descr->d_name);
390 Py_DECREF(type_qualname);
391 return res;
392}
393
394static PyObject *
395descr_get_qualname(PyDescrObject *descr)
396{
397 if (descr->d_qualname == NULL)
398 descr->d_qualname = calculate_qualname(descr);
399 Py_XINCREF(descr->d_qualname);
400 return descr->d_qualname;
401}
402
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100403static PyObject *
404descr_reduce(PyDescrObject *descr)
405{
406 PyObject *builtins;
407 PyObject *getattr;
408 _Py_IDENTIFIER(getattr);
409
410 builtins = PyEval_GetBuiltins();
411 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
412 return Py_BuildValue("O(OO)", getattr, PyDescr_TYPE(descr),
413 PyDescr_NAME(descr));
414}
415
416static PyMethodDef descr_methods[] = {
417 {"__reduce__", (PyCFunction)descr_reduce, METH_NOARGS, NULL},
418 {NULL, NULL}
419};
420
Guido van Rossum6f799372001-09-20 20:46:19 +0000421static PyMemberDef descr_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
423 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
424 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000425};
426
Guido van Rossum32d34c82001-09-20 21:45:26 +0000427static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 {"__doc__", (getter)method_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100429 {"__qualname__", (getter)descr_get_qualname},
Larry Hastings5c661892014-01-24 06:17:25 -0800430 {"__text_signature__", (getter)method_get_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 {0}
Guido van Rossum6f799372001-09-20 20:46:19 +0000432};
433
434static PyObject *
435member_get_doc(PyMemberDescrObject *descr, void *closure)
436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (descr->d_member->doc == NULL) {
438 Py_INCREF(Py_None);
439 return Py_None;
440 }
441 return PyUnicode_FromString(descr->d_member->doc);
Guido van Rossum6f799372001-09-20 20:46:19 +0000442}
443
Guido van Rossum32d34c82001-09-20 21:45:26 +0000444static PyGetSetDef member_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 {"__doc__", (getter)member_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100446 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000448};
449
450static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000451getset_get_doc(PyGetSetDescrObject *descr, void *closure)
452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (descr->d_getset->doc == NULL) {
454 Py_INCREF(Py_None);
455 return Py_None;
456 }
457 return PyUnicode_FromString(descr->d_getset->doc);
Guido van Rossum32d34c82001-09-20 21:45:26 +0000458}
459
460static PyGetSetDef getset_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 {"__doc__", (getter)getset_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100462 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 {0}
Guido van Rossum32d34c82001-09-20 21:45:26 +0000464};
465
466static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000467wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000468{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800469 return _PyType_GetDocFromInternalDoc(descr->d_base->name, descr->d_base->doc);
Larry Hastings5c661892014-01-24 06:17:25 -0800470}
471
472static PyObject *
473wrapperdescr_get_text_signature(PyWrapperDescrObject *descr, void *closure)
474{
Larry Hastings2623c8c2014-02-08 22:15:29 -0800475 return _PyType_GetTextSignatureFromInternalDoc(descr->d_base->name, descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000476}
477
Armin Rigoc6686b72005-11-07 08:38:00 +0000478static PyGetSetDef wrapperdescr_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 {"__doc__", (getter)wrapperdescr_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100480 {"__qualname__", (getter)descr_get_qualname},
Larry Hastings5c661892014-01-24 06:17:25 -0800481 {"__text_signature__", (getter)wrapperdescr_get_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000483};
484
Guido van Rossum048eb752001-10-02 21:24:57 +0000485static int
486descr_traverse(PyObject *self, visitproc visit, void *arg)
487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyDescrObject *descr = (PyDescrObject *)self;
489 Py_VISIT(descr->d_type);
490 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000491}
492
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000493PyTypeObject PyMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PyVarObject_HEAD_INIT(&PyType_Type, 0)
495 "method_descriptor",
496 sizeof(PyMethodDescrObject),
497 0,
498 (destructor)descr_dealloc, /* tp_dealloc */
499 0, /* tp_print */
500 0, /* tp_getattr */
501 0, /* tp_setattr */
502 0, /* tp_reserved */
503 (reprfunc)method_repr, /* tp_repr */
504 0, /* tp_as_number */
505 0, /* tp_as_sequence */
506 0, /* tp_as_mapping */
507 0, /* tp_hash */
508 (ternaryfunc)methoddescr_call, /* tp_call */
509 0, /* tp_str */
510 PyObject_GenericGetAttr, /* tp_getattro */
511 0, /* tp_setattro */
512 0, /* tp_as_buffer */
513 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
514 0, /* tp_doc */
515 descr_traverse, /* tp_traverse */
516 0, /* tp_clear */
517 0, /* tp_richcompare */
518 0, /* tp_weaklistoffset */
519 0, /* tp_iter */
520 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100521 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 descr_members, /* tp_members */
523 method_getset, /* tp_getset */
524 0, /* tp_base */
525 0, /* tp_dict */
526 (descrgetfunc)method_get, /* tp_descr_get */
527 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000528};
529
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000530/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000531PyTypeObject PyClassMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 PyVarObject_HEAD_INIT(&PyType_Type, 0)
533 "classmethod_descriptor",
534 sizeof(PyMethodDescrObject),
535 0,
536 (destructor)descr_dealloc, /* tp_dealloc */
537 0, /* tp_print */
538 0, /* tp_getattr */
539 0, /* tp_setattr */
540 0, /* tp_reserved */
541 (reprfunc)method_repr, /* tp_repr */
542 0, /* tp_as_number */
543 0, /* tp_as_sequence */
544 0, /* tp_as_mapping */
545 0, /* tp_hash */
546 (ternaryfunc)classmethoddescr_call, /* tp_call */
547 0, /* tp_str */
548 PyObject_GenericGetAttr, /* tp_getattro */
549 0, /* tp_setattro */
550 0, /* tp_as_buffer */
551 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
552 0, /* tp_doc */
553 descr_traverse, /* tp_traverse */
554 0, /* tp_clear */
555 0, /* tp_richcompare */
556 0, /* tp_weaklistoffset */
557 0, /* tp_iter */
558 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100559 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 descr_members, /* tp_members */
561 method_getset, /* tp_getset */
562 0, /* tp_base */
563 0, /* tp_dict */
564 (descrgetfunc)classmethod_get, /* tp_descr_get */
565 0, /* tp_descr_set */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000566};
567
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000568PyTypeObject PyMemberDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 PyVarObject_HEAD_INIT(&PyType_Type, 0)
570 "member_descriptor",
571 sizeof(PyMemberDescrObject),
572 0,
573 (destructor)descr_dealloc, /* tp_dealloc */
574 0, /* tp_print */
575 0, /* tp_getattr */
576 0, /* tp_setattr */
577 0, /* tp_reserved */
578 (reprfunc)member_repr, /* tp_repr */
579 0, /* tp_as_number */
580 0, /* tp_as_sequence */
581 0, /* tp_as_mapping */
582 0, /* tp_hash */
583 0, /* tp_call */
584 0, /* tp_str */
585 PyObject_GenericGetAttr, /* tp_getattro */
586 0, /* tp_setattro */
587 0, /* tp_as_buffer */
588 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
589 0, /* tp_doc */
590 descr_traverse, /* tp_traverse */
591 0, /* tp_clear */
592 0, /* tp_richcompare */
593 0, /* tp_weaklistoffset */
594 0, /* tp_iter */
595 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100596 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 descr_members, /* tp_members */
598 member_getset, /* tp_getset */
599 0, /* tp_base */
600 0, /* tp_dict */
601 (descrgetfunc)member_get, /* tp_descr_get */
602 (descrsetfunc)member_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000603};
604
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000605PyTypeObject PyGetSetDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 PyVarObject_HEAD_INIT(&PyType_Type, 0)
607 "getset_descriptor",
608 sizeof(PyGetSetDescrObject),
609 0,
610 (destructor)descr_dealloc, /* tp_dealloc */
611 0, /* tp_print */
612 0, /* tp_getattr */
613 0, /* tp_setattr */
614 0, /* tp_reserved */
615 (reprfunc)getset_repr, /* tp_repr */
616 0, /* tp_as_number */
617 0, /* tp_as_sequence */
618 0, /* tp_as_mapping */
619 0, /* tp_hash */
620 0, /* tp_call */
621 0, /* tp_str */
622 PyObject_GenericGetAttr, /* tp_getattro */
623 0, /* tp_setattro */
624 0, /* tp_as_buffer */
625 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
626 0, /* tp_doc */
627 descr_traverse, /* tp_traverse */
628 0, /* tp_clear */
629 0, /* tp_richcompare */
630 0, /* tp_weaklistoffset */
631 0, /* tp_iter */
632 0, /* tp_iternext */
633 0, /* tp_methods */
634 descr_members, /* tp_members */
635 getset_getset, /* tp_getset */
636 0, /* tp_base */
637 0, /* tp_dict */
638 (descrgetfunc)getset_get, /* tp_descr_get */
639 (descrsetfunc)getset_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640};
641
Guido van Rossumf4593e02001-10-03 12:09:30 +0000642PyTypeObject PyWrapperDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 PyVarObject_HEAD_INIT(&PyType_Type, 0)
644 "wrapper_descriptor",
645 sizeof(PyWrapperDescrObject),
646 0,
647 (destructor)descr_dealloc, /* tp_dealloc */
648 0, /* tp_print */
649 0, /* tp_getattr */
650 0, /* tp_setattr */
651 0, /* tp_reserved */
652 (reprfunc)wrapperdescr_repr, /* tp_repr */
653 0, /* tp_as_number */
654 0, /* tp_as_sequence */
655 0, /* tp_as_mapping */
656 0, /* tp_hash */
657 (ternaryfunc)wrapperdescr_call, /* tp_call */
658 0, /* tp_str */
659 PyObject_GenericGetAttr, /* tp_getattro */
660 0, /* tp_setattro */
661 0, /* tp_as_buffer */
662 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
663 0, /* tp_doc */
664 descr_traverse, /* tp_traverse */
665 0, /* tp_clear */
666 0, /* tp_richcompare */
667 0, /* tp_weaklistoffset */
668 0, /* tp_iter */
669 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100670 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 descr_members, /* tp_members */
672 wrapperdescr_getset, /* tp_getset */
673 0, /* tp_base */
674 0, /* tp_dict */
675 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
676 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677};
678
679static PyDescrObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000680descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 PyDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
685 if (descr != NULL) {
686 Py_XINCREF(type);
687 descr->d_type = type;
688 descr->d_name = PyUnicode_InternFromString(name);
689 if (descr->d_name == NULL) {
690 Py_DECREF(descr);
691 descr = NULL;
692 }
Benjamin Petersonf2fe7f02011-12-17 08:02:20 -0500693 else {
694 descr->d_qualname = NULL;
695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 }
697 return descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698}
699
700PyObject *
701PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyMethodDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
706 type, method->ml_name);
707 if (descr != NULL)
708 descr->d_method = method;
709 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000710}
711
712PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000713PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyMethodDescrObject *descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
718 type, method->ml_name);
719 if (descr != NULL)
720 descr->d_method = method;
721 return (PyObject *)descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000722}
723
724PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000725PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyMemberDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
730 type, member->name);
731 if (descr != NULL)
732 descr->d_member = member;
733 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000734}
735
736PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000737PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 PyGetSetDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
742 type, getset->name);
743 if (descr != NULL)
744 descr->d_getset = getset;
745 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746}
747
748PyObject *
749PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
754 type, base->name);
755 if (descr != NULL) {
756 descr->d_base = base;
757 descr->d_wrapped = wrapped;
758 }
759 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000760}
761
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762
Victor Stinner0db176f2012-04-16 00:16:30 +0200763/* --- mappingproxy: read-only proxy for mappings --- */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000764
765/* This has no reason to be in this file except that adding new files is a
766 bit of a pain */
767
768typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyObject_HEAD
Victor Stinner0db176f2012-04-16 00:16:30 +0200770 PyObject *mapping;
771} mappingproxyobject;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000772
Martin v. Löwis18e16552006-02-15 17:27:45 +0000773static Py_ssize_t
Victor Stinner0db176f2012-04-16 00:16:30 +0200774mappingproxy_len(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000775{
Victor Stinner0db176f2012-04-16 00:16:30 +0200776 return PyObject_Size(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000777}
778
779static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200780mappingproxy_getitem(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000781{
Victor Stinner0db176f2012-04-16 00:16:30 +0200782 return PyObject_GetItem(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000783}
784
Victor Stinner0db176f2012-04-16 00:16:30 +0200785static PyMappingMethods mappingproxy_as_mapping = {
786 (lenfunc)mappingproxy_len, /* mp_length */
787 (binaryfunc)mappingproxy_getitem, /* mp_subscript */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 0, /* mp_ass_subscript */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000789};
790
791static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200792mappingproxy_contains(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000793{
Victor Stinner0db176f2012-04-16 00:16:30 +0200794 if (PyDict_CheckExact(pp->mapping))
795 return PyDict_Contains(pp->mapping, key);
796 else
797 return PySequence_Contains(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798}
799
Victor Stinner0db176f2012-04-16 00:16:30 +0200800static PySequenceMethods mappingproxy_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 0, /* sq_length */
802 0, /* sq_concat */
803 0, /* sq_repeat */
804 0, /* sq_item */
805 0, /* sq_slice */
806 0, /* sq_ass_item */
807 0, /* sq_ass_slice */
Victor Stinner0db176f2012-04-16 00:16:30 +0200808 (objobjproc)mappingproxy_contains, /* sq_contains */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 0, /* sq_inplace_concat */
810 0, /* sq_inplace_repeat */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000811};
812
813static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200814mappingproxy_get(mappingproxyobject *pp, PyObject *args)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyObject *key, *def = Py_None;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200817 _Py_IDENTIFIER(get);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
820 return NULL;
Victor Stinner0db176f2012-04-16 00:16:30 +0200821 return _PyObject_CallMethodId(pp->mapping, &PyId_get, "(OO)", key, def);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000822}
823
824static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200825mappingproxy_keys(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000826{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200827 _Py_IDENTIFIER(keys);
Victor Stinner0db176f2012-04-16 00:16:30 +0200828 return _PyObject_CallMethodId(pp->mapping, &PyId_keys, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000829}
830
831static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200832mappingproxy_values(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000833{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200834 _Py_IDENTIFIER(values);
Victor Stinner0db176f2012-04-16 00:16:30 +0200835 return _PyObject_CallMethodId(pp->mapping, &PyId_values, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000836}
837
838static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200839mappingproxy_items(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000840{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200841 _Py_IDENTIFIER(items);
Victor Stinner0db176f2012-04-16 00:16:30 +0200842 return _PyObject_CallMethodId(pp->mapping, &PyId_items, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000843}
844
845static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200846mappingproxy_copy(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000847{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200848 _Py_IDENTIFIER(copy);
Victor Stinner0db176f2012-04-16 00:16:30 +0200849 return _PyObject_CallMethodId(pp->mapping, &PyId_copy, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000850}
851
Victor Stinner0db176f2012-04-16 00:16:30 +0200852/* WARNING: mappingproxy methods must not give access
853 to the underlying mapping */
854
855static PyMethodDef mappingproxy_methods[] = {
856 {"get", (PyCFunction)mappingproxy_get, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
Victor Stinner0db176f2012-04-16 00:16:30 +0200858 " d defaults to None.")},
859 {"keys", (PyCFunction)mappingproxy_keys, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PyDoc_STR("D.keys() -> list of D's keys")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200861 {"values", (PyCFunction)mappingproxy_values, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 PyDoc_STR("D.values() -> list of D's values")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200863 {"items", (PyCFunction)mappingproxy_items, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200865 {"copy", (PyCFunction)mappingproxy_copy, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 PyDoc_STR("D.copy() -> a shallow copy of D")},
867 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000868};
869
870static void
Victor Stinner0db176f2012-04-16 00:16:30 +0200871mappingproxy_dealloc(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 _PyObject_GC_UNTRACK(pp);
Victor Stinner0db176f2012-04-16 00:16:30 +0200874 Py_DECREF(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000876}
877
878static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200879mappingproxy_getiter(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880{
Victor Stinner0db176f2012-04-16 00:16:30 +0200881 return PyObject_GetIter(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882}
883
Neil Schemenauer26775122001-10-21 22:26:43 +0000884static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200885mappingproxy_str(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000886{
Victor Stinner0db176f2012-04-16 00:16:30 +0200887 return PyObject_Str(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000888}
889
Ezio Melottiac53ab62010-12-18 14:59:43 +0000890static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200891mappingproxy_repr(mappingproxyobject *pp)
Ezio Melottiac53ab62010-12-18 14:59:43 +0000892{
Victor Stinner0db176f2012-04-16 00:16:30 +0200893 return PyUnicode_FromFormat("mappingproxy(%R)", pp->mapping);
Ezio Melottiac53ab62010-12-18 14:59:43 +0000894}
895
Guido van Rossum048eb752001-10-02 21:24:57 +0000896static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200897mappingproxy_traverse(PyObject *self, visitproc visit, void *arg)
Guido van Rossum048eb752001-10-02 21:24:57 +0000898{
Victor Stinner0db176f2012-04-16 00:16:30 +0200899 mappingproxyobject *pp = (mappingproxyobject *)self;
900 Py_VISIT(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000902}
903
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000904static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200905mappingproxy_richcompare(mappingproxyobject *v, PyObject *w, int op)
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000906{
Victor Stinner0db176f2012-04-16 00:16:30 +0200907 return PyObject_RichCompare(v->mapping, w, op);
908}
909
910static int
911mappingproxy_check_mapping(PyObject *mapping)
912{
913 if (!PyMapping_Check(mapping)
914 || PyList_Check(mapping)
915 || PyTuple_Check(mapping)) {
916 PyErr_Format(PyExc_TypeError,
917 "mappingproxy() argument must be a mapping, not %s",
918 Py_TYPE(mapping)->tp_name);
919 return -1;
920 }
921 return 0;
922}
923
924static PyObject*
925mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
926{
927 static char *kwlist[] = {"mapping", NULL};
928 PyObject *mapping;
929 mappingproxyobject *mappingproxy;
930
931 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:mappingproxy",
932 kwlist, &mapping))
933 return NULL;
934
935 if (mappingproxy_check_mapping(mapping) == -1)
936 return NULL;
937
938 mappingproxy = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
939 if (mappingproxy == NULL)
940 return NULL;
941 Py_INCREF(mapping);
942 mappingproxy->mapping = mapping;
943 _PyObject_GC_TRACK(mappingproxy);
944 return (PyObject *)mappingproxy;
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000945}
946
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000947PyTypeObject PyDictProxy_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Victor Stinner0db176f2012-04-16 00:16:30 +0200949 "mappingproxy", /* tp_name */
950 sizeof(mappingproxyobject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 0, /* tp_itemsize */
952 /* methods */
Victor Stinner0db176f2012-04-16 00:16:30 +0200953 (destructor)mappingproxy_dealloc, /* tp_dealloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 0, /* tp_print */
955 0, /* tp_getattr */
956 0, /* tp_setattr */
957 0, /* tp_reserved */
Victor Stinner0db176f2012-04-16 00:16:30 +0200958 (reprfunc)mappingproxy_repr, /* tp_repr */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 0, /* tp_as_number */
Victor Stinner0db176f2012-04-16 00:16:30 +0200960 &mappingproxy_as_sequence, /* tp_as_sequence */
961 &mappingproxy_as_mapping, /* tp_as_mapping */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 0, /* tp_hash */
963 0, /* tp_call */
Victor Stinner0db176f2012-04-16 00:16:30 +0200964 (reprfunc)mappingproxy_str, /* tp_str */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyObject_GenericGetAttr, /* tp_getattro */
966 0, /* tp_setattro */
967 0, /* tp_as_buffer */
968 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
969 0, /* tp_doc */
Victor Stinner0db176f2012-04-16 00:16:30 +0200970 mappingproxy_traverse, /* tp_traverse */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 0, /* tp_clear */
Victor Stinner0db176f2012-04-16 00:16:30 +0200972 (richcmpfunc)mappingproxy_richcompare, /* tp_richcompare */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 0, /* tp_weaklistoffset */
Victor Stinner0db176f2012-04-16 00:16:30 +0200974 (getiterfunc)mappingproxy_getiter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 0, /* tp_iternext */
Victor Stinner0db176f2012-04-16 00:16:30 +0200976 mappingproxy_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 0, /* tp_members */
978 0, /* tp_getset */
979 0, /* tp_base */
980 0, /* tp_dict */
981 0, /* tp_descr_get */
982 0, /* tp_descr_set */
Victor Stinner0db176f2012-04-16 00:16:30 +0200983 0, /* tp_dictoffset */
984 0, /* tp_init */
985 0, /* tp_alloc */
986 mappingproxy_new, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000987};
988
989PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200990PyDictProxy_New(PyObject *mapping)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991{
Victor Stinner0db176f2012-04-16 00:16:30 +0200992 mappingproxyobject *pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000993
Victor Stinner0db176f2012-04-16 00:16:30 +0200994 if (mappingproxy_check_mapping(mapping) == -1)
995 return NULL;
996
997 pp = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 if (pp != NULL) {
Victor Stinner0db176f2012-04-16 00:16:30 +0200999 Py_INCREF(mapping);
1000 pp->mapping = mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 _PyObject_GC_TRACK(pp);
1002 }
1003 return (PyObject *)pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001004}
1005
1006
1007/* --- Wrapper object for "slot" methods --- */
1008
1009/* This has no reason to be in this file except that adding new files is a
1010 bit of a pain */
1011
1012typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 PyObject_HEAD
1014 PyWrapperDescrObject *descr;
1015 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001016} wrapperobject;
1017
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001018#define Wrapper_Check(v) (Py_TYPE(v) == &_PyMethodWrapper_Type)
Mark Dickinson211c6252009-02-01 10:28:51 +00001019
Tim Peters6d6c1a32001-08-02 04:15:00 +00001020static void
1021wrapper_dealloc(wrapperobject *wp)
1022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 PyObject_GC_UnTrack(wp);
1024 Py_TRASHCAN_SAFE_BEGIN(wp)
1025 Py_XDECREF(wp->descr);
1026 Py_XDECREF(wp->self);
1027 PyObject_GC_Del(wp);
1028 Py_TRASHCAN_SAFE_END(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001029}
1030
Mark Dickinson211c6252009-02-01 10:28:51 +00001031#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
1032
1033static PyObject *
1034wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +00001035{
Victor Stinner6f75a3e2013-05-08 00:44:15 +02001036 Py_intptr_t result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 PyObject *v;
1038 PyWrapperDescrObject *a_descr, *b_descr;
Mark Dickinson211c6252009-02-01 10:28:51 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 assert(a != NULL && b != NULL);
Mark Dickinson211c6252009-02-01 10:28:51 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 /* both arguments should be wrapperobjects */
1043 if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
1044 v = Py_NotImplemented;
1045 Py_INCREF(v);
1046 return v;
1047 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 /* compare by descriptor address; if the descriptors are the same,
1050 compare by the objects they're bound to */
1051 a_descr = ((wrapperobject *)a)->descr;
1052 b_descr = ((wrapperobject *)b)->descr;
1053 if (a_descr == b_descr) {
1054 a = ((wrapperobject *)a)->self;
1055 b = ((wrapperobject *)b)->self;
1056 return PyObject_RichCompare(a, b, op);
1057 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 result = a_descr - b_descr;
1060 switch (op) {
1061 case Py_EQ:
1062 v = TEST_COND(result == 0);
1063 break;
1064 case Py_NE:
1065 v = TEST_COND(result != 0);
1066 break;
1067 case Py_LE:
1068 v = TEST_COND(result <= 0);
1069 break;
1070 case Py_GE:
1071 v = TEST_COND(result >= 0);
1072 break;
1073 case Py_LT:
1074 v = TEST_COND(result < 0);
1075 break;
1076 case Py_GT:
1077 v = TEST_COND(result > 0);
1078 break;
1079 default:
1080 PyErr_BadArgument();
1081 return NULL;
1082 }
1083 Py_INCREF(v);
1084 return v;
Armin Rigoc6686b72005-11-07 08:38:00 +00001085}
1086
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001087static Py_hash_t
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001088wrapper_hash(wrapperobject *wp)
1089{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001090 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 x = _Py_HashPointer(wp->descr);
1092 if (x == -1)
1093 return -1;
1094 y = PyObject_Hash(wp->self);
1095 if (y == -1)
1096 return -1;
1097 x = x ^ y;
1098 if (x == -1)
1099 x = -2;
1100 return x;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001101}
1102
Armin Rigoc6686b72005-11-07 08:38:00 +00001103static PyObject *
1104wrapper_repr(wrapperobject *wp)
1105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
1107 wp->descr->d_base->name,
1108 wp->self->ob_type->tp_name,
1109 wp->self);
Armin Rigoc6686b72005-11-07 08:38:00 +00001110}
1111
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001112static PyObject *
1113wrapper_reduce(wrapperobject *wp)
1114{
1115 PyObject *builtins;
1116 PyObject *getattr;
1117 _Py_IDENTIFIER(getattr);
1118
1119 builtins = PyEval_GetBuiltins();
1120 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
1121 return Py_BuildValue("O(OO)", getattr, wp->self, PyDescr_NAME(wp->descr));
1122}
1123
1124static PyMethodDef wrapper_methods[] = {
1125 {"__reduce__", (PyCFunction)wrapper_reduce, METH_NOARGS, NULL},
1126 {NULL, NULL}
1127};
1128
Armin Rigoc6686b72005-11-07 08:38:00 +00001129static PyMemberDef wrapper_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
1131 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001132};
1133
1134static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +00001135wrapper_objclass(wrapperobject *wp)
1136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 PyObject *c = (PyObject *)PyDescr_TYPE(wp->descr);
Armin Rigoc6686b72005-11-07 08:38:00 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 Py_INCREF(c);
1140 return c;
Armin Rigoc6686b72005-11-07 08:38:00 +00001141}
1142
1143static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001144wrapper_name(wrapperobject *wp)
1145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001149}
1150
1151static PyObject *
Larry Hastings5c661892014-01-24 06:17:25 -08001152wrapper_doc(wrapperobject *wp, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001153{
Larry Hastings2623c8c2014-02-08 22:15:29 -08001154 return _PyType_GetDocFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
Larry Hastings5c661892014-01-24 06:17:25 -08001155}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001156
Larry Hastings5c661892014-01-24 06:17:25 -08001157static PyObject *
1158wrapper_text_signature(wrapperobject *wp, void *closure)
1159{
Larry Hastings2623c8c2014-02-08 22:15:29 -08001160 return _PyType_GetTextSignatureFromInternalDoc(wp->descr->d_base->name, wp->descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001161}
1162
Antoine Pitrou9d574812011-12-12 13:47:25 +01001163static PyObject *
1164wrapper_qualname(wrapperobject *wp)
1165{
1166 return descr_get_qualname((PyDescrObject *)wp->descr);
1167}
1168
Guido van Rossum32d34c82001-09-20 21:45:26 +00001169static PyGetSetDef wrapper_getsets[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 {"__objclass__", (getter)wrapper_objclass},
1171 {"__name__", (getter)wrapper_name},
Antoine Pitrou9d574812011-12-12 13:47:25 +01001172 {"__qualname__", (getter)wrapper_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 {"__doc__", (getter)wrapper_doc},
Larry Hastings5c661892014-01-24 06:17:25 -08001174 {"__text_signature__", (getter)wrapper_text_signature},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001176};
1177
1178static PyObject *
1179wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
1180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 wrapperfunc wrapper = wp->descr->d_base->wrapper;
1182 PyObject *self = wp->self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
1185 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
1186 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
1187 }
Guido van Rossumc8e56452001-10-22 00:43:43 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
1190 PyErr_Format(PyExc_TypeError,
1191 "wrapper %s doesn't take keyword arguments",
1192 wp->descr->d_base->name);
1193 return NULL;
1194 }
1195 return (*wrapper)(self, args, wp->descr->d_wrapped);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001196}
1197
Guido van Rossum048eb752001-10-02 21:24:57 +00001198static int
1199wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 wrapperobject *wp = (wrapperobject *)self;
1202 Py_VISIT(wp->descr);
1203 Py_VISIT(wp->self);
1204 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001205}
1206
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001207PyTypeObject _PyMethodWrapper_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1209 "method-wrapper", /* tp_name */
1210 sizeof(wrapperobject), /* tp_basicsize */
1211 0, /* tp_itemsize */
1212 /* methods */
1213 (destructor)wrapper_dealloc, /* tp_dealloc */
1214 0, /* tp_print */
1215 0, /* tp_getattr */
1216 0, /* tp_setattr */
1217 0, /* tp_reserved */
1218 (reprfunc)wrapper_repr, /* tp_repr */
1219 0, /* tp_as_number */
1220 0, /* tp_as_sequence */
1221 0, /* tp_as_mapping */
1222 (hashfunc)wrapper_hash, /* tp_hash */
1223 (ternaryfunc)wrapper_call, /* tp_call */
1224 0, /* tp_str */
1225 PyObject_GenericGetAttr, /* tp_getattro */
1226 0, /* tp_setattro */
1227 0, /* tp_as_buffer */
1228 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1229 0, /* tp_doc */
1230 wrapper_traverse, /* tp_traverse */
1231 0, /* tp_clear */
1232 wrapper_richcompare, /* tp_richcompare */
1233 0, /* tp_weaklistoffset */
1234 0, /* tp_iter */
1235 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001236 wrapper_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 wrapper_members, /* tp_members */
1238 wrapper_getsets, /* tp_getset */
1239 0, /* tp_base */
1240 0, /* tp_dict */
1241 0, /* tp_descr_get */
1242 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001243};
1244
1245PyObject *
1246PyWrapper_New(PyObject *d, PyObject *self)
1247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 wrapperobject *wp;
1249 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1252 descr = (PyWrapperDescrObject *)d;
Victor Stinner3249dec2011-05-01 23:19:15 +02001253 assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +02001254 (PyObject *)PyDescr_TYPE(descr)));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001255
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001256 wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (wp != NULL) {
1258 Py_INCREF(descr);
1259 wp->descr = descr;
1260 Py_INCREF(self);
1261 wp->self = self;
1262 _PyObject_GC_TRACK(wp);
1263 }
1264 return (PyObject *)wp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001266
1267
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001268/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001269
1270/*
Serhiy Storchakad741a882015-06-11 00:06:39 +03001271class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1274 if doc is None and fget is not None and hasattr(fget, "__doc__"):
Serhiy Storchakad741a882015-06-11 00:06:39 +03001275 doc = fget.__doc__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 self.__get = fget
1277 self.__set = fset
1278 self.__del = fdel
1279 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 def __get__(self, inst, type=None):
1282 if inst is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001283 return self
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if self.__get is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001285 raise AttributeError, "unreadable attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 def __set__(self, inst, value):
1289 if self.__set is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001290 raise AttributeError, "can't set attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 return self.__set(inst, value)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 def __delete__(self, inst):
1294 if self.__del is None:
Serhiy Storchakad741a882015-06-11 00:06:39 +03001295 raise AttributeError, "can't delete attribute"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 return self.__del(inst)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001297
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001298*/
1299
1300typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 PyObject_HEAD
1302 PyObject *prop_get;
1303 PyObject *prop_set;
1304 PyObject *prop_del;
1305 PyObject *prop_doc;
1306 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001307} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001308
Christian Heimes0449f632007-12-15 01:27:15 +00001309static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
Benjamin Peterson93964832010-06-28 03:07:10 +00001310 PyObject *);
Christian Heimes0449f632007-12-15 01:27:15 +00001311
Tim Peters66c1a522001-09-24 21:17:50 +00001312static PyMemberDef property_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1314 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1315 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
Raymond Hettingereac503a2015-05-13 01:09:59 -07001316 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 {0}
Tim Peters66c1a522001-09-24 21:17:50 +00001318};
1319
Christian Heimes0449f632007-12-15 01:27:15 +00001320
Guido van Rossum58da9312007-11-10 23:39:45 +00001321PyDoc_STRVAR(getter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 "Descriptor to change the getter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001323
Neal Norwitz32dde222008-04-15 06:43:13 +00001324static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001325property_getter(PyObject *self, PyObject *getter)
1326{
Benjamin Peterson93964832010-06-28 03:07:10 +00001327 return property_copy(self, getter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001328}
1329
Christian Heimes0449f632007-12-15 01:27:15 +00001330
Guido van Rossum58da9312007-11-10 23:39:45 +00001331PyDoc_STRVAR(setter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001333
Neal Norwitz32dde222008-04-15 06:43:13 +00001334static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001335property_setter(PyObject *self, PyObject *setter)
1336{
Benjamin Peterson93964832010-06-28 03:07:10 +00001337 return property_copy(self, NULL, setter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001338}
1339
Christian Heimes0449f632007-12-15 01:27:15 +00001340
Guido van Rossum58da9312007-11-10 23:39:45 +00001341PyDoc_STRVAR(deleter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 "Descriptor to change the deleter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001343
Neal Norwitz32dde222008-04-15 06:43:13 +00001344static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001345property_deleter(PyObject *self, PyObject *deleter)
1346{
Benjamin Peterson93964832010-06-28 03:07:10 +00001347 return property_copy(self, NULL, NULL, deleter);
Guido van Rossum58da9312007-11-10 23:39:45 +00001348}
1349
1350
Guido van Rossum58da9312007-11-10 23:39:45 +00001351static PyMethodDef property_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 {"getter", property_getter, METH_O, getter_doc},
1353 {"setter", property_setter, METH_O, setter_doc},
1354 {"deleter", property_deleter, METH_O, deleter_doc},
1355 {0}
Guido van Rossum58da9312007-11-10 23:39:45 +00001356};
1357
Tim Peters66c1a522001-09-24 21:17:50 +00001358
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001359static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001360property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 _PyObject_GC_UNTRACK(self);
1365 Py_XDECREF(gs->prop_get);
1366 Py_XDECREF(gs->prop_set);
1367 Py_XDECREF(gs->prop_del);
1368 Py_XDECREF(gs->prop_doc);
1369 self->ob_type->tp_free(self);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001370}
1371
1372static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001373property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001374{
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001375 static PyObject * volatile cached_args = NULL;
1376 PyObject *args;
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001377 PyObject *ret;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (obj == NULL || obj == Py_None) {
1381 Py_INCREF(self);
1382 return self;
1383 }
1384 if (gs->prop_get == NULL) {
1385 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1386 return NULL;
1387 }
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001388 args = cached_args;
1389 if (!args || Py_REFCNT(args) != 1) {
1390 Py_CLEAR(cached_args);
1391 if (!(cached_args = args = PyTuple_New(1)))
1392 return NULL;
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001393 }
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001394 Py_INCREF(args);
1395 assert (Py_REFCNT(args) == 2);
1396 Py_INCREF(obj);
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001397 PyTuple_SET_ITEM(args, 0, obj);
1398 ret = PyObject_Call(gs->prop_get, args, NULL);
Serhiy Storchakaa7a0ad62015-05-24 21:38:06 +03001399 if (args == cached_args) {
1400 if (Py_REFCNT(args) == 2) {
1401 obj = PyTuple_GET_ITEM(args, 0);
1402 PyTuple_SET_ITEM(args, 0, NULL);
1403 Py_XDECREF(obj);
1404 }
1405 else {
1406 Py_CLEAR(cached_args);
1407 }
1408 }
1409 Py_DECREF(args);
Raymond Hettingerc4e335b2015-04-30 08:08:13 -07001410 return ret;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001411}
1412
1413static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001414property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 propertyobject *gs = (propertyobject *)self;
1417 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (value == NULL)
1420 func = gs->prop_del;
1421 else
1422 func = gs->prop_set;
1423 if (func == NULL) {
1424 PyErr_SetString(PyExc_AttributeError,
1425 value == NULL ?
1426 "can't delete attribute" :
1427 "can't set attribute");
1428 return -1;
1429 }
1430 if (value == NULL)
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001431 res = PyObject_CallFunctionObjArgs(func, obj, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 else
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001433 res = PyObject_CallFunctionObjArgs(func, obj, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if (res == NULL)
1435 return -1;
1436 Py_DECREF(res);
1437 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001438}
1439
Christian Heimes0449f632007-12-15 01:27:15 +00001440static PyObject *
Benjamin Peterson93964832010-06-28 03:07:10 +00001441property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
Christian Heimes0449f632007-12-15 01:27:15 +00001442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 propertyobject *pold = (propertyobject *)old;
Benjamin Peterson93964832010-06-28 03:07:10 +00001444 PyObject *new, *type, *doc;
Christian Heimes0449f632007-12-15 01:27:15 +00001445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 type = PyObject_Type(old);
1447 if (type == NULL)
1448 return NULL;
Christian Heimes0449f632007-12-15 01:27:15 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 if (get == NULL || get == Py_None) {
1451 Py_XDECREF(get);
1452 get = pold->prop_get ? pold->prop_get : Py_None;
1453 }
1454 if (set == NULL || set == Py_None) {
1455 Py_XDECREF(set);
1456 set = pold->prop_set ? pold->prop_set : Py_None;
1457 }
1458 if (del == NULL || del == Py_None) {
1459 Py_XDECREF(del);
1460 del = pold->prop_del ? pold->prop_del : Py_None;
1461 }
Benjamin Peterson93964832010-06-28 03:07:10 +00001462 if (pold->getter_doc && get != Py_None) {
1463 /* make _init use __doc__ from getter */
1464 doc = Py_None;
1465 }
1466 else {
1467 doc = pold->prop_doc ? pold->prop_doc : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 }
R. David Murrayb18500d2009-05-04 22:59:07 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
1471 Py_DECREF(type);
1472 if (new == NULL)
1473 return NULL;
1474 return new;
Christian Heimes0449f632007-12-15 01:27:15 +00001475}
1476
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001477static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001478property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
1481 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
1482 propertyobject *prop = (propertyobject *)self;
Tim Peters66c1a522001-09-24 21:17:50 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
1485 kwlist, &get, &set, &del, &doc))
1486 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 if (get == Py_None)
1489 get = NULL;
1490 if (set == Py_None)
1491 set = NULL;
1492 if (del == Py_None)
1493 del = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 Py_XINCREF(get);
1496 Py_XINCREF(set);
1497 Py_XINCREF(del);
1498 Py_XINCREF(doc);
Christian Heimes0449f632007-12-15 01:27:15 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 prop->prop_get = get;
1501 prop->prop_set = set;
1502 prop->prop_del = del;
1503 prop->prop_doc = doc;
1504 prop->getter_doc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 /* if no docstring given and the getter has one, use that one */
1507 if ((doc == NULL || doc == Py_None) && get != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001508 _Py_IDENTIFIER(__doc__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001509 PyObject *get_doc = _PyObject_GetAttrId(get, &PyId___doc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (get_doc) {
1511 if (Py_TYPE(self) == &PyProperty_Type) {
Serhiy Storchaka48842712016-04-06 09:45:48 +03001512 Py_XSETREF(prop->prop_doc, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 }
1514 else {
1515 /* If this is a property subclass, put __doc__
1516 in dict of the subclass instance instead,
1517 otherwise it gets shadowed by __doc__ in the
1518 class's dict. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001519 int err = _PyObject_SetAttrId(self, &PyId___doc__, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 Py_DECREF(get_doc);
1521 if (err < 0)
1522 return -1;
1523 }
1524 prop->getter_doc = 1;
1525 }
1526 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1527 PyErr_Clear();
1528 }
1529 else {
1530 return -1;
1531 }
1532 }
1533
1534 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001535}
1536
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001537static PyObject *
1538property_get___isabstractmethod__(propertyobject *prop, void *closure)
1539{
1540 int res = _PyObject_IsAbstract(prop->prop_get);
1541 if (res == -1) {
1542 return NULL;
1543 }
1544 else if (res) {
1545 Py_RETURN_TRUE;
1546 }
1547
1548 res = _PyObject_IsAbstract(prop->prop_set);
1549 if (res == -1) {
1550 return NULL;
1551 }
1552 else if (res) {
1553 Py_RETURN_TRUE;
1554 }
1555
1556 res = _PyObject_IsAbstract(prop->prop_del);
1557 if (res == -1) {
1558 return NULL;
1559 }
1560 else if (res) {
1561 Py_RETURN_TRUE;
1562 }
1563 Py_RETURN_FALSE;
1564}
1565
1566static PyGetSetDef property_getsetlist[] = {
1567 {"__isabstractmethod__",
1568 (getter)property_get___isabstractmethod__, NULL,
1569 NULL,
1570 NULL},
1571 {NULL} /* Sentinel */
1572};
1573
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001574PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001575"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1576"\n"
1577"fget is a function to be used for getting an attribute value, and likewise\n"
1578"fset is a function for setting, and fdel a function for del'ing, an\n"
Raymond Hettinger97fc2ba2013-11-24 14:53:29 -08001579"attribute. Typical use is to define a managed attribute x:\n\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001580"class C(object):\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001581" def getx(self): return self._x\n"
1582" def setx(self, value): self._x = value\n"
1583" def delx(self): del self._x\n"
1584" x = property(getx, setx, delx, \"I'm the 'x' property.\")\n"
1585"\n"
Raymond Hettinger97fc2ba2013-11-24 14:53:29 -08001586"Decorators make defining new properties or modifying existing ones easy:\n\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001587"class C(object):\n"
1588" @property\n"
Raymond Hettinger97fc2ba2013-11-24 14:53:29 -08001589" def x(self):\n"
Gregory P. Smith0e524042013-11-25 00:30:56 -08001590" \"I am the 'x' property.\"\n"
Raymond Hettinger97fc2ba2013-11-24 14:53:29 -08001591" return self._x\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001592" @x.setter\n"
Raymond Hettinger97fc2ba2013-11-24 14:53:29 -08001593" def x(self, value):\n"
1594" self._x = value\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001595" @x.deleter\n"
Raymond Hettinger97fc2ba2013-11-24 14:53:29 -08001596" def x(self):\n"
1597" del self._x\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001598);
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001599
Guido van Rossum048eb752001-10-02 21:24:57 +00001600static int
1601property_traverse(PyObject *self, visitproc visit, void *arg)
1602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 propertyobject *pp = (propertyobject *)self;
1604 Py_VISIT(pp->prop_get);
1605 Py_VISIT(pp->prop_set);
1606 Py_VISIT(pp->prop_del);
1607 Py_VISIT(pp->prop_doc);
1608 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001609}
1610
Raymond Hettingerd4be6912015-05-13 11:12:33 -07001611static int
1612property_clear(PyObject *self)
1613{
1614 propertyobject *pp = (propertyobject *)self;
1615 Py_CLEAR(pp->prop_doc);
1616 return 0;
1617}
1618
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001619PyTypeObject PyProperty_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1621 "property", /* tp_name */
1622 sizeof(propertyobject), /* tp_basicsize */
1623 0, /* tp_itemsize */
1624 /* methods */
1625 property_dealloc, /* tp_dealloc */
1626 0, /* tp_print */
1627 0, /* tp_getattr */
1628 0, /* tp_setattr */
1629 0, /* tp_reserved */
1630 0, /* tp_repr */
1631 0, /* tp_as_number */
1632 0, /* tp_as_sequence */
1633 0, /* tp_as_mapping */
1634 0, /* tp_hash */
1635 0, /* tp_call */
1636 0, /* tp_str */
1637 PyObject_GenericGetAttr, /* tp_getattro */
1638 0, /* tp_setattro */
1639 0, /* tp_as_buffer */
1640 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1641 Py_TPFLAGS_BASETYPE, /* tp_flags */
1642 property_doc, /* tp_doc */
1643 property_traverse, /* tp_traverse */
Raymond Hettingerd4be6912015-05-13 11:12:33 -07001644 (inquiry)property_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 0, /* tp_richcompare */
1646 0, /* tp_weaklistoffset */
1647 0, /* tp_iter */
1648 0, /* tp_iternext */
1649 property_methods, /* tp_methods */
1650 property_members, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001651 property_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 0, /* tp_base */
1653 0, /* tp_dict */
1654 property_descr_get, /* tp_descr_get */
1655 property_descr_set, /* tp_descr_set */
1656 0, /* tp_dictoffset */
1657 property_init, /* tp_init */
1658 PyType_GenericAlloc, /* tp_alloc */
1659 PyType_GenericNew, /* tp_new */
1660 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001661};