blob: da88f8668a8801a86f07e711e39de3575e33d2a1 [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{
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
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100401static PyObject *
402descr_reduce(PyDescrObject *descr)
403{
404 PyObject *builtins;
405 PyObject *getattr;
406 _Py_IDENTIFIER(getattr);
407
408 builtins = PyEval_GetBuiltins();
409 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
410 return Py_BuildValue("O(OO)", getattr, PyDescr_TYPE(descr),
411 PyDescr_NAME(descr));
412}
413
414static PyMethodDef descr_methods[] = {
415 {"__reduce__", (PyCFunction)descr_reduce, METH_NOARGS, NULL},
416 {NULL, NULL}
417};
418
Guido van Rossum6f799372001-09-20 20:46:19 +0000419static PyMemberDef descr_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
421 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
422 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000423};
424
Guido van Rossum32d34c82001-09-20 21:45:26 +0000425static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 {"__doc__", (getter)method_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100427 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 {0}
Guido van Rossum6f799372001-09-20 20:46:19 +0000429};
430
431static PyObject *
432member_get_doc(PyMemberDescrObject *descr, void *closure)
433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (descr->d_member->doc == NULL) {
435 Py_INCREF(Py_None);
436 return Py_None;
437 }
438 return PyUnicode_FromString(descr->d_member->doc);
Guido van Rossum6f799372001-09-20 20:46:19 +0000439}
440
Guido van Rossum32d34c82001-09-20 21:45:26 +0000441static PyGetSetDef member_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 {"__doc__", (getter)member_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100443 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000445};
446
447static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000448getset_get_doc(PyGetSetDescrObject *descr, void *closure)
449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (descr->d_getset->doc == NULL) {
451 Py_INCREF(Py_None);
452 return Py_None;
453 }
454 return PyUnicode_FromString(descr->d_getset->doc);
Guido van Rossum32d34c82001-09-20 21:45:26 +0000455}
456
457static PyGetSetDef getset_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 {"__doc__", (getter)getset_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100459 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 {0}
Guido van Rossum32d34c82001-09-20 21:45:26 +0000461};
462
463static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000464wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (descr->d_base->doc == NULL) {
467 Py_INCREF(Py_None);
468 return Py_None;
469 }
470 return PyUnicode_FromString(descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000471}
472
Armin Rigoc6686b72005-11-07 08:38:00 +0000473static PyGetSetDef wrapperdescr_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 {"__doc__", (getter)wrapperdescr_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100475 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000477};
478
Guido van Rossum048eb752001-10-02 21:24:57 +0000479static int
480descr_traverse(PyObject *self, visitproc visit, void *arg)
481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 PyDescrObject *descr = (PyDescrObject *)self;
483 Py_VISIT(descr->d_type);
484 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000485}
486
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000487PyTypeObject PyMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyVarObject_HEAD_INIT(&PyType_Type, 0)
489 "method_descriptor",
490 sizeof(PyMethodDescrObject),
491 0,
492 (destructor)descr_dealloc, /* tp_dealloc */
493 0, /* tp_print */
494 0, /* tp_getattr */
495 0, /* tp_setattr */
496 0, /* tp_reserved */
497 (reprfunc)method_repr, /* tp_repr */
498 0, /* tp_as_number */
499 0, /* tp_as_sequence */
500 0, /* tp_as_mapping */
501 0, /* tp_hash */
502 (ternaryfunc)methoddescr_call, /* tp_call */
503 0, /* tp_str */
504 PyObject_GenericGetAttr, /* tp_getattro */
505 0, /* tp_setattro */
506 0, /* tp_as_buffer */
507 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
508 0, /* tp_doc */
509 descr_traverse, /* tp_traverse */
510 0, /* tp_clear */
511 0, /* tp_richcompare */
512 0, /* tp_weaklistoffset */
513 0, /* tp_iter */
514 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100515 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 descr_members, /* tp_members */
517 method_getset, /* tp_getset */
518 0, /* tp_base */
519 0, /* tp_dict */
520 (descrgetfunc)method_get, /* tp_descr_get */
521 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000522};
523
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000524/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000525PyTypeObject PyClassMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 PyVarObject_HEAD_INIT(&PyType_Type, 0)
527 "classmethod_descriptor",
528 sizeof(PyMethodDescrObject),
529 0,
530 (destructor)descr_dealloc, /* tp_dealloc */
531 0, /* tp_print */
532 0, /* tp_getattr */
533 0, /* tp_setattr */
534 0, /* tp_reserved */
535 (reprfunc)method_repr, /* tp_repr */
536 0, /* tp_as_number */
537 0, /* tp_as_sequence */
538 0, /* tp_as_mapping */
539 0, /* tp_hash */
540 (ternaryfunc)classmethoddescr_call, /* tp_call */
541 0, /* tp_str */
542 PyObject_GenericGetAttr, /* tp_getattro */
543 0, /* tp_setattro */
544 0, /* tp_as_buffer */
545 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
546 0, /* tp_doc */
547 descr_traverse, /* tp_traverse */
548 0, /* tp_clear */
549 0, /* tp_richcompare */
550 0, /* tp_weaklistoffset */
551 0, /* tp_iter */
552 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100553 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 descr_members, /* tp_members */
555 method_getset, /* tp_getset */
556 0, /* tp_base */
557 0, /* tp_dict */
558 (descrgetfunc)classmethod_get, /* tp_descr_get */
559 0, /* tp_descr_set */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000560};
561
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000562PyTypeObject PyMemberDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 PyVarObject_HEAD_INIT(&PyType_Type, 0)
564 "member_descriptor",
565 sizeof(PyMemberDescrObject),
566 0,
567 (destructor)descr_dealloc, /* tp_dealloc */
568 0, /* tp_print */
569 0, /* tp_getattr */
570 0, /* tp_setattr */
571 0, /* tp_reserved */
572 (reprfunc)member_repr, /* tp_repr */
573 0, /* tp_as_number */
574 0, /* tp_as_sequence */
575 0, /* tp_as_mapping */
576 0, /* tp_hash */
577 0, /* tp_call */
578 0, /* tp_str */
579 PyObject_GenericGetAttr, /* tp_getattro */
580 0, /* tp_setattro */
581 0, /* tp_as_buffer */
582 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
583 0, /* tp_doc */
584 descr_traverse, /* tp_traverse */
585 0, /* tp_clear */
586 0, /* tp_richcompare */
587 0, /* tp_weaklistoffset */
588 0, /* tp_iter */
589 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100590 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 descr_members, /* tp_members */
592 member_getset, /* tp_getset */
593 0, /* tp_base */
594 0, /* tp_dict */
595 (descrgetfunc)member_get, /* tp_descr_get */
596 (descrsetfunc)member_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000597};
598
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000599PyTypeObject PyGetSetDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 PyVarObject_HEAD_INIT(&PyType_Type, 0)
601 "getset_descriptor",
602 sizeof(PyGetSetDescrObject),
603 0,
604 (destructor)descr_dealloc, /* tp_dealloc */
605 0, /* tp_print */
606 0, /* tp_getattr */
607 0, /* tp_setattr */
608 0, /* tp_reserved */
609 (reprfunc)getset_repr, /* tp_repr */
610 0, /* tp_as_number */
611 0, /* tp_as_sequence */
612 0, /* tp_as_mapping */
613 0, /* tp_hash */
614 0, /* tp_call */
615 0, /* tp_str */
616 PyObject_GenericGetAttr, /* tp_getattro */
617 0, /* tp_setattro */
618 0, /* tp_as_buffer */
619 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
620 0, /* tp_doc */
621 descr_traverse, /* tp_traverse */
622 0, /* tp_clear */
623 0, /* tp_richcompare */
624 0, /* tp_weaklistoffset */
625 0, /* tp_iter */
626 0, /* tp_iternext */
627 0, /* tp_methods */
628 descr_members, /* tp_members */
629 getset_getset, /* tp_getset */
630 0, /* tp_base */
631 0, /* tp_dict */
632 (descrgetfunc)getset_get, /* tp_descr_get */
633 (descrsetfunc)getset_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000634};
635
Guido van Rossumf4593e02001-10-03 12:09:30 +0000636PyTypeObject PyWrapperDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 PyVarObject_HEAD_INIT(&PyType_Type, 0)
638 "wrapper_descriptor",
639 sizeof(PyWrapperDescrObject),
640 0,
641 (destructor)descr_dealloc, /* tp_dealloc */
642 0, /* tp_print */
643 0, /* tp_getattr */
644 0, /* tp_setattr */
645 0, /* tp_reserved */
646 (reprfunc)wrapperdescr_repr, /* tp_repr */
647 0, /* tp_as_number */
648 0, /* tp_as_sequence */
649 0, /* tp_as_mapping */
650 0, /* tp_hash */
651 (ternaryfunc)wrapperdescr_call, /* tp_call */
652 0, /* tp_str */
653 PyObject_GenericGetAttr, /* tp_getattro */
654 0, /* tp_setattro */
655 0, /* tp_as_buffer */
656 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
657 0, /* tp_doc */
658 descr_traverse, /* tp_traverse */
659 0, /* tp_clear */
660 0, /* tp_richcompare */
661 0, /* tp_weaklistoffset */
662 0, /* tp_iter */
663 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100664 descr_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 descr_members, /* tp_members */
666 wrapperdescr_getset, /* tp_getset */
667 0, /* tp_base */
668 0, /* tp_dict */
669 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
670 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000671};
672
673static PyDescrObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000674descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 PyDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
679 if (descr != NULL) {
680 Py_XINCREF(type);
681 descr->d_type = type;
682 descr->d_name = PyUnicode_InternFromString(name);
683 if (descr->d_name == NULL) {
684 Py_DECREF(descr);
685 descr = NULL;
686 }
Benjamin Petersonf2fe7f02011-12-17 08:02:20 -0500687 else {
688 descr->d_qualname = NULL;
689 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 }
691 return descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000692}
693
694PyObject *
695PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 PyMethodDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
700 type, method->ml_name);
701 if (descr != NULL)
702 descr->d_method = method;
703 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704}
705
706PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000707PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyMethodDescrObject *descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
712 type, method->ml_name);
713 if (descr != NULL)
714 descr->d_method = method;
715 return (PyObject *)descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000716}
717
718PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000719PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 PyMemberDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
724 type, member->name);
725 if (descr != NULL)
726 descr->d_member = member;
727 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728}
729
730PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000731PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 PyGetSetDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
736 type, getset->name);
737 if (descr != NULL)
738 descr->d_getset = getset;
739 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740}
741
742PyObject *
743PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
748 type, base->name);
749 if (descr != NULL) {
750 descr->d_base = base;
751 descr->d_wrapped = wrapped;
752 }
753 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000754}
755
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756
Victor Stinner0db176f2012-04-16 00:16:30 +0200757/* --- mappingproxy: read-only proxy for mappings --- */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000758
759/* This has no reason to be in this file except that adding new files is a
760 bit of a pain */
761
762typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 PyObject_HEAD
Victor Stinner0db176f2012-04-16 00:16:30 +0200764 PyObject *mapping;
765} mappingproxyobject;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000766
Martin v. Löwis18e16552006-02-15 17:27:45 +0000767static Py_ssize_t
Victor Stinner0db176f2012-04-16 00:16:30 +0200768mappingproxy_len(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000769{
Victor Stinner0db176f2012-04-16 00:16:30 +0200770 return PyObject_Size(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771}
772
773static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200774mappingproxy_getitem(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000775{
Victor Stinner0db176f2012-04-16 00:16:30 +0200776 return PyObject_GetItem(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000777}
778
Victor Stinner0db176f2012-04-16 00:16:30 +0200779static PyMappingMethods mappingproxy_as_mapping = {
780 (lenfunc)mappingproxy_len, /* mp_length */
781 (binaryfunc)mappingproxy_getitem, /* mp_subscript */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 0, /* mp_ass_subscript */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000783};
784
785static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200786mappingproxy_contains(mappingproxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000787{
Victor Stinner0db176f2012-04-16 00:16:30 +0200788 if (PyDict_CheckExact(pp->mapping))
789 return PyDict_Contains(pp->mapping, key);
790 else
791 return PySequence_Contains(pp->mapping, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000792}
793
Victor Stinner0db176f2012-04-16 00:16:30 +0200794static PySequenceMethods mappingproxy_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 0, /* sq_length */
796 0, /* sq_concat */
797 0, /* sq_repeat */
798 0, /* sq_item */
799 0, /* sq_slice */
800 0, /* sq_ass_item */
801 0, /* sq_ass_slice */
Victor Stinner0db176f2012-04-16 00:16:30 +0200802 (objobjproc)mappingproxy_contains, /* sq_contains */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 0, /* sq_inplace_concat */
804 0, /* sq_inplace_repeat */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805};
806
807static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200808mappingproxy_get(mappingproxyobject *pp, PyObject *args)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyObject *key, *def = Py_None;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200811 _Py_IDENTIFIER(get);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
814 return NULL;
Victor Stinner0db176f2012-04-16 00:16:30 +0200815 return _PyObject_CallMethodId(pp->mapping, &PyId_get, "(OO)", key, def);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000816}
817
818static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200819mappingproxy_keys(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000820{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200821 _Py_IDENTIFIER(keys);
Victor Stinner0db176f2012-04-16 00:16:30 +0200822 return _PyObject_CallMethodId(pp->mapping, &PyId_keys, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000823}
824
825static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200826mappingproxy_values(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000827{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200828 _Py_IDENTIFIER(values);
Victor Stinner0db176f2012-04-16 00:16:30 +0200829 return _PyObject_CallMethodId(pp->mapping, &PyId_values, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000830}
831
832static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200833mappingproxy_items(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000834{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200835 _Py_IDENTIFIER(items);
Victor Stinner0db176f2012-04-16 00:16:30 +0200836 return _PyObject_CallMethodId(pp->mapping, &PyId_items, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000837}
838
839static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200840mappingproxy_copy(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000841{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200842 _Py_IDENTIFIER(copy);
Victor Stinner0db176f2012-04-16 00:16:30 +0200843 return _PyObject_CallMethodId(pp->mapping, &PyId_copy, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000844}
845
Victor Stinner0db176f2012-04-16 00:16:30 +0200846/* WARNING: mappingproxy methods must not give access
847 to the underlying mapping */
848
849static PyMethodDef mappingproxy_methods[] = {
850 {"get", (PyCFunction)mappingproxy_get, METH_VARARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
Victor Stinner0db176f2012-04-16 00:16:30 +0200852 " d defaults to None.")},
853 {"keys", (PyCFunction)mappingproxy_keys, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyDoc_STR("D.keys() -> list of D's keys")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200855 {"values", (PyCFunction)mappingproxy_values, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 PyDoc_STR("D.values() -> list of D's values")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200857 {"items", (PyCFunction)mappingproxy_items, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Victor Stinner0db176f2012-04-16 00:16:30 +0200859 {"copy", (PyCFunction)mappingproxy_copy, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PyDoc_STR("D.copy() -> a shallow copy of D")},
861 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000862};
863
864static void
Victor Stinner0db176f2012-04-16 00:16:30 +0200865mappingproxy_dealloc(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 _PyObject_GC_UNTRACK(pp);
Victor Stinner0db176f2012-04-16 00:16:30 +0200868 Py_DECREF(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000870}
871
872static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200873mappingproxy_getiter(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000874{
Victor Stinner0db176f2012-04-16 00:16:30 +0200875 return PyObject_GetIter(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000876}
877
Neil Schemenauer26775122001-10-21 22:26:43 +0000878static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200879mappingproxy_str(mappingproxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880{
Victor Stinner0db176f2012-04-16 00:16:30 +0200881 return PyObject_Str(pp->mapping);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882}
883
Ezio Melottiac53ab62010-12-18 14:59:43 +0000884static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200885mappingproxy_repr(mappingproxyobject *pp)
Ezio Melottiac53ab62010-12-18 14:59:43 +0000886{
Victor Stinner0db176f2012-04-16 00:16:30 +0200887 return PyUnicode_FromFormat("mappingproxy(%R)", pp->mapping);
Ezio Melottiac53ab62010-12-18 14:59:43 +0000888}
889
Guido van Rossum048eb752001-10-02 21:24:57 +0000890static int
Victor Stinner0db176f2012-04-16 00:16:30 +0200891mappingproxy_traverse(PyObject *self, visitproc visit, void *arg)
Guido van Rossum048eb752001-10-02 21:24:57 +0000892{
Victor Stinner0db176f2012-04-16 00:16:30 +0200893 mappingproxyobject *pp = (mappingproxyobject *)self;
894 Py_VISIT(pp->mapping);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000896}
897
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000898static PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200899mappingproxy_richcompare(mappingproxyobject *v, PyObject *w, int op)
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000900{
Victor Stinner0db176f2012-04-16 00:16:30 +0200901 return PyObject_RichCompare(v->mapping, w, op);
902}
903
904static int
905mappingproxy_check_mapping(PyObject *mapping)
906{
907 if (!PyMapping_Check(mapping)
908 || PyList_Check(mapping)
909 || PyTuple_Check(mapping)) {
910 PyErr_Format(PyExc_TypeError,
911 "mappingproxy() argument must be a mapping, not %s",
912 Py_TYPE(mapping)->tp_name);
913 return -1;
914 }
915 return 0;
916}
917
918static PyObject*
919mappingproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
920{
921 static char *kwlist[] = {"mapping", NULL};
922 PyObject *mapping;
923 mappingproxyobject *mappingproxy;
924
925 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:mappingproxy",
926 kwlist, &mapping))
927 return NULL;
928
929 if (mappingproxy_check_mapping(mapping) == -1)
930 return NULL;
931
932 mappingproxy = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
933 if (mappingproxy == NULL)
934 return NULL;
935 Py_INCREF(mapping);
936 mappingproxy->mapping = mapping;
937 _PyObject_GC_TRACK(mappingproxy);
938 return (PyObject *)mappingproxy;
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000939}
940
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000941PyTypeObject PyDictProxy_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Victor Stinner0db176f2012-04-16 00:16:30 +0200943 "mappingproxy", /* tp_name */
944 sizeof(mappingproxyobject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 0, /* tp_itemsize */
946 /* methods */
Victor Stinner0db176f2012-04-16 00:16:30 +0200947 (destructor)mappingproxy_dealloc, /* tp_dealloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 0, /* tp_print */
949 0, /* tp_getattr */
950 0, /* tp_setattr */
951 0, /* tp_reserved */
Victor Stinner0db176f2012-04-16 00:16:30 +0200952 (reprfunc)mappingproxy_repr, /* tp_repr */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 0, /* tp_as_number */
Victor Stinner0db176f2012-04-16 00:16:30 +0200954 &mappingproxy_as_sequence, /* tp_as_sequence */
955 &mappingproxy_as_mapping, /* tp_as_mapping */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 0, /* tp_hash */
957 0, /* tp_call */
Victor Stinner0db176f2012-04-16 00:16:30 +0200958 (reprfunc)mappingproxy_str, /* tp_str */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 PyObject_GenericGetAttr, /* tp_getattro */
960 0, /* tp_setattro */
961 0, /* tp_as_buffer */
962 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
963 0, /* tp_doc */
Victor Stinner0db176f2012-04-16 00:16:30 +0200964 mappingproxy_traverse, /* tp_traverse */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 0, /* tp_clear */
Victor Stinner0db176f2012-04-16 00:16:30 +0200966 (richcmpfunc)mappingproxy_richcompare, /* tp_richcompare */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 0, /* tp_weaklistoffset */
Victor Stinner0db176f2012-04-16 00:16:30 +0200968 (getiterfunc)mappingproxy_getiter, /* tp_iter */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 0, /* tp_iternext */
Victor Stinner0db176f2012-04-16 00:16:30 +0200970 mappingproxy_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 0, /* tp_members */
972 0, /* tp_getset */
973 0, /* tp_base */
974 0, /* tp_dict */
975 0, /* tp_descr_get */
976 0, /* tp_descr_set */
Victor Stinner0db176f2012-04-16 00:16:30 +0200977 0, /* tp_dictoffset */
978 0, /* tp_init */
979 0, /* tp_alloc */
980 mappingproxy_new, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000981};
982
983PyObject *
Victor Stinner0db176f2012-04-16 00:16:30 +0200984PyDictProxy_New(PyObject *mapping)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000985{
Victor Stinner0db176f2012-04-16 00:16:30 +0200986 mappingproxyobject *pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000987
Victor Stinner0db176f2012-04-16 00:16:30 +0200988 if (mappingproxy_check_mapping(mapping) == -1)
989 return NULL;
990
991 pp = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 if (pp != NULL) {
Victor Stinner0db176f2012-04-16 00:16:30 +0200993 Py_INCREF(mapping);
994 pp->mapping = mapping;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 _PyObject_GC_TRACK(pp);
996 }
997 return (PyObject *)pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998}
999
1000
1001/* --- Wrapper object for "slot" methods --- */
1002
1003/* This has no reason to be in this file except that adding new files is a
1004 bit of a pain */
1005
1006typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 PyObject_HEAD
1008 PyWrapperDescrObject *descr;
1009 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001010} wrapperobject;
1011
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001012#define Wrapper_Check(v) (Py_TYPE(v) == &_PyMethodWrapper_Type)
Mark Dickinson211c6252009-02-01 10:28:51 +00001013
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014static void
1015wrapper_dealloc(wrapperobject *wp)
1016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 PyObject_GC_UnTrack(wp);
1018 Py_TRASHCAN_SAFE_BEGIN(wp)
1019 Py_XDECREF(wp->descr);
1020 Py_XDECREF(wp->self);
1021 PyObject_GC_Del(wp);
1022 Py_TRASHCAN_SAFE_END(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001023}
1024
Mark Dickinson211c6252009-02-01 10:28:51 +00001025#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
1026
1027static PyObject *
1028wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +00001029{
Victor Stinner6f75a3e2013-05-08 00:44:15 +02001030 Py_intptr_t result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyObject *v;
1032 PyWrapperDescrObject *a_descr, *b_descr;
Mark Dickinson211c6252009-02-01 10:28:51 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 assert(a != NULL && b != NULL);
Mark Dickinson211c6252009-02-01 10:28:51 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 /* both arguments should be wrapperobjects */
1037 if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
1038 v = Py_NotImplemented;
1039 Py_INCREF(v);
1040 return v;
1041 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 /* compare by descriptor address; if the descriptors are the same,
1044 compare by the objects they're bound to */
1045 a_descr = ((wrapperobject *)a)->descr;
1046 b_descr = ((wrapperobject *)b)->descr;
1047 if (a_descr == b_descr) {
1048 a = ((wrapperobject *)a)->self;
1049 b = ((wrapperobject *)b)->self;
1050 return PyObject_RichCompare(a, b, op);
1051 }
Mark Dickinson211c6252009-02-01 10:28:51 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 result = a_descr - b_descr;
1054 switch (op) {
1055 case Py_EQ:
1056 v = TEST_COND(result == 0);
1057 break;
1058 case Py_NE:
1059 v = TEST_COND(result != 0);
1060 break;
1061 case Py_LE:
1062 v = TEST_COND(result <= 0);
1063 break;
1064 case Py_GE:
1065 v = TEST_COND(result >= 0);
1066 break;
1067 case Py_LT:
1068 v = TEST_COND(result < 0);
1069 break;
1070 case Py_GT:
1071 v = TEST_COND(result > 0);
1072 break;
1073 default:
1074 PyErr_BadArgument();
1075 return NULL;
1076 }
1077 Py_INCREF(v);
1078 return v;
Armin Rigoc6686b72005-11-07 08:38:00 +00001079}
1080
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001081static Py_hash_t
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001082wrapper_hash(wrapperobject *wp)
1083{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001084 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 x = _Py_HashPointer(wp->descr);
1086 if (x == -1)
1087 return -1;
1088 y = PyObject_Hash(wp->self);
1089 if (y == -1)
1090 return -1;
1091 x = x ^ y;
1092 if (x == -1)
1093 x = -2;
1094 return x;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001095}
1096
Armin Rigoc6686b72005-11-07 08:38:00 +00001097static PyObject *
1098wrapper_repr(wrapperobject *wp)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
1101 wp->descr->d_base->name,
1102 wp->self->ob_type->tp_name,
1103 wp->self);
Armin Rigoc6686b72005-11-07 08:38:00 +00001104}
1105
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001106static PyObject *
1107wrapper_reduce(wrapperobject *wp)
1108{
1109 PyObject *builtins;
1110 PyObject *getattr;
1111 _Py_IDENTIFIER(getattr);
1112
1113 builtins = PyEval_GetBuiltins();
1114 getattr = _PyDict_GetItemId(builtins, &PyId_getattr);
1115 return Py_BuildValue("O(OO)", getattr, wp->self, PyDescr_NAME(wp->descr));
1116}
1117
1118static PyMethodDef wrapper_methods[] = {
1119 {"__reduce__", (PyCFunction)wrapper_reduce, METH_NOARGS, NULL},
1120 {NULL, NULL}
1121};
1122
Armin Rigoc6686b72005-11-07 08:38:00 +00001123static PyMemberDef wrapper_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
1125 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001126};
1127
1128static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +00001129wrapper_objclass(wrapperobject *wp)
1130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 PyObject *c = (PyObject *)PyDescr_TYPE(wp->descr);
Armin Rigoc6686b72005-11-07 08:38:00 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 Py_INCREF(c);
1134 return c;
Armin Rigoc6686b72005-11-07 08:38:00 +00001135}
1136
1137static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001138wrapper_name(wrapperobject *wp)
1139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001143}
1144
1145static PyObject *
1146wrapper_doc(wrapperobject *wp)
1147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 const char *s = wp->descr->d_base->doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 if (s == NULL) {
1151 Py_INCREF(Py_None);
1152 return Py_None;
1153 }
1154 else {
1155 return PyUnicode_FromString(s);
1156 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001157}
1158
Antoine Pitrou9d574812011-12-12 13:47:25 +01001159static PyObject *
1160wrapper_qualname(wrapperobject *wp)
1161{
1162 return descr_get_qualname((PyDescrObject *)wp->descr);
1163}
1164
Guido van Rossum32d34c82001-09-20 21:45:26 +00001165static PyGetSetDef wrapper_getsets[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 {"__objclass__", (getter)wrapper_objclass},
1167 {"__name__", (getter)wrapper_name},
Antoine Pitrou9d574812011-12-12 13:47:25 +01001168 {"__qualname__", (getter)wrapper_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 {"__doc__", (getter)wrapper_doc},
1170 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001171};
1172
1173static PyObject *
1174wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
1175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 wrapperfunc wrapper = wp->descr->d_base->wrapper;
1177 PyObject *self = wp->self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
1180 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
1181 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
1182 }
Guido van Rossumc8e56452001-10-22 00:43:43 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
1185 PyErr_Format(PyExc_TypeError,
1186 "wrapper %s doesn't take keyword arguments",
1187 wp->descr->d_base->name);
1188 return NULL;
1189 }
1190 return (*wrapper)(self, args, wp->descr->d_wrapped);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001191}
1192
Guido van Rossum048eb752001-10-02 21:24:57 +00001193static int
1194wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 wrapperobject *wp = (wrapperobject *)self;
1197 Py_VISIT(wp->descr);
1198 Py_VISIT(wp->self);
1199 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001200}
1201
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001202PyTypeObject _PyMethodWrapper_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1204 "method-wrapper", /* tp_name */
1205 sizeof(wrapperobject), /* tp_basicsize */
1206 0, /* tp_itemsize */
1207 /* methods */
1208 (destructor)wrapper_dealloc, /* tp_dealloc */
1209 0, /* tp_print */
1210 0, /* tp_getattr */
1211 0, /* tp_setattr */
1212 0, /* tp_reserved */
1213 (reprfunc)wrapper_repr, /* tp_repr */
1214 0, /* tp_as_number */
1215 0, /* tp_as_sequence */
1216 0, /* tp_as_mapping */
1217 (hashfunc)wrapper_hash, /* tp_hash */
1218 (ternaryfunc)wrapper_call, /* tp_call */
1219 0, /* tp_str */
1220 PyObject_GenericGetAttr, /* tp_getattro */
1221 0, /* tp_setattro */
1222 0, /* tp_as_buffer */
1223 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1224 0, /* tp_doc */
1225 wrapper_traverse, /* tp_traverse */
1226 0, /* tp_clear */
1227 wrapper_richcompare, /* tp_richcompare */
1228 0, /* tp_weaklistoffset */
1229 0, /* tp_iter */
1230 0, /* tp_iternext */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001231 wrapper_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 wrapper_members, /* tp_members */
1233 wrapper_getsets, /* tp_getset */
1234 0, /* tp_base */
1235 0, /* tp_dict */
1236 0, /* tp_descr_get */
1237 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001238};
1239
1240PyObject *
1241PyWrapper_New(PyObject *d, PyObject *self)
1242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 wrapperobject *wp;
1244 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1247 descr = (PyWrapperDescrObject *)d;
Victor Stinner3249dec2011-05-01 23:19:15 +02001248 assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +02001249 (PyObject *)PyDescr_TYPE(descr)));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001250
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001251 wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (wp != NULL) {
1253 Py_INCREF(descr);
1254 wp->descr = descr;
1255 Py_INCREF(self);
1256 wp->self = self;
1257 _PyObject_GC_TRACK(wp);
1258 }
1259 return (PyObject *)wp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001260}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001261
1262
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001263/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001264
1265/*
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001266 class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1269 if doc is None and fget is not None and hasattr(fget, "__doc__"):
1270 doc = fget.__doc__
1271 self.__get = fget
1272 self.__set = fset
1273 self.__del = fdel
1274 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 def __get__(self, inst, type=None):
1277 if inst is None:
1278 return self
1279 if self.__get is None:
1280 raise AttributeError, "unreadable attribute"
1281 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 def __set__(self, inst, value):
1284 if self.__set is None:
1285 raise AttributeError, "can't set attribute"
1286 return self.__set(inst, value)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 def __delete__(self, inst):
1289 if self.__del is None:
1290 raise AttributeError, "can't delete attribute"
1291 return self.__del(inst)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001292
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001293*/
1294
1295typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 PyObject_HEAD
1297 PyObject *prop_get;
1298 PyObject *prop_set;
1299 PyObject *prop_del;
1300 PyObject *prop_doc;
1301 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001302} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001303
Christian Heimes0449f632007-12-15 01:27:15 +00001304static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
Benjamin Peterson93964832010-06-28 03:07:10 +00001305 PyObject *);
Christian Heimes0449f632007-12-15 01:27:15 +00001306
Tim Peters66c1a522001-09-24 21:17:50 +00001307static PyMemberDef property_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1309 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1310 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1311 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1312 {0}
Tim Peters66c1a522001-09-24 21:17:50 +00001313};
1314
Christian Heimes0449f632007-12-15 01:27:15 +00001315
Guido van Rossum58da9312007-11-10 23:39:45 +00001316PyDoc_STRVAR(getter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 "Descriptor to change the getter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001318
Neal Norwitz32dde222008-04-15 06:43:13 +00001319static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001320property_getter(PyObject *self, PyObject *getter)
1321{
Benjamin Peterson93964832010-06-28 03:07:10 +00001322 return property_copy(self, getter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001323}
1324
Christian Heimes0449f632007-12-15 01:27:15 +00001325
Guido van Rossum58da9312007-11-10 23:39:45 +00001326PyDoc_STRVAR(setter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001328
Neal Norwitz32dde222008-04-15 06:43:13 +00001329static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001330property_setter(PyObject *self, PyObject *setter)
1331{
Benjamin Peterson93964832010-06-28 03:07:10 +00001332 return property_copy(self, NULL, setter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001333}
1334
Christian Heimes0449f632007-12-15 01:27:15 +00001335
Guido van Rossum58da9312007-11-10 23:39:45 +00001336PyDoc_STRVAR(deleter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 "Descriptor to change the deleter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001338
Neal Norwitz32dde222008-04-15 06:43:13 +00001339static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001340property_deleter(PyObject *self, PyObject *deleter)
1341{
Benjamin Peterson93964832010-06-28 03:07:10 +00001342 return property_copy(self, NULL, NULL, deleter);
Guido van Rossum58da9312007-11-10 23:39:45 +00001343}
1344
1345
Guido van Rossum58da9312007-11-10 23:39:45 +00001346static PyMethodDef property_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 {"getter", property_getter, METH_O, getter_doc},
1348 {"setter", property_setter, METH_O, setter_doc},
1349 {"deleter", property_deleter, METH_O, deleter_doc},
1350 {0}
Guido van Rossum58da9312007-11-10 23:39:45 +00001351};
1352
Tim Peters66c1a522001-09-24 21:17:50 +00001353
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001354static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001355property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 _PyObject_GC_UNTRACK(self);
1360 Py_XDECREF(gs->prop_get);
1361 Py_XDECREF(gs->prop_set);
1362 Py_XDECREF(gs->prop_del);
1363 Py_XDECREF(gs->prop_doc);
1364 self->ob_type->tp_free(self);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001365}
1366
1367static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001368property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (obj == NULL || obj == Py_None) {
1373 Py_INCREF(self);
1374 return self;
1375 }
1376 if (gs->prop_get == NULL) {
1377 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1378 return NULL;
1379 }
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001380 return PyObject_CallFunctionObjArgs(gs->prop_get, obj, NULL);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001381}
1382
1383static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001384property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 propertyobject *gs = (propertyobject *)self;
1387 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 if (value == NULL)
1390 func = gs->prop_del;
1391 else
1392 func = gs->prop_set;
1393 if (func == NULL) {
1394 PyErr_SetString(PyExc_AttributeError,
1395 value == NULL ?
1396 "can't delete attribute" :
1397 "can't set attribute");
1398 return -1;
1399 }
1400 if (value == NULL)
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001401 res = PyObject_CallFunctionObjArgs(func, obj, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 else
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001403 res = PyObject_CallFunctionObjArgs(func, obj, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 if (res == NULL)
1405 return -1;
1406 Py_DECREF(res);
1407 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001408}
1409
Christian Heimes0449f632007-12-15 01:27:15 +00001410static PyObject *
Benjamin Peterson93964832010-06-28 03:07:10 +00001411property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
Christian Heimes0449f632007-12-15 01:27:15 +00001412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 propertyobject *pold = (propertyobject *)old;
Benjamin Peterson93964832010-06-28 03:07:10 +00001414 PyObject *new, *type, *doc;
Christian Heimes0449f632007-12-15 01:27:15 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 type = PyObject_Type(old);
1417 if (type == NULL)
1418 return NULL;
Christian Heimes0449f632007-12-15 01:27:15 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (get == NULL || get == Py_None) {
1421 Py_XDECREF(get);
1422 get = pold->prop_get ? pold->prop_get : Py_None;
1423 }
1424 if (set == NULL || set == Py_None) {
1425 Py_XDECREF(set);
1426 set = pold->prop_set ? pold->prop_set : Py_None;
1427 }
1428 if (del == NULL || del == Py_None) {
1429 Py_XDECREF(del);
1430 del = pold->prop_del ? pold->prop_del : Py_None;
1431 }
Benjamin Peterson93964832010-06-28 03:07:10 +00001432 if (pold->getter_doc && get != Py_None) {
1433 /* make _init use __doc__ from getter */
1434 doc = Py_None;
1435 }
1436 else {
1437 doc = pold->prop_doc ? pold->prop_doc : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 }
R. David Murrayb18500d2009-05-04 22:59:07 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
1441 Py_DECREF(type);
1442 if (new == NULL)
1443 return NULL;
1444 return new;
Christian Heimes0449f632007-12-15 01:27:15 +00001445}
1446
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001447static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001448property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
1451 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
1452 propertyobject *prop = (propertyobject *)self;
Tim Peters66c1a522001-09-24 21:17:50 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
1455 kwlist, &get, &set, &del, &doc))
1456 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 if (get == Py_None)
1459 get = NULL;
1460 if (set == Py_None)
1461 set = NULL;
1462 if (del == Py_None)
1463 del = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 Py_XINCREF(get);
1466 Py_XINCREF(set);
1467 Py_XINCREF(del);
1468 Py_XINCREF(doc);
Christian Heimes0449f632007-12-15 01:27:15 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 prop->prop_get = get;
1471 prop->prop_set = set;
1472 prop->prop_del = del;
1473 prop->prop_doc = doc;
1474 prop->getter_doc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 /* if no docstring given and the getter has one, use that one */
1477 if ((doc == NULL || doc == Py_None) && get != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001478 _Py_IDENTIFIER(__doc__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001479 PyObject *get_doc = _PyObject_GetAttrId(get, &PyId___doc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 if (get_doc) {
1481 if (Py_TYPE(self) == &PyProperty_Type) {
1482 Py_XDECREF(prop->prop_doc);
1483 prop->prop_doc = get_doc;
1484 }
1485 else {
1486 /* If this is a property subclass, put __doc__
1487 in dict of the subclass instance instead,
1488 otherwise it gets shadowed by __doc__ in the
1489 class's dict. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001490 int err = _PyObject_SetAttrId(self, &PyId___doc__, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 Py_DECREF(get_doc);
1492 if (err < 0)
1493 return -1;
1494 }
1495 prop->getter_doc = 1;
1496 }
1497 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1498 PyErr_Clear();
1499 }
1500 else {
1501 return -1;
1502 }
1503 }
1504
1505 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001506}
1507
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001508static PyObject *
1509property_get___isabstractmethod__(propertyobject *prop, void *closure)
1510{
1511 int res = _PyObject_IsAbstract(prop->prop_get);
1512 if (res == -1) {
1513 return NULL;
1514 }
1515 else if (res) {
1516 Py_RETURN_TRUE;
1517 }
1518
1519 res = _PyObject_IsAbstract(prop->prop_set);
1520 if (res == -1) {
1521 return NULL;
1522 }
1523 else if (res) {
1524 Py_RETURN_TRUE;
1525 }
1526
1527 res = _PyObject_IsAbstract(prop->prop_del);
1528 if (res == -1) {
1529 return NULL;
1530 }
1531 else if (res) {
1532 Py_RETURN_TRUE;
1533 }
1534 Py_RETURN_FALSE;
1535}
1536
1537static PyGetSetDef property_getsetlist[] = {
1538 {"__isabstractmethod__",
1539 (getter)property_get___isabstractmethod__, NULL,
1540 NULL,
1541 NULL},
1542 {NULL} /* Sentinel */
1543};
1544
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001545PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001546"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1547"\n"
1548"fget is a function to be used for getting an attribute value, and likewise\n"
1549"fset is a function for setting, and fdel a function for del'ing, an\n"
1550"attribute. Typical use is to define a managed attribute x:\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001551"class C(object):\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001552" def getx(self): return self._x\n"
1553" def setx(self, value): self._x = value\n"
1554" def delx(self): del self._x\n"
1555" x = property(getx, setx, delx, \"I'm the 'x' property.\")\n"
1556"\n"
1557"Decorators make defining new properties or modifying existing ones easy:\n"
1558"class C(object):\n"
1559" @property\n"
1560" def x(self): return self._x\n"
1561" @x.setter\n"
1562" def x(self, value): self._x = value\n"
1563" @x.deleter\n"
1564" def x(self): del self._x\n"
1565);
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001566
Guido van Rossum048eb752001-10-02 21:24:57 +00001567static int
1568property_traverse(PyObject *self, visitproc visit, void *arg)
1569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 propertyobject *pp = (propertyobject *)self;
1571 Py_VISIT(pp->prop_get);
1572 Py_VISIT(pp->prop_set);
1573 Py_VISIT(pp->prop_del);
1574 Py_VISIT(pp->prop_doc);
1575 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001576}
1577
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001578PyTypeObject PyProperty_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1580 "property", /* tp_name */
1581 sizeof(propertyobject), /* tp_basicsize */
1582 0, /* tp_itemsize */
1583 /* methods */
1584 property_dealloc, /* tp_dealloc */
1585 0, /* tp_print */
1586 0, /* tp_getattr */
1587 0, /* tp_setattr */
1588 0, /* tp_reserved */
1589 0, /* tp_repr */
1590 0, /* tp_as_number */
1591 0, /* tp_as_sequence */
1592 0, /* tp_as_mapping */
1593 0, /* tp_hash */
1594 0, /* tp_call */
1595 0, /* tp_str */
1596 PyObject_GenericGetAttr, /* tp_getattro */
1597 0, /* tp_setattro */
1598 0, /* tp_as_buffer */
1599 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1600 Py_TPFLAGS_BASETYPE, /* tp_flags */
1601 property_doc, /* tp_doc */
1602 property_traverse, /* tp_traverse */
1603 0, /* tp_clear */
1604 0, /* tp_richcompare */
1605 0, /* tp_weaklistoffset */
1606 0, /* tp_iter */
1607 0, /* tp_iternext */
1608 property_methods, /* tp_methods */
1609 property_members, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001610 property_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 0, /* tp_base */
1612 0, /* tp_dict */
1613 property_descr_get, /* tp_descr_get */
1614 property_descr_set, /* tp_descr_set */
1615 0, /* tp_dictoffset */
1616 property_init, /* tp_init */
1617 PyType_GenericAlloc, /* tp_alloc */
1618 PyType_GenericNew, /* tp_new */
1619 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001620};