blob: abcc0020ff4acb43ec5b9c9b4cd017f7c3234c57 [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 }
118 return PyCFunction_New(descr->d_method, type);
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;
128 return PyCFunction_New(descr->d_method, obj);
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 func = PyCFunction_New(descr->d_method, self);
243 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
295 func = PyCFunction_New(descr->d_method, self);
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (descr->d_method->ml_doc == NULL) {
357 Py_INCREF(Py_None);
358 return Py_None;
359 }
360 return PyUnicode_FromString(descr->d_method->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000361}
362
Antoine Pitrou9d574812011-12-12 13:47:25 +0100363static PyObject *
364calculate_qualname(PyDescrObject *descr)
365{
366 PyObject *type_qualname, *res;
367 _Py_IDENTIFIER(__qualname__);
368
369 if (descr->d_name == NULL || !PyUnicode_Check(descr->d_name)) {
370 PyErr_SetString(PyExc_TypeError,
371 "<descriptor>.__name__ is not a unicode object");
372 return NULL;
373 }
374
375 type_qualname = _PyObject_GetAttrId((PyObject *)descr->d_type,
376 &PyId___qualname__);
377 if (type_qualname == NULL)
378 return NULL;
379
380 if (!PyUnicode_Check(type_qualname)) {
381 PyErr_SetString(PyExc_TypeError, "<descriptor>.__objclass__."
382 "__qualname__ is not a unicode object");
383 Py_XDECREF(type_qualname);
384 return NULL;
385 }
386
387 res = PyUnicode_FromFormat("%S.%S", type_qualname, descr->d_name);
388 Py_DECREF(type_qualname);
389 return res;
390}
391
392static PyObject *
393descr_get_qualname(PyDescrObject *descr)
394{
395 if (descr->d_qualname == NULL)
396 descr->d_qualname = calculate_qualname(descr);
397 Py_XINCREF(descr->d_qualname);
398 return descr->d_qualname;
399}
400
Guido van Rossum6f799372001-09-20 20:46:19 +0000401static PyMemberDef descr_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
403 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
404 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000405};
406
Guido van Rossum32d34c82001-09-20 21:45:26 +0000407static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 {"__doc__", (getter)method_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100409 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 {0}
Guido van Rossum6f799372001-09-20 20:46:19 +0000411};
412
413static PyObject *
414member_get_doc(PyMemberDescrObject *descr, void *closure)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 if (descr->d_member->doc == NULL) {
417 Py_INCREF(Py_None);
418 return Py_None;
419 }
420 return PyUnicode_FromString(descr->d_member->doc);
Guido van Rossum6f799372001-09-20 20:46:19 +0000421}
422
Guido van Rossum32d34c82001-09-20 21:45:26 +0000423static PyGetSetDef member_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 {"__doc__", (getter)member_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100425 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000427};
428
429static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000430getset_get_doc(PyGetSetDescrObject *descr, void *closure)
431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (descr->d_getset->doc == NULL) {
433 Py_INCREF(Py_None);
434 return Py_None;
435 }
436 return PyUnicode_FromString(descr->d_getset->doc);
Guido van Rossum32d34c82001-09-20 21:45:26 +0000437}
438
439static PyGetSetDef getset_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 {"__doc__", (getter)getset_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100441 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 {0}
Guido van Rossum32d34c82001-09-20 21:45:26 +0000443};
444
445static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000446wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (descr->d_base->doc == NULL) {
449 Py_INCREF(Py_None);
450 return Py_None;
451 }
452 return PyUnicode_FromString(descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000453}
454
Armin Rigoc6686b72005-11-07 08:38:00 +0000455static PyGetSetDef wrapperdescr_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 {"__doc__", (getter)wrapperdescr_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100457 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000459};
460
Guido van Rossum048eb752001-10-02 21:24:57 +0000461static int
462descr_traverse(PyObject *self, visitproc visit, void *arg)
463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 PyDescrObject *descr = (PyDescrObject *)self;
465 Py_VISIT(descr->d_type);
466 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000467}
468
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000469PyTypeObject PyMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyVarObject_HEAD_INIT(&PyType_Type, 0)
471 "method_descriptor",
472 sizeof(PyMethodDescrObject),
473 0,
474 (destructor)descr_dealloc, /* tp_dealloc */
475 0, /* tp_print */
476 0, /* tp_getattr */
477 0, /* tp_setattr */
478 0, /* tp_reserved */
479 (reprfunc)method_repr, /* tp_repr */
480 0, /* tp_as_number */
481 0, /* tp_as_sequence */
482 0, /* tp_as_mapping */
483 0, /* tp_hash */
484 (ternaryfunc)methoddescr_call, /* tp_call */
485 0, /* tp_str */
486 PyObject_GenericGetAttr, /* tp_getattro */
487 0, /* tp_setattro */
488 0, /* tp_as_buffer */
489 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
490 0, /* tp_doc */
491 descr_traverse, /* tp_traverse */
492 0, /* tp_clear */
493 0, /* tp_richcompare */
494 0, /* tp_weaklistoffset */
495 0, /* tp_iter */
496 0, /* tp_iternext */
497 0, /* tp_methods */
498 descr_members, /* tp_members */
499 method_getset, /* tp_getset */
500 0, /* tp_base */
501 0, /* tp_dict */
502 (descrgetfunc)method_get, /* tp_descr_get */
503 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000504};
505
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000506/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000507PyTypeObject PyClassMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 PyVarObject_HEAD_INIT(&PyType_Type, 0)
509 "classmethod_descriptor",
510 sizeof(PyMethodDescrObject),
511 0,
512 (destructor)descr_dealloc, /* tp_dealloc */
513 0, /* tp_print */
514 0, /* tp_getattr */
515 0, /* tp_setattr */
516 0, /* tp_reserved */
517 (reprfunc)method_repr, /* tp_repr */
518 0, /* tp_as_number */
519 0, /* tp_as_sequence */
520 0, /* tp_as_mapping */
521 0, /* tp_hash */
522 (ternaryfunc)classmethoddescr_call, /* tp_call */
523 0, /* tp_str */
524 PyObject_GenericGetAttr, /* tp_getattro */
525 0, /* tp_setattro */
526 0, /* tp_as_buffer */
527 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
528 0, /* tp_doc */
529 descr_traverse, /* tp_traverse */
530 0, /* tp_clear */
531 0, /* tp_richcompare */
532 0, /* tp_weaklistoffset */
533 0, /* tp_iter */
534 0, /* tp_iternext */
535 0, /* tp_methods */
536 descr_members, /* tp_members */
537 method_getset, /* tp_getset */
538 0, /* tp_base */
539 0, /* tp_dict */
540 (descrgetfunc)classmethod_get, /* tp_descr_get */
541 0, /* tp_descr_set */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000542};
543
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000544PyTypeObject PyMemberDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyVarObject_HEAD_INIT(&PyType_Type, 0)
546 "member_descriptor",
547 sizeof(PyMemberDescrObject),
548 0,
549 (destructor)descr_dealloc, /* tp_dealloc */
550 0, /* tp_print */
551 0, /* tp_getattr */
552 0, /* tp_setattr */
553 0, /* tp_reserved */
554 (reprfunc)member_repr, /* tp_repr */
555 0, /* tp_as_number */
556 0, /* tp_as_sequence */
557 0, /* tp_as_mapping */
558 0, /* tp_hash */
559 0, /* tp_call */
560 0, /* tp_str */
561 PyObject_GenericGetAttr, /* tp_getattro */
562 0, /* tp_setattro */
563 0, /* tp_as_buffer */
564 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
565 0, /* tp_doc */
566 descr_traverse, /* tp_traverse */
567 0, /* tp_clear */
568 0, /* tp_richcompare */
569 0, /* tp_weaklistoffset */
570 0, /* tp_iter */
571 0, /* tp_iternext */
572 0, /* tp_methods */
573 descr_members, /* tp_members */
574 member_getset, /* tp_getset */
575 0, /* tp_base */
576 0, /* tp_dict */
577 (descrgetfunc)member_get, /* tp_descr_get */
578 (descrsetfunc)member_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000579};
580
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000581PyTypeObject PyGetSetDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 PyVarObject_HEAD_INIT(&PyType_Type, 0)
583 "getset_descriptor",
584 sizeof(PyGetSetDescrObject),
585 0,
586 (destructor)descr_dealloc, /* tp_dealloc */
587 0, /* tp_print */
588 0, /* tp_getattr */
589 0, /* tp_setattr */
590 0, /* tp_reserved */
591 (reprfunc)getset_repr, /* tp_repr */
592 0, /* tp_as_number */
593 0, /* tp_as_sequence */
594 0, /* tp_as_mapping */
595 0, /* tp_hash */
596 0, /* tp_call */
597 0, /* tp_str */
598 PyObject_GenericGetAttr, /* tp_getattro */
599 0, /* tp_setattro */
600 0, /* tp_as_buffer */
601 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
602 0, /* tp_doc */
603 descr_traverse, /* tp_traverse */
604 0, /* tp_clear */
605 0, /* tp_richcompare */
606 0, /* tp_weaklistoffset */
607 0, /* tp_iter */
608 0, /* tp_iternext */
609 0, /* tp_methods */
610 descr_members, /* tp_members */
611 getset_getset, /* tp_getset */
612 0, /* tp_base */
613 0, /* tp_dict */
614 (descrgetfunc)getset_get, /* tp_descr_get */
615 (descrsetfunc)getset_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000616};
617
Guido van Rossumf4593e02001-10-03 12:09:30 +0000618PyTypeObject PyWrapperDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyVarObject_HEAD_INIT(&PyType_Type, 0)
620 "wrapper_descriptor",
621 sizeof(PyWrapperDescrObject),
622 0,
623 (destructor)descr_dealloc, /* tp_dealloc */
624 0, /* tp_print */
625 0, /* tp_getattr */
626 0, /* tp_setattr */
627 0, /* tp_reserved */
628 (reprfunc)wrapperdescr_repr, /* tp_repr */
629 0, /* tp_as_number */
630 0, /* tp_as_sequence */
631 0, /* tp_as_mapping */
632 0, /* tp_hash */
633 (ternaryfunc)wrapperdescr_call, /* tp_call */
634 0, /* tp_str */
635 PyObject_GenericGetAttr, /* tp_getattro */
636 0, /* tp_setattro */
637 0, /* tp_as_buffer */
638 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
639 0, /* tp_doc */
640 descr_traverse, /* tp_traverse */
641 0, /* tp_clear */
642 0, /* tp_richcompare */
643 0, /* tp_weaklistoffset */
644 0, /* tp_iter */
645 0, /* tp_iternext */
646 0, /* tp_methods */
647 descr_members, /* tp_members */
648 wrapperdescr_getset, /* tp_getset */
649 0, /* tp_base */
650 0, /* tp_dict */
651 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
652 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000653};
654
655static PyDescrObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000656descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 PyDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
661 if (descr != NULL) {
662 Py_XINCREF(type);
663 descr->d_type = type;
664 descr->d_name = PyUnicode_InternFromString(name);
665 if (descr->d_name == NULL) {
666 Py_DECREF(descr);
667 descr = NULL;
668 }
Benjamin Petersonf2fe7f02011-12-17 08:02:20 -0500669 else {
670 descr->d_qualname = NULL;
671 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 }
673 return descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000674}
675
676PyObject *
677PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 PyMethodDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
682 type, method->ml_name);
683 if (descr != NULL)
684 descr->d_method = method;
685 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000686}
687
688PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000689PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 PyMethodDescrObject *descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
694 type, method->ml_name);
695 if (descr != NULL)
696 descr->d_method = method;
697 return (PyObject *)descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000698}
699
700PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000701PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyMemberDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
706 type, member->name);
707 if (descr != NULL)
708 descr->d_member = member;
709 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000710}
711
712PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000713PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyGetSetDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
718 type, getset->name);
719 if (descr != NULL)
720 descr->d_getset = getset;
721 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722}
723
724PyObject *
725PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
730 type, base->name);
731 if (descr != NULL) {
732 descr->d_base = base;
733 descr->d_wrapped = wrapped;
734 }
735 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000736}
737
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738
Victor Stinner0db176f2012-04-16 00:16:30 +0200739/* --- mappingproxy: read-only proxy for mappings --- */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740
741/* This has no reason to be in this file except that adding new files is a
742 bit of a pain */
743
744typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 PyObject_HEAD
Victor Stinner0db176f2012-04-16 00:16:30 +0200746 PyObject *mapping;
747} mappingproxyobject;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000748
Martin v. Löwis18e16552006-02-15 17:27:45 +0000749static Py_ssize_t
Victor Stinner0db176f2012-04-16 00:16:30 +0200750mappingproxy_len(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000751{
Victor Stinner0db176f2012-04-16 00:16:30 +0200752 return PyObject_Size(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753}
754
755static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200756mappingproxy_getitem(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000757{
Victor Stinner0db176f2012-04-16 00:16:30 +0200758 return PyObject_GetItem(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759}
760
Victor Stinner0db176f2012-04-16 00:16:30 +0200761static PyMappingMethods mappingproxy_as_mapping = {
762 (lenfunc)mappingproxy_len, /* mp_length */
763 (binaryfunc)mappingproxy_getitem, /* mp_subscript */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 0, /* mp_ass_subscript */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765};
766
767static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200768mappingproxy_contains(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000769{
Victor Stinner0db176f2012-04-16 00:16:30 +0200770 if (PyDict_CheckExact(pp->mapping))
771 return PyDict_Contains(pp->mapping, key);
772 else
773 return PySequence_Contains(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000774}
775
Victor Stinner0db176f2012-04-16 00:16:30 +0200776static PySequenceMethods mappingproxy_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 0, /* sq_length */
778 0, /* sq_concat */
779 0, /* sq_repeat */
780 0, /* sq_item */
781 0, /* sq_slice */
782 0, /* sq_ass_item */
783 0, /* sq_ass_slice */
Victor Stinner0db176f2012-04-16 00:16:30 +0200784 (objobjproc)mappingproxy_contains, /* sq_contains */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 0, /* sq_inplace_concat */
786 0, /* sq_inplace_repeat */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000787};
788
789static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200790mappingproxy_get(mappingproxyobject *pp, PyObject *args)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyObject *key, *def = Py_None;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200793 _Py_IDENTIFIER(get);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
796 return NULL;
Victor Stinner0db176f2012-04-16 00:16:30 +0200797 return _PyObject_CallMethodId(pp->mapping, &PyId_get, "(OO)", key, def);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798}
799
800static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200801mappingproxy_keys(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000802{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200803 _Py_IDENTIFIER(keys);
Victor Stinner0db176f2012-04-16 00:16:30 +0200804 return _PyObject_CallMethodId(pp->mapping, &PyId_keys, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805}
806
807static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200808mappingproxy_values(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000809{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200810 _Py_IDENTIFIER(values);
Victor Stinner0db176f2012-04-16 00:16:30 +0200811 return _PyObject_CallMethodId(pp->mapping, &PyId_values, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000812}
813
814static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200815mappingproxy_items(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000816{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200817 _Py_IDENTIFIER(items);
Victor Stinner0db176f2012-04-16 00:16:30 +0200818 return _PyObject_CallMethodId(pp->mapping, &PyId_items, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000819}
820
821static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200822mappingproxy_copy(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000823{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200824 _Py_IDENTIFIER(copy);
Victor Stinner0db176f2012-04-16 00:16:30 +0200825 return _PyObject_CallMethodId(pp->mapping, &PyId_copy, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000826}
827
Victor Stinner0db176f2012-04-16 00:16:30 +0200828/* WARNING: mappingproxy methods must not give access
829 to the underlying mapping */
830
831static PyMethodDef mappingproxy_methods[] = {
832 {"get", (PyCFunction)mappingproxy_get, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
Victor Stinner0db176f2012-04-16 00:16:30 +0200834 " d defaults to None.")},
835 {"keys", (PyCFunction)mappingproxy_keys, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 PyDoc_STR("D.keys() -> list of D's keys")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200837 {"values", (PyCFunction)mappingproxy_values, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 PyDoc_STR("D.values() -> list of D's values")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200839 {"items", (PyCFunction)mappingproxy_items, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200841 {"copy", (PyCFunction)mappingproxy_copy, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 PyDoc_STR("D.copy() -> a shallow copy of D")},
843 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000844};
845
846static void
Victor Stinner0db176f2012-04-16 00:16:30 +0200847mappingproxy_dealloc(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 _PyObject_GC_UNTRACK(pp);
Victor Stinner0db176f2012-04-16 00:16:30 +0200850 Py_DECREF(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000852}
853
854static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200855mappingproxy_getiter(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000856{
Victor Stinner0db176f2012-04-16 00:16:30 +0200857 return PyObject_GetIter(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000858}
859
Neil Schemenauer26775122001-10-21 22:26:43 +0000860static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200861mappingproxy_str(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000862{
Victor Stinner0db176f2012-04-16 00:16:30 +0200863 return PyObject_Str(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000864}
865
Ezio Melottiac53ab62010-12-18 14:59:43 +0000866static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200867mappingproxy_repr(mappingproxyobject *pp)
Ezio Melottiac53ab62010-12-18 14:59:43 +0000868{
Victor Stinner0db176f2012-04-16 00:16:30 +0200869 return PyUnicode_FromFormat("mappingproxy(%R)", pp->mapping);
Ezio Melottiac53ab62010-12-18 14:59:43 +0000870}
871
Guido van Rossum048eb752001-10-02 21:24:57 +0000872static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200873mappingproxy_traverse(PyObject *self, visitproc visit, void *arg)
Guido van Rossum048eb752001-10-02 21:24:57 +0000874{
Victor Stinner0db176f2012-04-16 00:16:30 +0200875 mappingproxyobject *pp = (mappingproxyobject *)self;
876 Py_VISIT(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000878}
879
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000880static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200881mappingproxy_richcompare(mappingproxyobject *v, PyObject *w, int op)
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000882{
Victor Stinner0db176f2012-04-16 00:16:30 +0200883 return PyObject_RichCompare(v->mapping, w, op);
884}
885
886static int
887mappingproxy_check_mapping(PyObject *mapping)
888{
889 if (!PyMapping_Check(mapping)
890 || PyList_Check(mapping)
891 || PyTuple_Check(mapping)) {
892 PyErr_Format(PyExc_TypeError,
893 "mappingproxy() argument must be a mapping, not %s",
894 Py_TYPE(mapping)->tp_name);
895 return -1;
896 }
897 return 0;
898}
899
900static PyObject*
901mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
902{
903 static char *kwlist[] = {"mapping", NULL};
904 PyObject *mapping;
905 mappingproxyobject *mappingproxy;
906
907 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:mappingproxy",
908 kwlist, &mapping))
909 return NULL;
910
911 if (mappingproxy_check_mapping(mapping) == -1)
912 return NULL;
913
914 mappingproxy = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
915 if (mappingproxy == NULL)
916 return NULL;
917 Py_INCREF(mapping);
918 mappingproxy->mapping = mapping;
919 _PyObject_GC_TRACK(mappingproxy);
920 return (PyObject *)mappingproxy;
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000921}
922
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000923PyTypeObject PyDictProxy_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Victor Stinner0db176f2012-04-16 00:16:30 +0200925 "mappingproxy", /* tp_name */
926 sizeof(mappingproxyobject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 0, /* tp_itemsize */
928 /* methods */
Victor Stinner0db176f2012-04-16 00:16:30 +0200929 (destructor)mappingproxy_dealloc, /* tp_dealloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 0, /* tp_print */
931 0, /* tp_getattr */
932 0, /* tp_setattr */
933 0, /* tp_reserved */
Victor Stinner0db176f2012-04-16 00:16:30 +0200934 (reprfunc)mappingproxy_repr, /* tp_repr */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 0, /* tp_as_number */
Victor Stinner0db176f2012-04-16 00:16:30 +0200936 &mappingproxy_as_sequence, /* tp_as_sequence */
937 &mappingproxy_as_mapping, /* tp_as_mapping */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 0, /* tp_hash */
939 0, /* tp_call */
Victor Stinner0db176f2012-04-16 00:16:30 +0200940 (reprfunc)mappingproxy_str, /* tp_str */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyObject_GenericGetAttr, /* tp_getattro */
942 0, /* tp_setattro */
943 0, /* tp_as_buffer */
944 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
945 0, /* tp_doc */
Victor Stinner0db176f2012-04-16 00:16:30 +0200946 mappingproxy_traverse, /* tp_traverse */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 0, /* tp_clear */
Victor Stinner0db176f2012-04-16 00:16:30 +0200948 (richcmpfunc)mappingproxy_richcompare, /* tp_richcompare */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 0, /* tp_weaklistoffset */
Victor Stinner0db176f2012-04-16 00:16:30 +0200950 (getiterfunc)mappingproxy_getiter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 0, /* tp_iternext */
Victor Stinner0db176f2012-04-16 00:16:30 +0200952 mappingproxy_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 0, /* tp_members */
954 0, /* tp_getset */
955 0, /* tp_base */
956 0, /* tp_dict */
957 0, /* tp_descr_get */
958 0, /* tp_descr_set */
Victor Stinner0db176f2012-04-16 00:16:30 +0200959 0, /* tp_dictoffset */
960 0, /* tp_init */
961 0, /* tp_alloc */
962 mappingproxy_new, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000963};
964
965PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200966PyDictProxy_New(PyObject *mapping)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000967{
Victor Stinner0db176f2012-04-16 00:16:30 +0200968 mappingproxyobject *pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000969
Victor Stinner0db176f2012-04-16 00:16:30 +0200970 if (mappingproxy_check_mapping(mapping) == -1)
971 return NULL;
972
973 pp = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 if (pp != NULL) {
Victor Stinner0db176f2012-04-16 00:16:30 +0200975 Py_INCREF(mapping);
976 pp->mapping = mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 _PyObject_GC_TRACK(pp);
978 }
979 return (PyObject *)pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000980}
981
982
983/* --- Wrapper object for "slot" methods --- */
984
985/* This has no reason to be in this file except that adding new files is a
986 bit of a pain */
987
988typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 PyObject_HEAD
990 PyWrapperDescrObject *descr;
991 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992} wrapperobject;
993
Benjamin Petersoneff61f62011-09-01 16:32:31 -0400994#define Wrapper_Check(v) (Py_TYPE(v) == &_PyMethodWrapper_Type)
Mark Dickinson211c6252009-02-01 10:28:51 +0000995
Tim Peters6d6c1a32001-08-02 04:15:00 +0000996static void
997wrapper_dealloc(wrapperobject *wp)
998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyObject_GC_UnTrack(wp);
1000 Py_TRASHCAN_SAFE_BEGIN(wp)
1001 Py_XDECREF(wp->descr);
1002 Py_XDECREF(wp->self);
1003 PyObject_GC_Del(wp);
1004 Py_TRASHCAN_SAFE_END(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001005}
1006
Mark Dickinson211c6252009-02-01 10:28:51 +00001007#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
1008
1009static PyObject *
1010wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +00001011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 int result;
1013 PyObject *v;
1014 PyWrapperDescrObject *a_descr, *b_descr;
Mark Dickinson211c6252009-02-01 10:28:51 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 assert(a != NULL && b != NULL);
Mark Dickinson211c6252009-02-01 10:28:51 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 /* both arguments should be wrapperobjects */
1019 if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
1020 v = Py_NotImplemented;
1021 Py_INCREF(v);
1022 return v;
1023 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 /* compare by descriptor address; if the descriptors are the same,
1026 compare by the objects they're bound to */
1027 a_descr = ((wrapperobject *)a)->descr;
1028 b_descr = ((wrapperobject *)b)->descr;
1029 if (a_descr == b_descr) {
1030 a = ((wrapperobject *)a)->self;
1031 b = ((wrapperobject *)b)->self;
1032 return PyObject_RichCompare(a, b, op);
1033 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 result = a_descr - b_descr;
1036 switch (op) {
1037 case Py_EQ:
1038 v = TEST_COND(result == 0);
1039 break;
1040 case Py_NE:
1041 v = TEST_COND(result != 0);
1042 break;
1043 case Py_LE:
1044 v = TEST_COND(result <= 0);
1045 break;
1046 case Py_GE:
1047 v = TEST_COND(result >= 0);
1048 break;
1049 case Py_LT:
1050 v = TEST_COND(result < 0);
1051 break;
1052 case Py_GT:
1053 v = TEST_COND(result > 0);
1054 break;
1055 default:
1056 PyErr_BadArgument();
1057 return NULL;
1058 }
1059 Py_INCREF(v);
1060 return v;
Armin Rigoc6686b72005-11-07 08:38:00 +00001061}
1062
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001063static Py_hash_t
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001064wrapper_hash(wrapperobject *wp)
1065{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001066 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 x = _Py_HashPointer(wp->descr);
1068 if (x == -1)
1069 return -1;
1070 y = PyObject_Hash(wp->self);
1071 if (y == -1)
1072 return -1;
1073 x = x ^ y;
1074 if (x == -1)
1075 x = -2;
1076 return x;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001077}
1078
Armin Rigoc6686b72005-11-07 08:38:00 +00001079static PyObject *
1080wrapper_repr(wrapperobject *wp)
1081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
1083 wp->descr->d_base->name,
1084 wp->self->ob_type->tp_name,
1085 wp->self);
Armin Rigoc6686b72005-11-07 08:38:00 +00001086}
1087
1088static PyMemberDef wrapper_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
1090 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001091};
1092
1093static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +00001094wrapper_objclass(wrapperobject *wp)
1095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 PyObject *c = (PyObject *)PyDescr_TYPE(wp->descr);
Armin Rigoc6686b72005-11-07 08:38:00 +00001097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 Py_INCREF(c);
1099 return c;
Armin Rigoc6686b72005-11-07 08:38:00 +00001100}
1101
1102static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001103wrapper_name(wrapperobject *wp)
1104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001108}
1109
1110static PyObject *
1111wrapper_doc(wrapperobject *wp)
1112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 const char *s = wp->descr->d_base->doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (s == NULL) {
1116 Py_INCREF(Py_None);
1117 return Py_None;
1118 }
1119 else {
1120 return PyUnicode_FromString(s);
1121 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001122}
1123
Antoine Pitrou9d574812011-12-12 13:47:25 +01001124static PyObject *
1125wrapper_qualname(wrapperobject *wp)
1126{
1127 return descr_get_qualname((PyDescrObject *)wp->descr);
1128}
1129
Guido van Rossum32d34c82001-09-20 21:45:26 +00001130static PyGetSetDef wrapper_getsets[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 {"__objclass__", (getter)wrapper_objclass},
1132 {"__name__", (getter)wrapper_name},
Antoine Pitrou9d574812011-12-12 13:47:25 +01001133 {"__qualname__", (getter)wrapper_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 {"__doc__", (getter)wrapper_doc},
1135 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001136};
1137
1138static PyObject *
1139wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
1140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 wrapperfunc wrapper = wp->descr->d_base->wrapper;
1142 PyObject *self = wp->self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
1145 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
1146 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
1147 }
Guido van Rossumc8e56452001-10-22 00:43:43 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
1150 PyErr_Format(PyExc_TypeError,
1151 "wrapper %s doesn't take keyword arguments",
1152 wp->descr->d_base->name);
1153 return NULL;
1154 }
1155 return (*wrapper)(self, args, wp->descr->d_wrapped);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001156}
1157
Guido van Rossum048eb752001-10-02 21:24:57 +00001158static int
1159wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 wrapperobject *wp = (wrapperobject *)self;
1162 Py_VISIT(wp->descr);
1163 Py_VISIT(wp->self);
1164 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001165}
1166
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001167PyTypeObject _PyMethodWrapper_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1169 "method-wrapper", /* tp_name */
1170 sizeof(wrapperobject), /* tp_basicsize */
1171 0, /* tp_itemsize */
1172 /* methods */
1173 (destructor)wrapper_dealloc, /* tp_dealloc */
1174 0, /* tp_print */
1175 0, /* tp_getattr */
1176 0, /* tp_setattr */
1177 0, /* tp_reserved */
1178 (reprfunc)wrapper_repr, /* tp_repr */
1179 0, /* tp_as_number */
1180 0, /* tp_as_sequence */
1181 0, /* tp_as_mapping */
1182 (hashfunc)wrapper_hash, /* tp_hash */
1183 (ternaryfunc)wrapper_call, /* tp_call */
1184 0, /* tp_str */
1185 PyObject_GenericGetAttr, /* tp_getattro */
1186 0, /* tp_setattro */
1187 0, /* tp_as_buffer */
1188 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1189 0, /* tp_doc */
1190 wrapper_traverse, /* tp_traverse */
1191 0, /* tp_clear */
1192 wrapper_richcompare, /* tp_richcompare */
1193 0, /* tp_weaklistoffset */
1194 0, /* tp_iter */
1195 0, /* tp_iternext */
1196 0, /* tp_methods */
1197 wrapper_members, /* tp_members */
1198 wrapper_getsets, /* tp_getset */
1199 0, /* tp_base */
1200 0, /* tp_dict */
1201 0, /* tp_descr_get */
1202 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001203};
1204
1205PyObject *
1206PyWrapper_New(PyObject *d, PyObject *self)
1207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 wrapperobject *wp;
1209 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1212 descr = (PyWrapperDescrObject *)d;
Victor Stinner3249dec2011-05-01 23:19:15 +02001213 assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +02001214 (PyObject *)PyDescr_TYPE(descr)));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001215
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001216 wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (wp != NULL) {
1218 Py_INCREF(descr);
1219 wp->descr = descr;
1220 Py_INCREF(self);
1221 wp->self = self;
1222 _PyObject_GC_TRACK(wp);
1223 }
1224 return (PyObject *)wp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001225}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001226
1227
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001228/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001229
1230/*
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001231 class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1234 if doc is None and fget is not None and hasattr(fget, "__doc__"):
1235 doc = fget.__doc__
1236 self.__get = fget
1237 self.__set = fset
1238 self.__del = fdel
1239 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 def __get__(self, inst, type=None):
1242 if inst is None:
1243 return self
1244 if self.__get is None:
1245 raise AttributeError, "unreadable attribute"
1246 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 def __set__(self, inst, value):
1249 if self.__set is None:
1250 raise AttributeError, "can't set attribute"
1251 return self.__set(inst, value)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 def __delete__(self, inst):
1254 if self.__del is None:
1255 raise AttributeError, "can't delete attribute"
1256 return self.__del(inst)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001257
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001258*/
1259
1260typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 PyObject_HEAD
1262 PyObject *prop_get;
1263 PyObject *prop_set;
1264 PyObject *prop_del;
1265 PyObject *prop_doc;
1266 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001267} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001268
Christian Heimes0449f632007-12-15 01:27:15 +00001269static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
Benjamin Peterson93964832010-06-28 03:07:10 +00001270 PyObject *);
Christian Heimes0449f632007-12-15 01:27:15 +00001271
Tim Peters66c1a522001-09-24 21:17:50 +00001272static PyMemberDef property_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1274 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1275 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1276 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1277 {0}
Tim Peters66c1a522001-09-24 21:17:50 +00001278};
1279
Christian Heimes0449f632007-12-15 01:27:15 +00001280
Guido van Rossum58da9312007-11-10 23:39:45 +00001281PyDoc_STRVAR(getter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 "Descriptor to change the getter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001283
Neal Norwitz32dde222008-04-15 06:43:13 +00001284static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001285property_getter(PyObject *self, PyObject *getter)
1286{
Benjamin Peterson93964832010-06-28 03:07:10 +00001287 return property_copy(self, getter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001288}
1289
Christian Heimes0449f632007-12-15 01:27:15 +00001290
Guido van Rossum58da9312007-11-10 23:39:45 +00001291PyDoc_STRVAR(setter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001293
Neal Norwitz32dde222008-04-15 06:43:13 +00001294static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001295property_setter(PyObject *self, PyObject *setter)
1296{
Benjamin Peterson93964832010-06-28 03:07:10 +00001297 return property_copy(self, NULL, setter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001298}
1299
Christian Heimes0449f632007-12-15 01:27:15 +00001300
Guido van Rossum58da9312007-11-10 23:39:45 +00001301PyDoc_STRVAR(deleter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 "Descriptor to change the deleter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001303
Neal Norwitz32dde222008-04-15 06:43:13 +00001304static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001305property_deleter(PyObject *self, PyObject *deleter)
1306{
Benjamin Peterson93964832010-06-28 03:07:10 +00001307 return property_copy(self, NULL, NULL, deleter);
Guido van Rossum58da9312007-11-10 23:39:45 +00001308}
1309
1310
Guido van Rossum58da9312007-11-10 23:39:45 +00001311static PyMethodDef property_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 {"getter", property_getter, METH_O, getter_doc},
1313 {"setter", property_setter, METH_O, setter_doc},
1314 {"deleter", property_deleter, METH_O, deleter_doc},
1315 {0}
Guido van Rossum58da9312007-11-10 23:39:45 +00001316};
1317
Tim Peters66c1a522001-09-24 21:17:50 +00001318
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001319static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001320property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 _PyObject_GC_UNTRACK(self);
1325 Py_XDECREF(gs->prop_get);
1326 Py_XDECREF(gs->prop_set);
1327 Py_XDECREF(gs->prop_del);
1328 Py_XDECREF(gs->prop_doc);
1329 self->ob_type->tp_free(self);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001330}
1331
1332static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001333property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if (obj == NULL || obj == Py_None) {
1338 Py_INCREF(self);
1339 return self;
1340 }
1341 if (gs->prop_get == NULL) {
1342 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1343 return NULL;
1344 }
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001345 return PyObject_CallFunctionObjArgs(gs->prop_get, obj, NULL);
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)
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001366 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
1406 Py_DECREF(type);
1407 if (new == NULL)
1408 return NULL;
1409 return new;
Christian Heimes0449f632007-12-15 01:27:15 +00001410}
1411
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001412static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001413property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
1416 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
1417 propertyobject *prop = (propertyobject *)self;
Tim Peters66c1a522001-09-24 21:17:50 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
1420 kwlist, &get, &set, &del, &doc))
1421 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (get == Py_None)
1424 get = NULL;
1425 if (set == Py_None)
1426 set = NULL;
1427 if (del == Py_None)
1428 del = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 Py_XINCREF(get);
1431 Py_XINCREF(set);
1432 Py_XINCREF(del);
1433 Py_XINCREF(doc);
Christian Heimes0449f632007-12-15 01:27:15 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 prop->prop_get = get;
1436 prop->prop_set = set;
1437 prop->prop_del = del;
1438 prop->prop_doc = doc;
1439 prop->getter_doc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 /* if no docstring given and the getter has one, use that one */
1442 if ((doc == NULL || doc == Py_None) && get != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001443 _Py_IDENTIFIER(__doc__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001444 PyObject *get_doc = _PyObject_GetAttrId(get, &PyId___doc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (get_doc) {
1446 if (Py_TYPE(self) == &PyProperty_Type) {
1447 Py_XDECREF(prop->prop_doc);
1448 prop->prop_doc = get_doc;
1449 }
1450 else {
1451 /* If this is a property subclass, put __doc__
1452 in dict of the subclass instance instead,
1453 otherwise it gets shadowed by __doc__ in the
1454 class's dict. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001455 int err = _PyObject_SetAttrId(self, &PyId___doc__, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 Py_DECREF(get_doc);
1457 if (err < 0)
1458 return -1;
1459 }
1460 prop->getter_doc = 1;
1461 }
1462 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1463 PyErr_Clear();
1464 }
1465 else {
1466 return -1;
1467 }
1468 }
1469
1470 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001471}
1472
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001473static PyObject *
1474property_get___isabstractmethod__(propertyobject *prop, void *closure)
1475{
1476 int res = _PyObject_IsAbstract(prop->prop_get);
1477 if (res == -1) {
1478 return NULL;
1479 }
1480 else if (res) {
1481 Py_RETURN_TRUE;
1482 }
1483
1484 res = _PyObject_IsAbstract(prop->prop_set);
1485 if (res == -1) {
1486 return NULL;
1487 }
1488 else if (res) {
1489 Py_RETURN_TRUE;
1490 }
1491
1492 res = _PyObject_IsAbstract(prop->prop_del);
1493 if (res == -1) {
1494 return NULL;
1495 }
1496 else if (res) {
1497 Py_RETURN_TRUE;
1498 }
1499 Py_RETURN_FALSE;
1500}
1501
1502static PyGetSetDef property_getsetlist[] = {
1503 {"__isabstractmethod__",
1504 (getter)property_get___isabstractmethod__, NULL,
1505 NULL,
1506 NULL},
1507 {NULL} /* Sentinel */
1508};
1509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001510PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001511"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1512"\n"
1513"fget is a function to be used for getting an attribute value, and likewise\n"
1514"fset is a function for setting, and fdel a function for del'ing, an\n"
1515"attribute. Typical use is to define a managed attribute x:\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001516"class C(object):\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001517" def getx(self): return self._x\n"
1518" def setx(self, value): self._x = value\n"
1519" def delx(self): del self._x\n"
1520" x = property(getx, setx, delx, \"I'm the 'x' property.\")\n"
1521"\n"
1522"Decorators make defining new properties or modifying existing ones easy:\n"
1523"class C(object):\n"
1524" @property\n"
1525" def x(self): return self._x\n"
1526" @x.setter\n"
1527" def x(self, value): self._x = value\n"
1528" @x.deleter\n"
1529" def x(self): del self._x\n"
1530);
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001531
Guido van Rossum048eb752001-10-02 21:24:57 +00001532static int
1533property_traverse(PyObject *self, visitproc visit, void *arg)
1534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 propertyobject *pp = (propertyobject *)self;
1536 Py_VISIT(pp->prop_get);
1537 Py_VISIT(pp->prop_set);
1538 Py_VISIT(pp->prop_del);
1539 Py_VISIT(pp->prop_doc);
1540 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001541}
1542
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001543PyTypeObject PyProperty_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1545 "property", /* tp_name */
1546 sizeof(propertyobject), /* tp_basicsize */
1547 0, /* tp_itemsize */
1548 /* methods */
1549 property_dealloc, /* tp_dealloc */
1550 0, /* tp_print */
1551 0, /* tp_getattr */
1552 0, /* tp_setattr */
1553 0, /* tp_reserved */
1554 0, /* tp_repr */
1555 0, /* tp_as_number */
1556 0, /* tp_as_sequence */
1557 0, /* tp_as_mapping */
1558 0, /* tp_hash */
1559 0, /* tp_call */
1560 0, /* tp_str */
1561 PyObject_GenericGetAttr, /* tp_getattro */
1562 0, /* tp_setattro */
1563 0, /* tp_as_buffer */
1564 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1565 Py_TPFLAGS_BASETYPE, /* tp_flags */
1566 property_doc, /* tp_doc */
1567 property_traverse, /* tp_traverse */
1568 0, /* tp_clear */
1569 0, /* tp_richcompare */
1570 0, /* tp_weaklistoffset */
1571 0, /* tp_iter */
1572 0, /* tp_iternext */
1573 property_methods, /* tp_methods */
1574 property_members, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001575 property_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 0, /* tp_base */
1577 0, /* tp_dict */
1578 property_descr_get, /* tp_descr_get */
1579 property_descr_set, /* tp_descr_set */
1580 0, /* tp_dictoffset */
1581 property_init, /* tp_init */
1582 PyType_GenericAlloc, /* tp_alloc */
1583 PyType_GenericNew, /* tp_new */
1584 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001585};