blob: 5f118ce09ed4788f47a8f799d075cb608576c170 [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 Pitrou7f14f0d2010-05-09 16:14:21 +00009 _PyObject_GC_UNTRACK(descr);
10 Py_XDECREF(descr->d_type);
11 Py_XDECREF(descr->d_name);
12 PyObject_GC_Del(descr);
Tim Peters6d6c1a32001-08-02 04:15:00 +000013}
14
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000015static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000016descr_name(PyDescrObject *descr)
17{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000018 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
19 return descr->d_name;
20 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000021}
22
23static PyObject *
24descr_repr(PyDescrObject *descr, char *format)
25{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000026 PyObject *name = NULL;
27 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
28 name = descr->d_name;
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000029
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000030 return PyUnicode_FromFormat(format, name, "?", descr->d_type->tp_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +000031}
32
33static PyObject *
34method_repr(PyMethodDescrObject *descr)
35{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000036 return descr_repr((PyDescrObject *)descr,
37 "<method '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000038}
39
40static PyObject *
41member_repr(PyMemberDescrObject *descr)
42{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000043 return descr_repr((PyDescrObject *)descr,
44 "<member '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000045}
46
47static PyObject *
48getset_repr(PyGetSetDescrObject *descr)
49{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000050 return descr_repr((PyDescrObject *)descr,
51 "<attribute '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000052}
53
54static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +000055wrapperdescr_repr(PyWrapperDescrObject *descr)
Tim Peters6d6c1a32001-08-02 04:15:00 +000056{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000057 return descr_repr((PyDescrObject *)descr,
58 "<slot wrapper '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000059}
60
61static int
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000062descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +000063{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000064 if (obj == NULL) {
65 Py_INCREF(descr);
66 *pres = (PyObject *)descr;
67 return 1;
68 }
69 if (!PyObject_TypeCheck(obj, descr->d_type)) {
70 PyErr_Format(PyExc_TypeError,
71 "descriptor '%V' for '%s' objects "
72 "doesn't apply to '%s' object",
73 descr_name((PyDescrObject *)descr), "?",
74 descr->d_type->tp_name,
75 obj->ob_type->tp_name);
76 *pres = NULL;
77 return 1;
78 }
79 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +000080}
81
82static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000083classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Petersbca1cbc2002-12-09 22:56:13 +000084{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000085 /* Ensure a valid type. Class methods ignore obj. */
86 if (type == NULL) {
87 if (obj != NULL)
88 type = (PyObject *)obj->ob_type;
89 else {
90 /* Wot - no type?! */
91 PyErr_Format(PyExc_TypeError,
92 "descriptor '%V' for type '%s' "
93 "needs either an object or a type",
94 descr_name((PyDescrObject *)descr), "?",
95 descr->d_type->tp_name);
96 return NULL;
97 }
98 }
99 if (!PyType_Check(type)) {
100 PyErr_Format(PyExc_TypeError,
101 "descriptor '%V' for type '%s' "
102 "needs a type, not a '%s' as arg 2",
103 descr_name((PyDescrObject *)descr), "?",
104 descr->d_type->tp_name,
105 type->ob_type->tp_name);
106 return NULL;
107 }
108 if (!PyType_IsSubtype((PyTypeObject *)type, descr->d_type)) {
109 PyErr_Format(PyExc_TypeError,
110 "descriptor '%V' for type '%s' "
111 "doesn't apply to type '%s'",
112 descr_name((PyDescrObject *)descr), "?",
113 descr->d_type->tp_name,
114 ((PyTypeObject *)type)->tp_name);
115 return NULL;
116 }
117 return PyCFunction_New(descr->d_method, type);
Tim Petersbca1cbc2002-12-09 22:56:13 +0000118}
119
120static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000121method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000122{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000123 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000124
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000125 if (descr_check((PyDescrObject *)descr, obj, &res))
126 return res;
127 return PyCFunction_New(descr->d_method, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000128}
129
130static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000131member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000132{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000133 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000134
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000135 if (descr_check((PyDescrObject *)descr, obj, &res))
136 return res;
137 return PyMember_GetOne((char *)obj, descr->d_member);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000138}
139
140static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000141getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000142{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000143 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000144
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000145 if (descr_check((PyDescrObject *)descr, obj, &res))
146 return res;
147 if (descr->d_getset->get != NULL)
148 return descr->d_getset->get(obj, descr->d_getset->closure);
149 PyErr_Format(PyExc_AttributeError,
150 "attribute '%V' of '%.100s' objects is not readable",
151 descr_name((PyDescrObject *)descr), "?",
152 descr->d_type->tp_name);
153 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000154}
155
156static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000157wrapperdescr_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000158{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000159 PyObject *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000160
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000161 if (descr_check((PyDescrObject *)descr, obj, &res))
162 return res;
163 return PyWrapper_New((PyObject *)descr, obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000164}
165
166static int
167descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000168 int *pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000169{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000170 assert(obj != NULL);
171 if (!PyObject_TypeCheck(obj, descr->d_type)) {
172 PyErr_Format(PyExc_TypeError,
173 "descriptor '%V' for '%.100s' objects "
174 "doesn't apply to '%.100s' object",
175 descr_name(descr), "?",
176 descr->d_type->tp_name,
177 obj->ob_type->tp_name);
178 *pres = -1;
179 return 1;
180 }
181 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000182}
183
184static int
185member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
186{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000188
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000189 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
190 return res;
191 return PyMember_SetOne((char *)obj, descr->d_member, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000192}
193
194static int
195getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
196{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000197 int res;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000198
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000199 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
200 return res;
201 if (descr->d_getset->set != NULL)
202 return descr->d_getset->set(obj, value,
203 descr->d_getset->closure);
204 PyErr_Format(PyExc_AttributeError,
205 "attribute '%V' of '%.100s' objects is not writable",
206 descr_name((PyDescrObject *)descr), "?",
207 descr->d_type->tp_name);
208 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000209}
210
211static PyObject *
212methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
213{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 Py_ssize_t argc;
215 PyObject *self, *func, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000216
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 /* Make sure that the first argument is acceptable as 'self' */
218 assert(PyTuple_Check(args));
219 argc = PyTuple_GET_SIZE(args);
220 if (argc < 1) {
221 PyErr_Format(PyExc_TypeError,
222 "descriptor '%V' of '%.100s' "
223 "object needs an argument",
224 descr_name((PyDescrObject *)descr), "?",
225 descr->d_type->tp_name);
226 return NULL;
227 }
228 self = PyTuple_GET_ITEM(args, 0);
229 if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
230 PyErr_Format(PyExc_TypeError,
231 "descriptor '%V' "
232 "requires a '%.100s' object "
233 "but received a '%.100s'",
234 descr_name((PyDescrObject *)descr), "?",
235 descr->d_type->tp_name,
236 self->ob_type->tp_name);
237 return NULL;
238 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000239
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000240 func = PyCFunction_New(descr->d_method, self);
241 if (func == NULL)
242 return NULL;
243 args = PyTuple_GetSlice(args, 1, argc);
244 if (args == NULL) {
245 Py_DECREF(func);
246 return NULL;
247 }
248 result = PyEval_CallObjectWithKeywords(func, args, kwds);
249 Py_DECREF(args);
250 Py_DECREF(func);
251 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000252}
253
254static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000255classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000256 PyObject *kwds)
Tim Petersbca1cbc2002-12-09 22:56:13 +0000257{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000258 PyObject *func, *result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000259
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000260 func = PyCFunction_New(descr->d_method, (PyObject *)descr->d_type);
261 if (func == NULL)
262 return NULL;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000263
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000264 result = PyEval_CallObjectWithKeywords(func, args, kwds);
265 Py_DECREF(func);
266 return result;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000267}
268
269static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000270wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
271{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000272 Py_ssize_t argc;
273 PyObject *self, *func, *result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000274
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000275 /* Make sure that the first argument is acceptable as 'self' */
276 assert(PyTuple_Check(args));
277 argc = PyTuple_GET_SIZE(args);
278 if (argc < 1) {
279 PyErr_Format(PyExc_TypeError,
280 "descriptor '%V' of '%.100s' "
281 "object needs an argument",
282 descr_name((PyDescrObject *)descr), "?",
283 descr->d_type->tp_name);
284 return NULL;
285 }
286 self = PyTuple_GET_ITEM(args, 0);
287 if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
288 PyErr_Format(PyExc_TypeError,
289 "descriptor '%V' "
290 "requires a '%.100s' object "
291 "but received a '%.100s'",
292 descr_name((PyDescrObject *)descr), "?",
293 descr->d_type->tp_name,
294 self->ob_type->tp_name);
295 return NULL;
296 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000297
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000298 func = PyWrapper_New((PyObject *)descr, self);
299 if (func == NULL)
300 return NULL;
301 args = PyTuple_GetSlice(args, 1, argc);
302 if (args == NULL) {
303 Py_DECREF(func);
304 return NULL;
305 }
306 result = PyEval_CallObjectWithKeywords(func, args, kwds);
307 Py_DECREF(args);
308 Py_DECREF(func);
309 return result;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000310}
311
312static PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000313method_get_doc(PyMethodDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000314{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000315 if (descr->d_method->ml_doc == NULL) {
316 Py_INCREF(Py_None);
317 return Py_None;
318 }
319 return PyUnicode_FromString(descr->d_method->ml_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000320}
321
Guido van Rossum6f799372001-09-20 20:46:19 +0000322static PyMemberDef descr_members[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
324 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
325 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000326};
327
Guido van Rossum32d34c82001-09-20 21:45:26 +0000328static PyGetSetDef method_getset[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000329 {"__doc__", (getter)method_get_doc},
330 {0}
Guido van Rossum6f799372001-09-20 20:46:19 +0000331};
332
333static PyObject *
334member_get_doc(PyMemberDescrObject *descr, void *closure)
335{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000336 if (descr->d_member->doc == NULL) {
337 Py_INCREF(Py_None);
338 return Py_None;
339 }
340 return PyUnicode_FromString(descr->d_member->doc);
Guido van Rossum6f799372001-09-20 20:46:19 +0000341}
342
Guido van Rossum32d34c82001-09-20 21:45:26 +0000343static PyGetSetDef member_getset[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000344 {"__doc__", (getter)member_get_doc},
345 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000346};
347
348static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000349getset_get_doc(PyGetSetDescrObject *descr, void *closure)
350{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 if (descr->d_getset->doc == NULL) {
352 Py_INCREF(Py_None);
353 return Py_None;
354 }
355 return PyUnicode_FromString(descr->d_getset->doc);
Guido van Rossum32d34c82001-09-20 21:45:26 +0000356}
357
358static PyGetSetDef getset_getset[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000359 {"__doc__", (getter)getset_get_doc},
360 {0}
Guido van Rossum32d34c82001-09-20 21:45:26 +0000361};
362
363static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000364wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000365{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000366 if (descr->d_base->doc == NULL) {
367 Py_INCREF(Py_None);
368 return Py_None;
369 }
370 return PyUnicode_FromString(descr->d_base->doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000371}
372
Armin Rigoc6686b72005-11-07 08:38:00 +0000373static PyGetSetDef wrapperdescr_getset[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000374 {"__doc__", (getter)wrapperdescr_get_doc},
375 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000376};
377
Guido van Rossum048eb752001-10-02 21:24:57 +0000378static int
379descr_traverse(PyObject *self, visitproc visit, void *arg)
380{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000381 PyDescrObject *descr = (PyDescrObject *)self;
382 Py_VISIT(descr->d_type);
383 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000384}
385
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000386PyTypeObject PyMethodDescr_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000387 PyVarObject_HEAD_INIT(&PyType_Type, 0)
388 "method_descriptor",
389 sizeof(PyMethodDescrObject),
390 0,
391 (destructor)descr_dealloc, /* tp_dealloc */
392 0, /* tp_print */
393 0, /* tp_getattr */
394 0, /* tp_setattr */
395 0, /* tp_reserved */
396 (reprfunc)method_repr, /* tp_repr */
397 0, /* tp_as_number */
398 0, /* tp_as_sequence */
399 0, /* tp_as_mapping */
400 0, /* tp_hash */
401 (ternaryfunc)methoddescr_call, /* tp_call */
402 0, /* tp_str */
403 PyObject_GenericGetAttr, /* tp_getattro */
404 0, /* tp_setattro */
405 0, /* tp_as_buffer */
406 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
407 0, /* tp_doc */
408 descr_traverse, /* tp_traverse */
409 0, /* tp_clear */
410 0, /* tp_richcompare */
411 0, /* tp_weaklistoffset */
412 0, /* tp_iter */
413 0, /* tp_iternext */
414 0, /* tp_methods */
415 descr_members, /* tp_members */
416 method_getset, /* tp_getset */
417 0, /* tp_base */
418 0, /* tp_dict */
419 (descrgetfunc)method_get, /* tp_descr_get */
420 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000421};
422
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000423/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000424PyTypeObject PyClassMethodDescr_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000425 PyVarObject_HEAD_INIT(&PyType_Type, 0)
426 "classmethod_descriptor",
427 sizeof(PyMethodDescrObject),
428 0,
429 (destructor)descr_dealloc, /* tp_dealloc */
430 0, /* tp_print */
431 0, /* tp_getattr */
432 0, /* tp_setattr */
433 0, /* tp_reserved */
434 (reprfunc)method_repr, /* tp_repr */
435 0, /* tp_as_number */
436 0, /* tp_as_sequence */
437 0, /* tp_as_mapping */
438 0, /* tp_hash */
439 (ternaryfunc)classmethoddescr_call, /* tp_call */
440 0, /* tp_str */
441 PyObject_GenericGetAttr, /* tp_getattro */
442 0, /* tp_setattro */
443 0, /* tp_as_buffer */
444 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
445 0, /* tp_doc */
446 descr_traverse, /* tp_traverse */
447 0, /* tp_clear */
448 0, /* tp_richcompare */
449 0, /* tp_weaklistoffset */
450 0, /* tp_iter */
451 0, /* tp_iternext */
452 0, /* tp_methods */
453 descr_members, /* tp_members */
454 method_getset, /* tp_getset */
455 0, /* tp_base */
456 0, /* tp_dict */
457 (descrgetfunc)classmethod_get, /* tp_descr_get */
458 0, /* tp_descr_set */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000459};
460
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000461PyTypeObject PyMemberDescr_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000462 PyVarObject_HEAD_INIT(&PyType_Type, 0)
463 "member_descriptor",
464 sizeof(PyMemberDescrObject),
465 0,
466 (destructor)descr_dealloc, /* tp_dealloc */
467 0, /* tp_print */
468 0, /* tp_getattr */
469 0, /* tp_setattr */
470 0, /* tp_reserved */
471 (reprfunc)member_repr, /* tp_repr */
472 0, /* tp_as_number */
473 0, /* tp_as_sequence */
474 0, /* tp_as_mapping */
475 0, /* tp_hash */
476 0, /* tp_call */
477 0, /* tp_str */
478 PyObject_GenericGetAttr, /* tp_getattro */
479 0, /* tp_setattro */
480 0, /* tp_as_buffer */
481 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
482 0, /* tp_doc */
483 descr_traverse, /* tp_traverse */
484 0, /* tp_clear */
485 0, /* tp_richcompare */
486 0, /* tp_weaklistoffset */
487 0, /* tp_iter */
488 0, /* tp_iternext */
489 0, /* tp_methods */
490 descr_members, /* tp_members */
491 member_getset, /* tp_getset */
492 0, /* tp_base */
493 0, /* tp_dict */
494 (descrgetfunc)member_get, /* tp_descr_get */
495 (descrsetfunc)member_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000496};
497
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000498PyTypeObject PyGetSetDescr_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499 PyVarObject_HEAD_INIT(&PyType_Type, 0)
500 "getset_descriptor",
501 sizeof(PyGetSetDescrObject),
502 0,
503 (destructor)descr_dealloc, /* tp_dealloc */
504 0, /* tp_print */
505 0, /* tp_getattr */
506 0, /* tp_setattr */
507 0, /* tp_reserved */
508 (reprfunc)getset_repr, /* tp_repr */
509 0, /* tp_as_number */
510 0, /* tp_as_sequence */
511 0, /* tp_as_mapping */
512 0, /* tp_hash */
513 0, /* tp_call */
514 0, /* tp_str */
515 PyObject_GenericGetAttr, /* tp_getattro */
516 0, /* tp_setattro */
517 0, /* tp_as_buffer */
518 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
519 0, /* tp_doc */
520 descr_traverse, /* tp_traverse */
521 0, /* tp_clear */
522 0, /* tp_richcompare */
523 0, /* tp_weaklistoffset */
524 0, /* tp_iter */
525 0, /* tp_iternext */
526 0, /* tp_methods */
527 descr_members, /* tp_members */
528 getset_getset, /* tp_getset */
529 0, /* tp_base */
530 0, /* tp_dict */
531 (descrgetfunc)getset_get, /* tp_descr_get */
532 (descrsetfunc)getset_set, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000533};
534
Guido van Rossumf4593e02001-10-03 12:09:30 +0000535PyTypeObject PyWrapperDescr_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000536 PyVarObject_HEAD_INIT(&PyType_Type, 0)
537 "wrapper_descriptor",
538 sizeof(PyWrapperDescrObject),
539 0,
540 (destructor)descr_dealloc, /* tp_dealloc */
541 0, /* tp_print */
542 0, /* tp_getattr */
543 0, /* tp_setattr */
544 0, /* tp_reserved */
545 (reprfunc)wrapperdescr_repr, /* tp_repr */
546 0, /* tp_as_number */
547 0, /* tp_as_sequence */
548 0, /* tp_as_mapping */
549 0, /* tp_hash */
550 (ternaryfunc)wrapperdescr_call, /* tp_call */
551 0, /* tp_str */
552 PyObject_GenericGetAttr, /* tp_getattro */
553 0, /* tp_setattro */
554 0, /* tp_as_buffer */
555 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
556 0, /* tp_doc */
557 descr_traverse, /* tp_traverse */
558 0, /* tp_clear */
559 0, /* tp_richcompare */
560 0, /* tp_weaklistoffset */
561 0, /* tp_iter */
562 0, /* tp_iternext */
563 0, /* tp_methods */
564 descr_members, /* tp_members */
565 wrapperdescr_getset, /* tp_getset */
566 0, /* tp_base */
567 0, /* tp_dict */
568 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
569 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000570};
571
572static PyDescrObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000573descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000574{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000575 PyDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000576
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000577 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
578 if (descr != NULL) {
579 Py_XINCREF(type);
580 descr->d_type = type;
581 descr->d_name = PyUnicode_InternFromString(name);
582 if (descr->d_name == NULL) {
583 Py_DECREF(descr);
584 descr = NULL;
585 }
586 }
587 return descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000588}
589
590PyObject *
591PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
592{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000593 PyMethodDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000594
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
596 type, method->ml_name);
597 if (descr != NULL)
598 descr->d_method = method;
599 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000600}
601
602PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000603PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
604{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000605 PyMethodDescrObject *descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000606
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000607 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
608 type, method->ml_name);
609 if (descr != NULL)
610 descr->d_method = method;
611 return (PyObject *)descr;
Tim Petersbca1cbc2002-12-09 22:56:13 +0000612}
613
614PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000615PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000616{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 PyMemberDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000618
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000619 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
620 type, member->name);
621 if (descr != NULL)
622 descr->d_member = member;
623 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000624}
625
626PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000627PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000628{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000629 PyGetSetDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000630
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000631 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
632 type, getset->name);
633 if (descr != NULL)
634 descr->d_getset = getset;
635 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636}
637
638PyObject *
639PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
640{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000641 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000643 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
644 type, base->name);
645 if (descr != NULL) {
646 descr->d_base = base;
647 descr->d_wrapped = wrapped;
648 }
649 return (PyObject *)descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650}
651
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652
653/* --- Readonly proxy for dictionaries (actually any mapping) --- */
654
655/* This has no reason to be in this file except that adding new files is a
656 bit of a pain */
657
658typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000659 PyObject_HEAD
660 PyObject *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000661} proxyobject;
662
Martin v. Löwis18e16552006-02-15 17:27:45 +0000663static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +0000664proxy_len(proxyobject *pp)
665{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000666 return PyObject_Size(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000667}
668
669static PyObject *
670proxy_getitem(proxyobject *pp, PyObject *key)
671{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000672 return PyObject_GetItem(pp->dict, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000673}
674
675static PyMappingMethods proxy_as_mapping = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000676 (lenfunc)proxy_len, /* mp_length */
677 (binaryfunc)proxy_getitem, /* mp_subscript */
678 0, /* mp_ass_subscript */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000679};
680
681static int
682proxy_contains(proxyobject *pp, PyObject *key)
683{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000684 return PyDict_Contains(pp->dict, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685}
686
687static PySequenceMethods proxy_as_sequence = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000688 0, /* sq_length */
689 0, /* sq_concat */
690 0, /* sq_repeat */
691 0, /* sq_item */
692 0, /* sq_slice */
693 0, /* sq_ass_item */
694 0, /* sq_ass_slice */
695 (objobjproc)proxy_contains, /* sq_contains */
696 0, /* sq_inplace_concat */
697 0, /* sq_inplace_repeat */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698};
699
700static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701proxy_get(proxyobject *pp, PyObject *args)
702{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000703 PyObject *key, *def = Py_None;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000705 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
706 return NULL;
707 return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000708}
709
710static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000711proxy_keys(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000713 return PyMapping_Keys(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000714}
715
716static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000717proxy_values(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000718{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 return PyMapping_Values(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000720}
721
722static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000723proxy_items(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000724{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000725 return PyMapping_Items(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726}
727
728static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000729proxy_copy(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000731 return PyObject_CallMethod(pp->dict, "copy", NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000732}
733
734static PyMethodDef proxy_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000735 {"get", (PyCFunction)proxy_get, METH_VARARGS,
736 PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
737 " d defaults to None.")},
738 {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
739 PyDoc_STR("D.keys() -> list of D's keys")},
740 {"values", (PyCFunction)proxy_values, METH_NOARGS,
741 PyDoc_STR("D.values() -> list of D's values")},
742 {"items", (PyCFunction)proxy_items, METH_NOARGS,
743 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
744 {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
745 PyDoc_STR("D.copy() -> a shallow copy of D")},
746 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000747};
748
749static void
750proxy_dealloc(proxyobject *pp)
751{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000752 _PyObject_GC_UNTRACK(pp);
753 Py_DECREF(pp->dict);
754 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755}
756
757static PyObject *
758proxy_getiter(proxyobject *pp)
759{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000760 return PyObject_GetIter(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000761}
762
Neil Schemenauer26775122001-10-21 22:26:43 +0000763static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000764proxy_str(proxyobject *pp)
765{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000766 return PyObject_Str(pp->dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000767}
768
Ezio Melotti9d254f72010-12-18 15:06:45 +0000769static PyObject *
770proxy_repr(proxyobject *pp)
771{
772 return PyUnicode_FromFormat("dict_proxy(%R)", pp->dict);
773}
774
Guido van Rossum048eb752001-10-02 21:24:57 +0000775static int
776proxy_traverse(PyObject *self, visitproc visit, void *arg)
777{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000778 proxyobject *pp = (proxyobject *)self;
779 Py_VISIT(pp->dict);
780 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +0000781}
782
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000783static PyObject *
784proxy_richcompare(proxyobject *v, PyObject *w, int op)
785{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000786 return PyObject_RichCompare(v->dict, w, op);
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000787}
788
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000789PyTypeObject PyDictProxy_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000790 PyVarObject_HEAD_INIT(&PyType_Type, 0)
791 "dict_proxy", /* tp_name */
792 sizeof(proxyobject), /* tp_basicsize */
793 0, /* tp_itemsize */
794 /* methods */
795 (destructor)proxy_dealloc, /* tp_dealloc */
796 0, /* tp_print */
797 0, /* tp_getattr */
798 0, /* tp_setattr */
799 0, /* tp_reserved */
Ezio Melotti9d254f72010-12-18 15:06:45 +0000800 (reprfunc)proxy_repr, /* tp_repr */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000801 0, /* tp_as_number */
802 &proxy_as_sequence, /* tp_as_sequence */
803 &proxy_as_mapping, /* tp_as_mapping */
804 0, /* tp_hash */
805 0, /* tp_call */
806 (reprfunc)proxy_str, /* tp_str */
807 PyObject_GenericGetAttr, /* tp_getattro */
808 0, /* tp_setattro */
809 0, /* tp_as_buffer */
810 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
811 0, /* tp_doc */
812 proxy_traverse, /* tp_traverse */
813 0, /* tp_clear */
814 (richcmpfunc)proxy_richcompare, /* tp_richcompare */
815 0, /* tp_weaklistoffset */
816 (getiterfunc)proxy_getiter, /* tp_iter */
817 0, /* tp_iternext */
818 proxy_methods, /* tp_methods */
819 0, /* tp_members */
820 0, /* tp_getset */
821 0, /* tp_base */
822 0, /* tp_dict */
823 0, /* tp_descr_get */
824 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000825};
826
827PyObject *
828PyDictProxy_New(PyObject *dict)
829{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000830 proxyobject *pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000831
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000832 pp = PyObject_GC_New(proxyobject, &PyDictProxy_Type);
833 if (pp != NULL) {
834 Py_INCREF(dict);
835 pp->dict = dict;
836 _PyObject_GC_TRACK(pp);
837 }
838 return (PyObject *)pp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000839}
840
841
842/* --- Wrapper object for "slot" methods --- */
843
844/* This has no reason to be in this file except that adding new files is a
845 bit of a pain */
846
Mark Dickinson211c6252009-02-01 10:28:51 +0000847/* forward */
848static PyTypeObject wrappertype;
849
Tim Peters6d6c1a32001-08-02 04:15:00 +0000850typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000851 PyObject_HEAD
852 PyWrapperDescrObject *descr;
853 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000854} wrapperobject;
855
Mark Dickinson211c6252009-02-01 10:28:51 +0000856#define Wrapper_Check(v) (Py_TYPE(v) == &wrappertype)
857
Tim Peters6d6c1a32001-08-02 04:15:00 +0000858static void
859wrapper_dealloc(wrapperobject *wp)
860{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000861 PyObject_GC_UnTrack(wp);
862 Py_TRASHCAN_SAFE_BEGIN(wp)
863 Py_XDECREF(wp->descr);
864 Py_XDECREF(wp->self);
865 PyObject_GC_Del(wp);
866 Py_TRASHCAN_SAFE_END(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000867}
868
Mark Dickinson211c6252009-02-01 10:28:51 +0000869#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
870
871static PyObject *
872wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +0000873{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000874 int result;
875 PyObject *v;
876 PyWrapperDescrObject *a_descr, *b_descr;
Mark Dickinson211c6252009-02-01 10:28:51 +0000877
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 assert(a != NULL && b != NULL);
Mark Dickinson211c6252009-02-01 10:28:51 +0000879
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000880 /* both arguments should be wrapperobjects */
881 if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
882 v = Py_NotImplemented;
883 Py_INCREF(v);
884 return v;
885 }
Mark Dickinson211c6252009-02-01 10:28:51 +0000886
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000887 /* compare by descriptor address; if the descriptors are the same,
888 compare by the objects they're bound to */
889 a_descr = ((wrapperobject *)a)->descr;
890 b_descr = ((wrapperobject *)b)->descr;
891 if (a_descr == b_descr) {
892 a = ((wrapperobject *)a)->self;
893 b = ((wrapperobject *)b)->self;
894 return PyObject_RichCompare(a, b, op);
895 }
Mark Dickinson211c6252009-02-01 10:28:51 +0000896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 result = a_descr - b_descr;
898 switch (op) {
899 case Py_EQ:
900 v = TEST_COND(result == 0);
901 break;
902 case Py_NE:
903 v = TEST_COND(result != 0);
904 break;
905 case Py_LE:
906 v = TEST_COND(result <= 0);
907 break;
908 case Py_GE:
909 v = TEST_COND(result >= 0);
910 break;
911 case Py_LT:
912 v = TEST_COND(result < 0);
913 break;
914 case Py_GT:
915 v = TEST_COND(result > 0);
916 break;
917 default:
918 PyErr_BadArgument();
919 return NULL;
920 }
921 Py_INCREF(v);
922 return v;
Armin Rigoc6686b72005-11-07 08:38:00 +0000923}
924
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000925static long
926wrapper_hash(wrapperobject *wp)
927{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000928 int x, y;
929 x = _Py_HashPointer(wp->descr);
930 if (x == -1)
931 return -1;
932 y = PyObject_Hash(wp->self);
933 if (y == -1)
934 return -1;
935 x = x ^ y;
936 if (x == -1)
937 x = -2;
938 return x;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000939}
940
Armin Rigoc6686b72005-11-07 08:38:00 +0000941static PyObject *
942wrapper_repr(wrapperobject *wp)
943{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000944 return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
945 wp->descr->d_base->name,
946 wp->self->ob_type->tp_name,
947 wp->self);
Armin Rigoc6686b72005-11-07 08:38:00 +0000948}
949
950static PyMemberDef wrapper_members[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000951 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
952 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000953};
954
955static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000956wrapper_objclass(wrapperobject *wp)
957{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000958 PyObject *c = (PyObject *)wp->descr->d_type;
Armin Rigoc6686b72005-11-07 08:38:00 +0000959
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000960 Py_INCREF(c);
961 return c;
Armin Rigoc6686b72005-11-07 08:38:00 +0000962}
963
964static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000965wrapper_name(wrapperobject *wp)
966{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000967 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000968
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000969 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000970}
971
972static PyObject *
973wrapper_doc(wrapperobject *wp)
974{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000975 const char *s = wp->descr->d_base->doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000976
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000977 if (s == NULL) {
978 Py_INCREF(Py_None);
979 return Py_None;
980 }
981 else {
982 return PyUnicode_FromString(s);
983 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000984}
985
Guido van Rossum32d34c82001-09-20 21:45:26 +0000986static PyGetSetDef wrapper_getsets[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000987 {"__objclass__", (getter)wrapper_objclass},
988 {"__name__", (getter)wrapper_name},
989 {"__doc__", (getter)wrapper_doc},
990 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991};
992
993static PyObject *
994wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
995{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000996 wrapperfunc wrapper = wp->descr->d_base->wrapper;
997 PyObject *self = wp->self;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000999 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
1000 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
1001 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
1002 }
Guido van Rossumc8e56452001-10-22 00:43:43 +00001003
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001004 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
1005 PyErr_Format(PyExc_TypeError,
1006 "wrapper %s doesn't take keyword arguments",
1007 wp->descr->d_base->name);
1008 return NULL;
1009 }
1010 return (*wrapper)(self, args, wp->descr->d_wrapped);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001011}
1012
Guido van Rossum048eb752001-10-02 21:24:57 +00001013static int
1014wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1015{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001016 wrapperobject *wp = (wrapperobject *)self;
1017 Py_VISIT(wp->descr);
1018 Py_VISIT(wp->self);
1019 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001020}
1021
Neil Schemenauer26775122001-10-21 22:26:43 +00001022static PyTypeObject wrappertype = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001023 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1024 "method-wrapper", /* tp_name */
1025 sizeof(wrapperobject), /* tp_basicsize */
1026 0, /* tp_itemsize */
1027 /* methods */
1028 (destructor)wrapper_dealloc, /* tp_dealloc */
1029 0, /* tp_print */
1030 0, /* tp_getattr */
1031 0, /* tp_setattr */
1032 0, /* tp_reserved */
1033 (reprfunc)wrapper_repr, /* tp_repr */
1034 0, /* tp_as_number */
1035 0, /* tp_as_sequence */
1036 0, /* tp_as_mapping */
1037 (hashfunc)wrapper_hash, /* tp_hash */
1038 (ternaryfunc)wrapper_call, /* tp_call */
1039 0, /* tp_str */
1040 PyObject_GenericGetAttr, /* tp_getattro */
1041 0, /* tp_setattro */
1042 0, /* tp_as_buffer */
1043 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1044 0, /* tp_doc */
1045 wrapper_traverse, /* tp_traverse */
1046 0, /* tp_clear */
1047 wrapper_richcompare, /* tp_richcompare */
1048 0, /* tp_weaklistoffset */
1049 0, /* tp_iter */
1050 0, /* tp_iternext */
1051 0, /* tp_methods */
1052 wrapper_members, /* tp_members */
1053 wrapper_getsets, /* tp_getset */
1054 0, /* tp_base */
1055 0, /* tp_dict */
1056 0, /* tp_descr_get */
1057 0, /* tp_descr_set */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001058};
1059
1060PyObject *
1061PyWrapper_New(PyObject *d, PyObject *self)
1062{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001063 wrapperobject *wp;
1064 PyWrapperDescrObject *descr;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001065
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001066 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1067 descr = (PyWrapperDescrObject *)d;
1068 assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001069
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001070 wp = PyObject_GC_New(wrapperobject, &wrappertype);
1071 if (wp != NULL) {
1072 Py_INCREF(descr);
1073 wp->descr = descr;
1074 Py_INCREF(self);
1075 wp->self = self;
1076 _PyObject_GC_TRACK(wp);
1077 }
1078 return (PyObject *)wp;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001080
1081
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001082/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001083
1084/*
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001085 class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001086
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001087 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1088 if doc is None and fget is not None and hasattr(fget, "__doc__"):
1089 doc = fget.__doc__
1090 self.__get = fget
1091 self.__set = fset
1092 self.__del = fdel
1093 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001094
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001095 def __get__(self, inst, type=None):
1096 if inst is None:
1097 return self
1098 if self.__get is None:
1099 raise AttributeError, "unreadable attribute"
1100 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001101
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001102 def __set__(self, inst, value):
1103 if self.__set is None:
1104 raise AttributeError, "can't set attribute"
1105 return self.__set(inst, value)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001106
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001107 def __delete__(self, inst):
1108 if self.__del is None:
1109 raise AttributeError, "can't delete attribute"
1110 return self.__del(inst)
Guido van Rossumba2485f2001-12-10 18:03:34 +00001111
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001112*/
1113
1114typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001115 PyObject_HEAD
1116 PyObject *prop_get;
1117 PyObject *prop_set;
1118 PyObject *prop_del;
1119 PyObject *prop_doc;
1120 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001121} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001122
Christian Heimes0449f632007-12-15 01:27:15 +00001123static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001124 PyObject *, PyObject *);
Christian Heimes0449f632007-12-15 01:27:15 +00001125
Tim Peters66c1a522001-09-24 21:17:50 +00001126static PyMemberDef property_members[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001127 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1128 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1129 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1130 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1131 {0}
Tim Peters66c1a522001-09-24 21:17:50 +00001132};
1133
Christian Heimes0449f632007-12-15 01:27:15 +00001134
Guido van Rossum58da9312007-11-10 23:39:45 +00001135PyDoc_STRVAR(getter_doc,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001136 "Descriptor to change the getter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001137
Neal Norwitz32dde222008-04-15 06:43:13 +00001138static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001139property_getter(PyObject *self, PyObject *getter)
1140{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001141 return property_copy(self, getter, NULL, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001142}
1143
Christian Heimes0449f632007-12-15 01:27:15 +00001144
Guido van Rossum58da9312007-11-10 23:39:45 +00001145PyDoc_STRVAR(setter_doc,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001146 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001147
Neal Norwitz32dde222008-04-15 06:43:13 +00001148static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001149property_setter(PyObject *self, PyObject *setter)
1150{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001151 return property_copy(self, NULL, setter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001152}
1153
Christian Heimes0449f632007-12-15 01:27:15 +00001154
Guido van Rossum58da9312007-11-10 23:39:45 +00001155PyDoc_STRVAR(deleter_doc,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001156 "Descriptor to change the deleter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001157
Neal Norwitz32dde222008-04-15 06:43:13 +00001158static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001159property_deleter(PyObject *self, PyObject *deleter)
1160{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001161 return property_copy(self, NULL, NULL, deleter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001162}
1163
1164
Guido van Rossum58da9312007-11-10 23:39:45 +00001165static PyMethodDef property_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001166 {"getter", property_getter, METH_O, getter_doc},
1167 {"setter", property_setter, METH_O, setter_doc},
1168 {"deleter", property_deleter, METH_O, deleter_doc},
1169 {0}
Guido van Rossum58da9312007-11-10 23:39:45 +00001170};
1171
Tim Peters66c1a522001-09-24 21:17:50 +00001172
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001173static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001174property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001175{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001176 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001177
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001178 _PyObject_GC_UNTRACK(self);
1179 Py_XDECREF(gs->prop_get);
1180 Py_XDECREF(gs->prop_set);
1181 Py_XDECREF(gs->prop_del);
1182 Py_XDECREF(gs->prop_doc);
1183 self->ob_type->tp_free(self);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001184}
1185
1186static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001187property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001188{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001189 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001190
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001191 if (obj == NULL || obj == Py_None) {
1192 Py_INCREF(self);
1193 return self;
1194 }
1195 if (gs->prop_get == NULL) {
1196 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1197 return NULL;
1198 }
1199 return PyObject_CallFunction(gs->prop_get, "(O)", obj);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001200}
1201
1202static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001203property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001204{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001205 propertyobject *gs = (propertyobject *)self;
1206 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001207
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001208 if (value == NULL)
1209 func = gs->prop_del;
1210 else
1211 func = gs->prop_set;
1212 if (func == NULL) {
1213 PyErr_SetString(PyExc_AttributeError,
1214 value == NULL ?
1215 "can't delete attribute" :
1216 "can't set attribute");
1217 return -1;
1218 }
1219 if (value == NULL)
1220 res = PyObject_CallFunction(func, "(O)", obj);
1221 else
1222 res = PyObject_CallFunction(func, "(OO)", obj, value);
1223 if (res == NULL)
1224 return -1;
1225 Py_DECREF(res);
1226 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001227}
1228
Christian Heimes0449f632007-12-15 01:27:15 +00001229static PyObject *
1230property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001231 PyObject *doc)
Christian Heimes0449f632007-12-15 01:27:15 +00001232{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001233 propertyobject *pold = (propertyobject *)old;
1234 PyObject *new, *type;
Christian Heimes0449f632007-12-15 01:27:15 +00001235
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001236 type = PyObject_Type(old);
1237 if (type == NULL)
1238 return NULL;
Christian Heimes0449f632007-12-15 01:27:15 +00001239
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001240 if (get == NULL || get == Py_None) {
1241 Py_XDECREF(get);
1242 get = pold->prop_get ? pold->prop_get : Py_None;
1243 }
1244 if (set == NULL || set == Py_None) {
1245 Py_XDECREF(set);
1246 set = pold->prop_set ? pold->prop_set : Py_None;
1247 }
1248 if (del == NULL || del == Py_None) {
1249 Py_XDECREF(del);
1250 del = pold->prop_del ? pold->prop_del : Py_None;
1251 }
1252 if (doc == NULL || doc == Py_None) {
1253 Py_XDECREF(doc);
1254 if (pold->getter_doc && get != Py_None) {
1255 /* make _init use __doc__ from getter */
1256 doc = Py_None;
1257 }
1258 else {
1259 doc = pold->prop_doc ? pold->prop_doc : Py_None;
1260 }
1261 }
R. David Murrayb18500d2009-05-04 22:59:07 +00001262
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001263 new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
1264 Py_DECREF(type);
1265 if (new == NULL)
1266 return NULL;
1267 return new;
Christian Heimes0449f632007-12-15 01:27:15 +00001268}
1269
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001270static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001271property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001272{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001273 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
1274 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
1275 propertyobject *prop = (propertyobject *)self;
Tim Peters66c1a522001-09-24 21:17:50 +00001276
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001277 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
1278 kwlist, &get, &set, &del, &doc))
1279 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001280
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001281 if (get == Py_None)
1282 get = NULL;
1283 if (set == Py_None)
1284 set = NULL;
1285 if (del == Py_None)
1286 del = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001287
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001288 Py_XINCREF(get);
1289 Py_XINCREF(set);
1290 Py_XINCREF(del);
1291 Py_XINCREF(doc);
Christian Heimes0449f632007-12-15 01:27:15 +00001292
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001293 prop->prop_get = get;
1294 prop->prop_set = set;
1295 prop->prop_del = del;
1296 prop->prop_doc = doc;
1297 prop->getter_doc = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001298
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001299 /* if no docstring given and the getter has one, use that one */
1300 if ((doc == NULL || doc == Py_None) && get != NULL) {
1301 PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");
1302 if (get_doc) {
1303 if (Py_TYPE(self) == &PyProperty_Type) {
1304 Py_XDECREF(prop->prop_doc);
1305 prop->prop_doc = get_doc;
1306 }
1307 else {
1308 /* If this is a property subclass, put __doc__
1309 in dict of the subclass instance instead,
1310 otherwise it gets shadowed by __doc__ in the
1311 class's dict. */
1312 int err = PyObject_SetAttrString(self, "__doc__", get_doc);
1313 Py_DECREF(get_doc);
1314 if (err < 0)
1315 return -1;
1316 }
1317 prop->getter_doc = 1;
1318 }
1319 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
1320 PyErr_Clear();
1321 }
1322 else {
1323 return -1;
1324 }
1325 }
1326
1327 return 0;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001328}
1329
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001330PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001331"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1332"\n"
1333"fget is a function to be used for getting an attribute value, and likewise\n"
1334"fset is a function for setting, and fdel a function for del'ing, an\n"
1335"attribute. Typical use is to define a managed attribute x:\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001336"class C(object):\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001337" def getx(self): return self._x\n"
1338" def setx(self, value): self._x = value\n"
1339" def delx(self): del self._x\n"
1340" x = property(getx, setx, delx, \"I'm the 'x' property.\")\n"
1341"\n"
1342"Decorators make defining new properties or modifying existing ones easy:\n"
1343"class C(object):\n"
1344" @property\n"
1345" def x(self): return self._x\n"
1346" @x.setter\n"
1347" def x(self, value): self._x = value\n"
1348" @x.deleter\n"
1349" def x(self): del self._x\n"
1350);
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001351
Guido van Rossum048eb752001-10-02 21:24:57 +00001352static int
1353property_traverse(PyObject *self, visitproc visit, void *arg)
1354{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001355 propertyobject *pp = (propertyobject *)self;
1356 Py_VISIT(pp->prop_get);
1357 Py_VISIT(pp->prop_set);
1358 Py_VISIT(pp->prop_del);
1359 Py_VISIT(pp->prop_doc);
1360 return 0;
Guido van Rossum048eb752001-10-02 21:24:57 +00001361}
1362
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001363PyTypeObject PyProperty_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001364 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1365 "property", /* tp_name */
1366 sizeof(propertyobject), /* tp_basicsize */
1367 0, /* tp_itemsize */
1368 /* methods */
1369 property_dealloc, /* tp_dealloc */
1370 0, /* tp_print */
1371 0, /* tp_getattr */
1372 0, /* tp_setattr */
1373 0, /* tp_reserved */
1374 0, /* tp_repr */
1375 0, /* tp_as_number */
1376 0, /* tp_as_sequence */
1377 0, /* tp_as_mapping */
1378 0, /* tp_hash */
1379 0, /* tp_call */
1380 0, /* tp_str */
1381 PyObject_GenericGetAttr, /* tp_getattro */
1382 0, /* tp_setattro */
1383 0, /* tp_as_buffer */
1384 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1385 Py_TPFLAGS_BASETYPE, /* tp_flags */
1386 property_doc, /* tp_doc */
1387 property_traverse, /* tp_traverse */
1388 0, /* tp_clear */
1389 0, /* tp_richcompare */
1390 0, /* tp_weaklistoffset */
1391 0, /* tp_iter */
1392 0, /* tp_iternext */
1393 property_methods, /* tp_methods */
1394 property_members, /* tp_members */
1395 0, /* tp_getset */
1396 0, /* tp_base */
1397 0, /* tp_dict */
1398 property_descr_get, /* tp_descr_get */
1399 property_descr_set, /* tp_descr_set */
1400 0, /* tp_dictoffset */
1401 property_init, /* tp_init */
1402 PyType_GenericAlloc, /* tp_alloc */
1403 PyType_GenericNew, /* tp_new */
1404 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001405};