blob: 1268df9c1614afd217ba7fd98c85a64a7f947bb0 [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 PyObject *func, *result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 func = PyCFunction_New(descr->d_method, (PyObject *)PyDescr_TYPE(descr));
263 if (func == NULL)
264 return NULL;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 result = PyEval_CallObjectWithKeywords(func, args, kwds);
267 Py_DECREF(func);
268 return result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000269}
270
271static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000272wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 Py_ssize_t argc;
275 PyObject *self, *func, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 /* Make sure that the first argument is acceptable as 'self' */
278 assert(PyTuple_Check(args));
279 argc = PyTuple_GET_SIZE(args);
280 if (argc < 1) {
281 PyErr_Format(PyExc_TypeError,
282 "descriptor '%V' of '%.100s' "
283 "object needs an argument",
284 descr_name((PyDescrObject *)descr), "?",
285 PyDescr_TYPE(descr)->tp_name);
286 return NULL;
287 }
288 self = PyTuple_GET_ITEM(args, 0);
Victor Stinner3249dec2011-05-01 23:19:15 +0200289 if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +0200290 (PyObject *)PyDescr_TYPE(descr))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 PyErr_Format(PyExc_TypeError,
292 "descriptor '%V' "
293 "requires a '%.100s' object "
294 "but received a '%.100s'",
295 descr_name((PyDescrObject *)descr), "?",
296 PyDescr_TYPE(descr)->tp_name,
297 self->ob_type->tp_name);
298 return NULL;
299 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 func = PyWrapper_New((PyObject *)descr, self);
302 if (func == NULL)
303 return NULL;
304 args = PyTuple_GetSlice(args, 1, argc);
305 if (args == NULL) {
306 Py_DECREF(func);
307 return NULL;
308 }
309 result = PyEval_CallObjectWithKeywords(func, args, kwds);
310 Py_DECREF(args);
311 Py_DECREF(func);
312 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000313}
314
315static PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000316method_get_doc(PyMethodDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 if (descr->d_method->ml_doc == NULL) {
319 Py_INCREF(Py_None);
320 return Py_None;
321 }
322 return PyUnicode_FromString(descr->d_method->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000323}
324
Antoine Pitrou9d574812011-12-12 13:47:25 +0100325static PyObject *
326calculate_qualname(PyDescrObject *descr)
327{
328 PyObject *type_qualname, *res;
329 _Py_IDENTIFIER(__qualname__);
330
331 if (descr->d_name == NULL || !PyUnicode_Check(descr->d_name)) {
332 PyErr_SetString(PyExc_TypeError,
333 "<descriptor>.__name__ is not a unicode object");
334 return NULL;
335 }
336
337 type_qualname = _PyObject_GetAttrId((PyObject *)descr->d_type,
338 &PyId___qualname__);
339 if (type_qualname == NULL)
340 return NULL;
341
342 if (!PyUnicode_Check(type_qualname)) {
343 PyErr_SetString(PyExc_TypeError, "<descriptor>.__objclass__."
344 "__qualname__ is not a unicode object");
345 Py_XDECREF(type_qualname);
346 return NULL;
347 }
348
349 res = PyUnicode_FromFormat("%S.%S", type_qualname, descr->d_name);
350 Py_DECREF(type_qualname);
351 return res;
352}
353
354static PyObject *
355descr_get_qualname(PyDescrObject *descr)
356{
357 if (descr->d_qualname == NULL)
358 descr->d_qualname = calculate_qualname(descr);
359 Py_XINCREF(descr->d_qualname);
360 return descr->d_qualname;
361}
362
Guido van Rossum6f799372001-09-20 20:46:19 +0000363static PyMemberDef descr_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
365 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
366 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000367};
368
Guido van Rossum32d34c82001-09-20 21:45:26 +0000369static PyGetSetDef method_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 {"__doc__", (getter)method_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100371 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 {0}
Guido van Rossum6f799372001-09-20 20:46:19 +0000373};
374
375static PyObject *
376member_get_doc(PyMemberDescrObject *descr, void *closure)
377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (descr->d_member->doc == NULL) {
379 Py_INCREF(Py_None);
380 return Py_None;
381 }
382 return PyUnicode_FromString(descr->d_member->doc);
Guido van Rossum6f799372001-09-20 20:46:19 +0000383}
384
Guido van Rossum32d34c82001-09-20 21:45:26 +0000385static PyGetSetDef member_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 {"__doc__", (getter)member_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100387 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000389};
390
391static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000392getset_get_doc(PyGetSetDescrObject *descr, void *closure)
393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (descr->d_getset->doc == NULL) {
395 Py_INCREF(Py_None);
396 return Py_None;
397 }
398 return PyUnicode_FromString(descr->d_getset->doc);
Guido van Rossum32d34c82001-09-20 21:45:26 +0000399}
400
401static PyGetSetDef getset_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 {"__doc__", (getter)getset_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100403 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 {0}
Guido van Rossum32d34c82001-09-20 21:45:26 +0000405};
406
407static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000408wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 if (descr->d_base->doc == NULL) {
411 Py_INCREF(Py_None);
412 return Py_None;
413 }
414 return PyUnicode_FromString(descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000415}
416
Armin Rigoc6686b72005-11-07 08:38:00 +0000417static PyGetSetDef wrapperdescr_getset[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 {"__doc__", (getter)wrapperdescr_get_doc},
Antoine Pitrou9d574812011-12-12 13:47:25 +0100419 {"__qualname__", (getter)descr_get_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000421};
422
Guido van Rossum048eb752001-10-02 21:24:57 +0000423static int
424descr_traverse(PyObject *self, visitproc visit, void *arg)
425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 PyDescrObject *descr = (PyDescrObject *)self;
427 Py_VISIT(descr->d_type);
428 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000429}
430
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000431PyTypeObject PyMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 PyVarObject_HEAD_INIT(&PyType_Type, 0)
433 "method_descriptor",
434 sizeof(PyMethodDescrObject),
435 0,
436 (destructor)descr_dealloc, /* tp_dealloc */
437 0, /* tp_print */
438 0, /* tp_getattr */
439 0, /* tp_setattr */
440 0, /* tp_reserved */
441 (reprfunc)method_repr, /* tp_repr */
442 0, /* tp_as_number */
443 0, /* tp_as_sequence */
444 0, /* tp_as_mapping */
445 0, /* tp_hash */
446 (ternaryfunc)methoddescr_call, /* tp_call */
447 0, /* tp_str */
448 PyObject_GenericGetAttr, /* tp_getattro */
449 0, /* tp_setattro */
450 0, /* tp_as_buffer */
451 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
452 0, /* tp_doc */
453 descr_traverse, /* tp_traverse */
454 0, /* tp_clear */
455 0, /* tp_richcompare */
456 0, /* tp_weaklistoffset */
457 0, /* tp_iter */
458 0, /* tp_iternext */
459 0, /* tp_methods */
460 descr_members, /* tp_members */
461 method_getset, /* tp_getset */
462 0, /* tp_base */
463 0, /* tp_dict */
464 (descrgetfunc)method_get, /* tp_descr_get */
465 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000466};
467
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000468/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000469PyTypeObject PyClassMethodDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyVarObject_HEAD_INIT(&PyType_Type, 0)
471 "classmethod_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)classmethoddescr_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)classmethod_get, /* tp_descr_get */
503 0, /* tp_descr_set */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000504};
505
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000506PyTypeObject PyMemberDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 PyVarObject_HEAD_INIT(&PyType_Type, 0)
508 "member_descriptor",
509 sizeof(PyMemberDescrObject),
510 0,
511 (destructor)descr_dealloc, /* tp_dealloc */
512 0, /* tp_print */
513 0, /* tp_getattr */
514 0, /* tp_setattr */
515 0, /* tp_reserved */
516 (reprfunc)member_repr, /* tp_repr */
517 0, /* tp_as_number */
518 0, /* tp_as_sequence */
519 0, /* tp_as_mapping */
520 0, /* tp_hash */
521 0, /* tp_call */
522 0, /* tp_str */
523 PyObject_GenericGetAttr, /* tp_getattro */
524 0, /* tp_setattro */
525 0, /* tp_as_buffer */
526 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
527 0, /* tp_doc */
528 descr_traverse, /* tp_traverse */
529 0, /* tp_clear */
530 0, /* tp_richcompare */
531 0, /* tp_weaklistoffset */
532 0, /* tp_iter */
533 0, /* tp_iternext */
534 0, /* tp_methods */
535 descr_members, /* tp_members */
536 member_getset, /* tp_getset */
537 0, /* tp_base */
538 0, /* tp_dict */
539 (descrgetfunc)member_get, /* tp_descr_get */
540 (descrsetfunc)member_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000541};
542
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000543PyTypeObject PyGetSetDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyVarObject_HEAD_INIT(&PyType_Type, 0)
545 "getset_descriptor",
546 sizeof(PyGetSetDescrObject),
547 0,
548 (destructor)descr_dealloc, /* tp_dealloc */
549 0, /* tp_print */
550 0, /* tp_getattr */
551 0, /* tp_setattr */
552 0, /* tp_reserved */
553 (reprfunc)getset_repr, /* tp_repr */
554 0, /* tp_as_number */
555 0, /* tp_as_sequence */
556 0, /* tp_as_mapping */
557 0, /* tp_hash */
558 0, /* tp_call */
559 0, /* tp_str */
560 PyObject_GenericGetAttr, /* tp_getattro */
561 0, /* tp_setattro */
562 0, /* tp_as_buffer */
563 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
564 0, /* tp_doc */
565 descr_traverse, /* tp_traverse */
566 0, /* tp_clear */
567 0, /* tp_richcompare */
568 0, /* tp_weaklistoffset */
569 0, /* tp_iter */
570 0, /* tp_iternext */
571 0, /* tp_methods */
572 descr_members, /* tp_members */
573 getset_getset, /* tp_getset */
574 0, /* tp_base */
575 0, /* tp_dict */
576 (descrgetfunc)getset_get, /* tp_descr_get */
577 (descrsetfunc)getset_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000578};
579
Guido van Rossumf4593e02001-10-03 12:09:30 +0000580PyTypeObject PyWrapperDescr_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 PyVarObject_HEAD_INIT(&PyType_Type, 0)
582 "wrapper_descriptor",
583 sizeof(PyWrapperDescrObject),
584 0,
585 (destructor)descr_dealloc, /* tp_dealloc */
586 0, /* tp_print */
587 0, /* tp_getattr */
588 0, /* tp_setattr */
589 0, /* tp_reserved */
590 (reprfunc)wrapperdescr_repr, /* tp_repr */
591 0, /* tp_as_number */
592 0, /* tp_as_sequence */
593 0, /* tp_as_mapping */
594 0, /* tp_hash */
595 (ternaryfunc)wrapperdescr_call, /* tp_call */
596 0, /* tp_str */
597 PyObject_GenericGetAttr, /* tp_getattro */
598 0, /* tp_setattro */
599 0, /* tp_as_buffer */
600 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
601 0, /* tp_doc */
602 descr_traverse, /* tp_traverse */
603 0, /* tp_clear */
604 0, /* tp_richcompare */
605 0, /* tp_weaklistoffset */
606 0, /* tp_iter */
607 0, /* tp_iternext */
608 0, /* tp_methods */
609 descr_members, /* tp_members */
610 wrapperdescr_getset, /* tp_getset */
611 0, /* tp_base */
612 0, /* tp_dict */
613 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
614 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000615};
616
617static PyDescrObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000618descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 PyDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
623 if (descr != NULL) {
624 Py_XINCREF(type);
625 descr->d_type = type;
626 descr->d_name = PyUnicode_InternFromString(name);
627 if (descr->d_name == NULL) {
628 Py_DECREF(descr);
629 descr = NULL;
630 }
Antoine Pitrou9d574812011-12-12 13:47:25 +0100631 descr->d_qualname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 }
633 return descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000634}
635
636PyObject *
637PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 PyMethodDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
642 type, method->ml_name);
643 if (descr != NULL)
644 descr->d_method = method;
645 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000646}
647
648PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000649PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 PyMethodDescrObject *descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
654 type, method->ml_name);
655 if (descr != NULL)
656 descr->d_method = method;
657 return (PyObject *)descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000658}
659
660PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000661PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyMemberDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
666 type, member->name);
667 if (descr != NULL)
668 descr->d_member = member;
669 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000670}
671
672PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000673PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 PyGetSetDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
678 type, getset->name);
679 if (descr != NULL)
680 descr->d_getset = getset;
681 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000682}
683
684PyObject *
685PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
690 type, base->name);
691 if (descr != NULL) {
692 descr->d_base = base;
693 descr->d_wrapped = wrapped;
694 }
695 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000696}
697
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698
699/* --- Readonly proxy for dictionaries (actually any mapping) --- */
700
701/* This has no reason to be in this file except that adding new files is a
702 bit of a pain */
703
704typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 PyObject_HEAD
706 PyObject *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000707} proxyobject;
708
Martin v. Löwis18e16552006-02-15 17:27:45 +0000709static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +0000710proxy_len(proxyobject *pp)
711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 return PyObject_Size(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000713}
714
715static PyObject *
716proxy_getitem(proxyobject *pp, PyObject *key)
717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 return PyObject_GetItem(pp->dict, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719}
720
721static PyMappingMethods proxy_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 (lenfunc)proxy_len, /* mp_length */
723 (binaryfunc)proxy_getitem, /* mp_subscript */
724 0, /* mp_ass_subscript */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725};
726
727static int
728proxy_contains(proxyobject *pp, PyObject *key)
729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 return PyDict_Contains(pp->dict, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731}
732
733static PySequenceMethods proxy_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 0, /* sq_length */
735 0, /* sq_concat */
736 0, /* sq_repeat */
737 0, /* sq_item */
738 0, /* sq_slice */
739 0, /* sq_ass_item */
740 0, /* sq_ass_slice */
741 (objobjproc)proxy_contains, /* sq_contains */
742 0, /* sq_inplace_concat */
743 0, /* sq_inplace_repeat */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744};
745
746static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000747proxy_get(proxyobject *pp, PyObject *args)
748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyObject *key, *def = Py_None;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200750 _Py_IDENTIFIER(get);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
753 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200754 return _PyObject_CallMethodId(pp->dict, &PyId_get, "(OO)", key, def);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755}
756
757static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000758proxy_keys(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200760 _Py_IDENTIFIER(keys);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200761 return _PyObject_CallMethodId(pp->dict, &PyId_keys, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762}
763
764static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000765proxy_values(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000766{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200767 _Py_IDENTIFIER(values);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200768 return _PyObject_CallMethodId(pp->dict, &PyId_values, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000769}
770
771static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000772proxy_items(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200774 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200775 return _PyObject_CallMethodId(pp->dict, &PyId_items, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000776}
777
778static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000779proxy_copy(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000780{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200781 _Py_IDENTIFIER(copy);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200782 return _PyObject_CallMethodId(pp->dict, &PyId_copy, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000783}
784
785static PyMethodDef proxy_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 {"get", (PyCFunction)proxy_get, METH_VARARGS,
787 PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
788 " d defaults to None.")},
789 {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
790 PyDoc_STR("D.keys() -> list of D's keys")},
791 {"values", (PyCFunction)proxy_values, METH_NOARGS,
792 PyDoc_STR("D.values() -> list of D's values")},
793 {"items", (PyCFunction)proxy_items, METH_NOARGS,
794 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
795 {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
796 PyDoc_STR("D.copy() -> a shallow copy of D")},
797 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798};
799
800static void
801proxy_dealloc(proxyobject *pp)
802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 _PyObject_GC_UNTRACK(pp);
804 Py_DECREF(pp->dict);
805 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000806}
807
808static PyObject *
809proxy_getiter(proxyobject *pp)
810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 return PyObject_GetIter(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000812}
813
Neil Schemenauer26775122001-10-21 22:26:43 +0000814static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000815proxy_str(proxyobject *pp)
816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 return PyObject_Str(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000818}
819
Ezio Melottiac53ab62010-12-18 14:59:43 +0000820static PyObject *
821proxy_repr(proxyobject *pp)
822{
823 return PyUnicode_FromFormat("dict_proxy(%R)", pp->dict);
824}
825
Guido van Rossum048eb752001-10-02 21:24:57 +0000826static int
827proxy_traverse(PyObject *self, visitproc visit, void *arg)
828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 proxyobject *pp = (proxyobject *)self;
830 Py_VISIT(pp->dict);
831 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000832}
833
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000834static PyObject *
835proxy_richcompare(proxyobject *v, PyObject *w, int op)
836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 return PyObject_RichCompare(v->dict, w, op);
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000838}
839
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000840PyTypeObject PyDictProxy_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 PyVarObject_HEAD_INIT(&PyType_Type, 0)
842 "dict_proxy", /* tp_name */
843 sizeof(proxyobject), /* tp_basicsize */
844 0, /* tp_itemsize */
845 /* methods */
846 (destructor)proxy_dealloc, /* tp_dealloc */
847 0, /* tp_print */
848 0, /* tp_getattr */
849 0, /* tp_setattr */
850 0, /* tp_reserved */
Ezio Melottiac53ab62010-12-18 14:59:43 +0000851 (reprfunc)proxy_repr, /* tp_repr */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 0, /* tp_as_number */
853 &proxy_as_sequence, /* tp_as_sequence */
854 &proxy_as_mapping, /* tp_as_mapping */
855 0, /* tp_hash */
856 0, /* tp_call */
857 (reprfunc)proxy_str, /* tp_str */
858 PyObject_GenericGetAttr, /* tp_getattro */
859 0, /* tp_setattro */
860 0, /* tp_as_buffer */
861 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
862 0, /* tp_doc */
863 proxy_traverse, /* tp_traverse */
864 0, /* tp_clear */
865 (richcmpfunc)proxy_richcompare, /* tp_richcompare */
866 0, /* tp_weaklistoffset */
867 (getiterfunc)proxy_getiter, /* tp_iter */
868 0, /* tp_iternext */
869 proxy_methods, /* tp_methods */
870 0, /* tp_members */
871 0, /* tp_getset */
872 0, /* tp_base */
873 0, /* tp_dict */
874 0, /* tp_descr_get */
875 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000876};
877
878PyObject *
879PyDictProxy_New(PyObject *dict)
880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 proxyobject *pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 pp = PyObject_GC_New(proxyobject, &PyDictProxy_Type);
884 if (pp != NULL) {
885 Py_INCREF(dict);
886 pp->dict = dict;
887 _PyObject_GC_TRACK(pp);
888 }
889 return (PyObject *)pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890}
891
892
893/* --- Wrapper object for "slot" methods --- */
894
895/* This has no reason to be in this file except that adding new files is a
896 bit of a pain */
897
898typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 PyObject_HEAD
900 PyWrapperDescrObject *descr;
901 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902} wrapperobject;
903
Benjamin Petersoneff61f62011-09-01 16:32:31 -0400904#define Wrapper_Check(v) (Py_TYPE(v) == &_PyMethodWrapper_Type)
Mark Dickinson211c6252009-02-01 10:28:51 +0000905
Tim Peters6d6c1a32001-08-02 04:15:00 +0000906static void
907wrapper_dealloc(wrapperobject *wp)
908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 PyObject_GC_UnTrack(wp);
910 Py_TRASHCAN_SAFE_BEGIN(wp)
911 Py_XDECREF(wp->descr);
912 Py_XDECREF(wp->self);
913 PyObject_GC_Del(wp);
914 Py_TRASHCAN_SAFE_END(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000915}
916
Mark Dickinson211c6252009-02-01 10:28:51 +0000917#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
918
919static PyObject *
920wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +0000921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 int result;
923 PyObject *v;
924 PyWrapperDescrObject *a_descr, *b_descr;
Mark Dickinson211c6252009-02-01 10:28:51 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 assert(a != NULL && b != NULL);
Mark Dickinson211c6252009-02-01 10:28:51 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* both arguments should be wrapperobjects */
929 if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
930 v = Py_NotImplemented;
931 Py_INCREF(v);
932 return v;
933 }
Mark Dickinson211c6252009-02-01 10:28:51 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 /* compare by descriptor address; if the descriptors are the same,
936 compare by the objects they're bound to */
937 a_descr = ((wrapperobject *)a)->descr;
938 b_descr = ((wrapperobject *)b)->descr;
939 if (a_descr == b_descr) {
940 a = ((wrapperobject *)a)->self;
941 b = ((wrapperobject *)b)->self;
942 return PyObject_RichCompare(a, b, op);
943 }
Mark Dickinson211c6252009-02-01 10:28:51 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 result = a_descr - b_descr;
946 switch (op) {
947 case Py_EQ:
948 v = TEST_COND(result == 0);
949 break;
950 case Py_NE:
951 v = TEST_COND(result != 0);
952 break;
953 case Py_LE:
954 v = TEST_COND(result <= 0);
955 break;
956 case Py_GE:
957 v = TEST_COND(result >= 0);
958 break;
959 case Py_LT:
960 v = TEST_COND(result < 0);
961 break;
962 case Py_GT:
963 v = TEST_COND(result > 0);
964 break;
965 default:
966 PyErr_BadArgument();
967 return NULL;
968 }
969 Py_INCREF(v);
970 return v;
Armin Rigoc6686b72005-11-07 08:38:00 +0000971}
972
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000973static Py_hash_t
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000974wrapper_hash(wrapperobject *wp)
975{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000976 Py_hash_t x, y;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 x = _Py_HashPointer(wp->descr);
978 if (x == -1)
979 return -1;
980 y = PyObject_Hash(wp->self);
981 if (y == -1)
982 return -1;
983 x = x ^ y;
984 if (x == -1)
985 x = -2;
986 return x;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000987}
988
Armin Rigoc6686b72005-11-07 08:38:00 +0000989static PyObject *
990wrapper_repr(wrapperobject *wp)
991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
993 wp->descr->d_base->name,
994 wp->self->ob_type->tp_name,
995 wp->self);
Armin Rigoc6686b72005-11-07 08:38:00 +0000996}
997
998static PyMemberDef wrapper_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
1000 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001001};
1002
1003static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +00001004wrapper_objclass(wrapperobject *wp)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 PyObject *c = (PyObject *)PyDescr_TYPE(wp->descr);
Armin Rigoc6686b72005-11-07 08:38:00 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 Py_INCREF(c);
1009 return c;
Armin Rigoc6686b72005-11-07 08:38:00 +00001010}
1011
1012static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001013wrapper_name(wrapperobject *wp)
1014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001018}
1019
1020static PyObject *
1021wrapper_doc(wrapperobject *wp)
1022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 const char *s = wp->descr->d_base->doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 if (s == NULL) {
1026 Py_INCREF(Py_None);
1027 return Py_None;
1028 }
1029 else {
1030 return PyUnicode_FromString(s);
1031 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032}
1033
Antoine Pitrou9d574812011-12-12 13:47:25 +01001034static PyObject *
1035wrapper_qualname(wrapperobject *wp)
1036{
1037 return descr_get_qualname((PyDescrObject *)wp->descr);
1038}
1039
Guido van Rossum32d34c82001-09-20 21:45:26 +00001040static PyGetSetDef wrapper_getsets[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 {"__objclass__", (getter)wrapper_objclass},
1042 {"__name__", (getter)wrapper_name},
Antoine Pitrou9d574812011-12-12 13:47:25 +01001043 {"__qualname__", (getter)wrapper_qualname},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 {"__doc__", (getter)wrapper_doc},
1045 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +00001046};
1047
1048static PyObject *
1049wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
1050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 wrapperfunc wrapper = wp->descr->d_base->wrapper;
1052 PyObject *self = wp->self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
1055 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
1056 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
1057 }
Guido van Rossumc8e56452001-10-22 00:43:43 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
1060 PyErr_Format(PyExc_TypeError,
1061 "wrapper %s doesn't take keyword arguments",
1062 wp->descr->d_base->name);
1063 return NULL;
1064 }
1065 return (*wrapper)(self, args, wp->descr->d_wrapped);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001066}
1067
Guido van Rossum048eb752001-10-02 21:24:57 +00001068static int
1069wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 wrapperobject *wp = (wrapperobject *)self;
1072 Py_VISIT(wp->descr);
1073 Py_VISIT(wp->self);
1074 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001075}
1076
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001077PyTypeObject _PyMethodWrapper_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1079 "method-wrapper", /* tp_name */
1080 sizeof(wrapperobject), /* tp_basicsize */
1081 0, /* tp_itemsize */
1082 /* methods */
1083 (destructor)wrapper_dealloc, /* tp_dealloc */
1084 0, /* tp_print */
1085 0, /* tp_getattr */
1086 0, /* tp_setattr */
1087 0, /* tp_reserved */
1088 (reprfunc)wrapper_repr, /* tp_repr */
1089 0, /* tp_as_number */
1090 0, /* tp_as_sequence */
1091 0, /* tp_as_mapping */
1092 (hashfunc)wrapper_hash, /* tp_hash */
1093 (ternaryfunc)wrapper_call, /* tp_call */
1094 0, /* tp_str */
1095 PyObject_GenericGetAttr, /* tp_getattro */
1096 0, /* tp_setattro */
1097 0, /* tp_as_buffer */
1098 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1099 0, /* tp_doc */
1100 wrapper_traverse, /* tp_traverse */
1101 0, /* tp_clear */
1102 wrapper_richcompare, /* tp_richcompare */
1103 0, /* tp_weaklistoffset */
1104 0, /* tp_iter */
1105 0, /* tp_iternext */
1106 0, /* tp_methods */
1107 wrapper_members, /* tp_members */
1108 wrapper_getsets, /* tp_getset */
1109 0, /* tp_base */
1110 0, /* tp_dict */
1111 0, /* tp_descr_get */
1112 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001113};
1114
1115PyObject *
1116PyWrapper_New(PyObject *d, PyObject *self)
1117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 wrapperobject *wp;
1119 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1122 descr = (PyWrapperDescrObject *)d;
Victor Stinner3249dec2011-05-01 23:19:15 +02001123 assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
Victor Stinnerd9561312011-05-01 23:31:36 +02001124 (PyObject *)PyDescr_TYPE(descr)));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001125
Benjamin Petersoneff61f62011-09-01 16:32:31 -04001126 wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (wp != NULL) {
1128 Py_INCREF(descr);
1129 wp->descr = descr;
1130 Py_INCREF(self);
1131 wp->self = self;
1132 _PyObject_GC_TRACK(wp);
1133 }
1134 return (PyObject *)wp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001135}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001136
1137
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001138/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001139
1140/*
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001141 class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1144 if doc is None and fget is not None and hasattr(fget, "__doc__"):
1145 doc = fget.__doc__
1146 self.__get = fget
1147 self.__set = fset
1148 self.__del = fdel
1149 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 def __get__(self, inst, type=None):
1152 if inst is None:
1153 return self
1154 if self.__get is None:
1155 raise AttributeError, "unreadable attribute"
1156 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 def __set__(self, inst, value):
1159 if self.__set is None:
1160 raise AttributeError, "can't set attribute"
1161 return self.__set(inst, value)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 def __delete__(self, inst):
1164 if self.__del is None:
1165 raise AttributeError, "can't delete attribute"
1166 return self.__del(inst)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001167
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001168*/
1169
1170typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 PyObject_HEAD
1172 PyObject *prop_get;
1173 PyObject *prop_set;
1174 PyObject *prop_del;
1175 PyObject *prop_doc;
1176 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001177} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001178
Christian Heimes0449f632007-12-15 01:27:15 +00001179static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
Benjamin Peterson93964832010-06-28 03:07:10 +00001180 PyObject *);
Christian Heimes0449f632007-12-15 01:27:15 +00001181
Tim Peters66c1a522001-09-24 21:17:50 +00001182static PyMemberDef property_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1184 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1185 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1186 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1187 {0}
Tim Peters66c1a522001-09-24 21:17:50 +00001188};
1189
Christian Heimes0449f632007-12-15 01:27:15 +00001190
Guido van Rossum58da9312007-11-10 23:39:45 +00001191PyDoc_STRVAR(getter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 "Descriptor to change the getter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001193
Neal Norwitz32dde222008-04-15 06:43:13 +00001194static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001195property_getter(PyObject *self, PyObject *getter)
1196{
Benjamin Peterson93964832010-06-28 03:07:10 +00001197 return property_copy(self, getter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001198}
1199
Christian Heimes0449f632007-12-15 01:27:15 +00001200
Guido van Rossum58da9312007-11-10 23:39:45 +00001201PyDoc_STRVAR(setter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001203
Neal Norwitz32dde222008-04-15 06:43:13 +00001204static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001205property_setter(PyObject *self, PyObject *setter)
1206{
Benjamin Peterson93964832010-06-28 03:07:10 +00001207 return property_copy(self, NULL, setter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001208}
1209
Christian Heimes0449f632007-12-15 01:27:15 +00001210
Guido van Rossum58da9312007-11-10 23:39:45 +00001211PyDoc_STRVAR(deleter_doc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 "Descriptor to change the deleter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001213
Neal Norwitz32dde222008-04-15 06:43:13 +00001214static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001215property_deleter(PyObject *self, PyObject *deleter)
1216{
Benjamin Peterson93964832010-06-28 03:07:10 +00001217 return property_copy(self, NULL, NULL, deleter);
Guido van Rossum58da9312007-11-10 23:39:45 +00001218}
1219
1220
Guido van Rossum58da9312007-11-10 23:39:45 +00001221static PyMethodDef property_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 {"getter", property_getter, METH_O, getter_doc},
1223 {"setter", property_setter, METH_O, setter_doc},
1224 {"deleter", property_deleter, METH_O, deleter_doc},
1225 {0}
Guido van Rossum58da9312007-11-10 23:39:45 +00001226};
1227
Tim Peters66c1a522001-09-24 21:17:50 +00001228
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001229static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001230property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 _PyObject_GC_UNTRACK(self);
1235 Py_XDECREF(gs->prop_get);
1236 Py_XDECREF(gs->prop_set);
1237 Py_XDECREF(gs->prop_del);
1238 Py_XDECREF(gs->prop_doc);
1239 self->ob_type->tp_free(self);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001240}
1241
1242static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001243property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (obj == NULL || obj == Py_None) {
1248 Py_INCREF(self);
1249 return self;
1250 }
1251 if (gs->prop_get == NULL) {
1252 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1253 return NULL;
1254 }
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001255 return PyObject_CallFunctionObjArgs(gs->prop_get, obj, NULL);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001256}
1257
1258static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001259property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 propertyobject *gs = (propertyobject *)self;
1262 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (value == NULL)
1265 func = gs->prop_del;
1266 else
1267 func = gs->prop_set;
1268 if (func == NULL) {
1269 PyErr_SetString(PyExc_AttributeError,
1270 value == NULL ?
1271 "can't delete attribute" :
1272 "can't set attribute");
1273 return -1;
1274 }
1275 if (value == NULL)
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001276 res = PyObject_CallFunctionObjArgs(func, obj, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 else
Benjamin Peterson9b955de2010-12-07 04:04:02 +00001278 res = PyObject_CallFunctionObjArgs(func, obj, value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (res == NULL)
1280 return -1;
1281 Py_DECREF(res);
1282 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001283}
1284
Christian Heimes0449f632007-12-15 01:27:15 +00001285static PyObject *
Benjamin Peterson93964832010-06-28 03:07:10 +00001286property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
Christian Heimes0449f632007-12-15 01:27:15 +00001287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 propertyobject *pold = (propertyobject *)old;
Benjamin Peterson93964832010-06-28 03:07:10 +00001289 PyObject *new, *type, *doc;
Christian Heimes0449f632007-12-15 01:27:15 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 type = PyObject_Type(old);
1292 if (type == NULL)
1293 return NULL;
Christian Heimes0449f632007-12-15 01:27:15 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if (get == NULL || get == Py_None) {
1296 Py_XDECREF(get);
1297 get = pold->prop_get ? pold->prop_get : Py_None;
1298 }
1299 if (set == NULL || set == Py_None) {
1300 Py_XDECREF(set);
1301 set = pold->prop_set ? pold->prop_set : Py_None;
1302 }
1303 if (del == NULL || del == Py_None) {
1304 Py_XDECREF(del);
1305 del = pold->prop_del ? pold->prop_del : Py_None;
1306 }
Benjamin Peterson93964832010-06-28 03:07:10 +00001307 if (pold->getter_doc && get != Py_None) {
1308 /* make _init use __doc__ from getter */
1309 doc = Py_None;
1310 }
1311 else {
1312 doc = pold->prop_doc ? pold->prop_doc : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 }
R. David Murrayb18500d2009-05-04 22:59:07 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
1316 Py_DECREF(type);
1317 if (new == NULL)
1318 return NULL;
1319 return new;
Christian Heimes0449f632007-12-15 01:27:15 +00001320}
1321
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001322static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001323property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
1326 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
1327 propertyobject *prop = (propertyobject *)self;
Tim Peters66c1a522001-09-24 21:17:50 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
1330 kwlist, &get, &set, &del, &doc))
1331 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (get == Py_None)
1334 get = NULL;
1335 if (set == Py_None)
1336 set = NULL;
1337 if (del == Py_None)
1338 del = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 Py_XINCREF(get);
1341 Py_XINCREF(set);
1342 Py_XINCREF(del);
1343 Py_XINCREF(doc);
Christian Heimes0449f632007-12-15 01:27:15 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 prop->prop_get = get;
1346 prop->prop_set = set;
1347 prop->prop_del = del;
1348 prop->prop_doc = doc;
1349 prop->getter_doc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 /* if no docstring given and the getter has one, use that one */
1352 if ((doc == NULL || doc == Py_None) && get != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001353 _Py_IDENTIFIER(__doc__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001354 PyObject *get_doc = _PyObject_GetAttrId(get, &PyId___doc__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 if (get_doc) {
1356 if (Py_TYPE(self) == &PyProperty_Type) {
1357 Py_XDECREF(prop->prop_doc);
1358 prop->prop_doc = get_doc;
1359 }
1360 else {
1361 /* If this is a property subclass, put __doc__
1362 in dict of the subclass instance instead,
1363 otherwise it gets shadowed by __doc__ in the
1364 class's dict. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001365 int err = _PyObject_SetAttrId(self, &PyId___doc__, get_doc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 Py_DECREF(get_doc);
1367 if (err < 0)
1368 return -1;
1369 }
1370 prop->getter_doc = 1;
1371 }
1372 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1373 PyErr_Clear();
1374 }
1375 else {
1376 return -1;
1377 }
1378 }
1379
1380 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001381}
1382
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001383static PyObject *
1384property_get___isabstractmethod__(propertyobject *prop, void *closure)
1385{
1386 int res = _PyObject_IsAbstract(prop->prop_get);
1387 if (res == -1) {
1388 return NULL;
1389 }
1390 else if (res) {
1391 Py_RETURN_TRUE;
1392 }
1393
1394 res = _PyObject_IsAbstract(prop->prop_set);
1395 if (res == -1) {
1396 return NULL;
1397 }
1398 else if (res) {
1399 Py_RETURN_TRUE;
1400 }
1401
1402 res = _PyObject_IsAbstract(prop->prop_del);
1403 if (res == -1) {
1404 return NULL;
1405 }
1406 else if (res) {
1407 Py_RETURN_TRUE;
1408 }
1409 Py_RETURN_FALSE;
1410}
1411
1412static PyGetSetDef property_getsetlist[] = {
1413 {"__isabstractmethod__",
1414 (getter)property_get___isabstractmethod__, NULL,
1415 NULL,
1416 NULL},
1417 {NULL} /* Sentinel */
1418};
1419
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001420PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001421"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1422"\n"
1423"fget is a function to be used for getting an attribute value, and likewise\n"
1424"fset is a function for setting, and fdel a function for del'ing, an\n"
1425"attribute. Typical use is to define a managed attribute x:\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001426"class C(object):\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001427" def getx(self): return self._x\n"
1428" def setx(self, value): self._x = value\n"
1429" def delx(self): del self._x\n"
1430" x = property(getx, setx, delx, \"I'm the 'x' property.\")\n"
1431"\n"
1432"Decorators make defining new properties or modifying existing ones easy:\n"
1433"class C(object):\n"
1434" @property\n"
1435" def x(self): return self._x\n"
1436" @x.setter\n"
1437" def x(self, value): self._x = value\n"
1438" @x.deleter\n"
1439" def x(self): del self._x\n"
1440);
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001441
Guido van Rossum048eb752001-10-02 21:24:57 +00001442static int
1443property_traverse(PyObject *self, visitproc visit, void *arg)
1444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 propertyobject *pp = (propertyobject *)self;
1446 Py_VISIT(pp->prop_get);
1447 Py_VISIT(pp->prop_set);
1448 Py_VISIT(pp->prop_del);
1449 Py_VISIT(pp->prop_doc);
1450 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001451}
1452
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001453PyTypeObject PyProperty_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1455 "property", /* tp_name */
1456 sizeof(propertyobject), /* tp_basicsize */
1457 0, /* tp_itemsize */
1458 /* methods */
1459 property_dealloc, /* tp_dealloc */
1460 0, /* tp_print */
1461 0, /* tp_getattr */
1462 0, /* tp_setattr */
1463 0, /* tp_reserved */
1464 0, /* tp_repr */
1465 0, /* tp_as_number */
1466 0, /* tp_as_sequence */
1467 0, /* tp_as_mapping */
1468 0, /* tp_hash */
1469 0, /* tp_call */
1470 0, /* tp_str */
1471 PyObject_GenericGetAttr, /* tp_getattro */
1472 0, /* tp_setattro */
1473 0, /* tp_as_buffer */
1474 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1475 Py_TPFLAGS_BASETYPE, /* tp_flags */
1476 property_doc, /* tp_doc */
1477 property_traverse, /* tp_traverse */
1478 0, /* tp_clear */
1479 0, /* tp_richcompare */
1480 0, /* tp_weaklistoffset */
1481 0, /* tp_iter */
1482 0, /* tp_iternext */
1483 property_methods, /* tp_methods */
1484 property_members, /* tp_members */
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -05001485 property_getsetlist, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 0, /* tp_base */
1487 0, /* tp_dict */
1488 property_descr_get, /* tp_descr_get */
1489 property_descr_set, /* tp_descr_set */
1490 0, /* tp_dictoffset */
1491 property_init, /* tp_init */
1492 PyType_GenericAlloc, /* tp_alloc */
1493 PyType_GenericNew, /* tp_new */
1494 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001495};