blob: 81e6be957a41d6355febc2ace8d70de53b926c2c [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{
Guido van Rossum048eb752001-10-02 21:24:57 +00009 _PyObject_GC_UNTRACK(descr);
Tim Peters6d6c1a32001-08-02 04:15:00 +000010 Py_XDECREF(descr->d_type);
11 Py_XDECREF(descr->d_name);
Guido van Rossum048eb752001-10-02 21:24:57 +000012 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{
Martin v. Löwis5b222132007-06-10 09:51:05 +000018 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000019 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{
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000026 PyObject *name = NULL;
27 if (descr->d_name != NULL && PyUnicode_Check(descr->d_name))
28 name = descr->d_name;
29
30 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{
Walter Dörwald1ab83302007-05-18 17:15:44 +000036 return descr_repr((PyDescrObject *)descr,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000037 "<method '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000038}
39
40static PyObject *
41member_repr(PyMemberDescrObject *descr)
42{
Walter Dörwald1ab83302007-05-18 17:15:44 +000043 return descr_repr((PyDescrObject *)descr,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000044 "<member '%V' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000045}
46
47static PyObject *
48getset_repr(PyGetSetDescrObject *descr)
49{
Walter Dörwald1ab83302007-05-18 17:15:44 +000050 return descr_repr((PyDescrObject *)descr,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000051 "<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{
Walter Dörwald1ab83302007-05-18 17:15:44 +000057 return descr_repr((PyDescrObject *)descr,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000058 "<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{
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000064 if (obj == NULL) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000065 Py_INCREF(descr);
66 *pres = (PyObject *)descr;
67 return 1;
68 }
Guido van Rossumc588e902002-08-19 16:02:33 +000069 if (!PyObject_TypeCheck(obj, descr->d_type)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000070 PyErr_Format(PyExc_TypeError,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000071 "descriptor '%V' for '%s' objects "
Barry Warsaw7ce36942001-08-24 18:34:26 +000072 "doesn't apply to '%s' object",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000073 descr_name((PyDescrObject *)descr), "?",
Tim Peters6d6c1a32001-08-02 04:15:00 +000074 descr->d_type->tp_name,
75 obj->ob_type->tp_name);
76 *pres = NULL;
77 return 1;
78 }
79 return 0;
80}
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{
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +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,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000092 "descriptor '%V' for type '%s' "
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000093 "needs either an object or a type",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +000094 descr_name((PyDescrObject *)descr), "?",
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000095 descr->d_type->tp_name);
96 return NULL;
97 }
98 }
99 if (!PyType_Check(type)) {
100 PyErr_Format(PyExc_TypeError,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000101 "descriptor '%V' for type '%s' "
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000102 "needs a type, not a '%s' as arg 2",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000103 descr_name((PyDescrObject *)descr), "?",
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000104 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,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000110 "descriptor '%V' for type '%s' "
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000111 "doesn't apply to type '%s'",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000112 descr_name((PyDescrObject *)descr), "?",
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000113 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{
123 PyObject *res;
124
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000125 if (descr_check((PyDescrObject *)descr, obj, &res))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000126 return res;
127 return PyCFunction_New(descr->d_method, obj);
128}
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{
133 PyObject *res;
134
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000135 if (descr_check((PyDescrObject *)descr, obj, &res))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000136 return res;
Guido van Rossum6f799372001-09-20 20:46:19 +0000137 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{
143 PyObject *res;
144
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000145 if (descr_check((PyDescrObject *)descr, obj, &res))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000146 return res;
147 if (descr->d_getset->get != NULL)
148 return descr->d_getset->get(obj, descr->d_getset->closure);
Barry Warsawc8d907c2005-04-19 23:43:40 +0000149 PyErr_Format(PyExc_AttributeError,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000150 "attribute '%V' of '%.100s' objects is not readable",
151 descr_name((PyDescrObject *)descr), "?",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000152 descr->d_type->tp_name);
153 return NULL;
154}
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{
159 PyObject *res;
160
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000161 if (descr_check((PyDescrObject *)descr, obj, &res))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000162 return res;
163 return PyWrapper_New((PyObject *)descr, obj);
164}
165
166static int
167descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
168 int *pres)
169{
170 assert(obj != NULL);
Christian Heimesa156e092008-02-16 07:38:31 +0000171 if (!PyObject_TypeCheck(obj, descr->d_type)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000172 PyErr_Format(PyExc_TypeError,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000173 "descriptor '%V' for '%.100s' objects "
Tim Peters6d6c1a32001-08-02 04:15:00 +0000174 "doesn't apply to '%.100s' object",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000175 descr_name(descr), "?",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000176 descr->d_type->tp_name,
177 obj->ob_type->tp_name);
178 *pres = -1;
179 return 1;
180 }
181 return 0;
182}
183
184static int
185member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
186{
187 int res;
188
189 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
190 return res;
Guido van Rossum6f799372001-09-20 20:46:19 +0000191 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{
197 int res;
198
199 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);
Barry Warsawc8d907c2005-04-19 23:43:40 +0000204 PyErr_Format(PyExc_AttributeError,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000205 "attribute '%V' of '%.100s' objects is not writable",
206 descr_name((PyDescrObject *)descr), "?",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000207 descr->d_type->tp_name);
208 return -1;
209}
210
211static PyObject *
212methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
213{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000214 Py_ssize_t argc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000215 PyObject *self, *func, *result;
216
217 /* 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,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000222 "descriptor '%V' of '%.100s' "
Tim Peters6d6c1a32001-08-02 04:15:00 +0000223 "object needs an argument",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000224 descr_name((PyDescrObject *)descr), "?",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000225 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,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000231 "descriptor '%V' "
Tim Peters6d6c1a32001-08-02 04:15:00 +0000232 "requires a '%.100s' object "
233 "but received a '%.100s'",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000234 descr_name((PyDescrObject *)descr), "?",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000235 descr->d_type->tp_name,
236 self->ob_type->tp_name);
237 return NULL;
238 }
239
240 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;
252}
253
254static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000255classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
256 PyObject *kwds)
257{
258 PyObject *func, *result;
259
260 func = PyCFunction_New(descr->d_method, (PyObject *)descr->d_type);
261 if (func == NULL)
262 return NULL;
263
264 result = PyEval_CallObjectWithKeywords(func, args, kwds);
265 Py_DECREF(func);
266 return result;
267}
268
269static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000270wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
271{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000272 Py_ssize_t argc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000273 PyObject *self, *func, *result;
274
275 /* 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,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000280 "descriptor '%V' of '%.100s' "
Tim Peters6d6c1a32001-08-02 04:15:00 +0000281 "object needs an argument",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000282 descr_name((PyDescrObject *)descr), "?",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000283 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,
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000289 "descriptor '%V' "
Tim Peters6d6c1a32001-08-02 04:15:00 +0000290 "requires a '%.100s' object "
291 "but received a '%.100s'",
Walter Dörwaldd7fb7642007-06-11 16:36:59 +0000292 descr_name((PyDescrObject *)descr), "?",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000293 descr->d_type->tp_name,
294 self->ob_type->tp_name);
295 return NULL;
296 }
297
298 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;
310}
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{
315 if (descr->d_method->ml_doc == NULL) {
316 Py_INCREF(Py_None);
317 return Py_None;
318 }
Neal Norwitza369c5a2007-08-25 07:41:59 +0000319 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[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000323 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
324 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
325 {0}
326};
327
Guido van Rossum32d34c82001-09-20 21:45:26 +0000328static PyGetSetDef method_getset[] = {
Guido van Rossum6f799372001-09-20 20:46:19 +0000329 {"__doc__", (getter)method_get_doc},
330 {0}
331};
332
333static PyObject *
334member_get_doc(PyMemberDescrObject *descr, void *closure)
335{
336 if (descr->d_member->doc == NULL) {
337 Py_INCREF(Py_None);
338 return Py_None;
339 }
Neal Norwitza369c5a2007-08-25 07:41:59 +0000340 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[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000344 {"__doc__", (getter)member_get_doc},
345 {0}
346};
347
348static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000349getset_get_doc(PyGetSetDescrObject *descr, void *closure)
350{
351 if (descr->d_getset->doc == NULL) {
352 Py_INCREF(Py_None);
353 return Py_None;
354 }
Neal Norwitza369c5a2007-08-25 07:41:59 +0000355 return PyUnicode_FromString(descr->d_getset->doc);
Guido van Rossum32d34c82001-09-20 21:45:26 +0000356}
357
358static PyGetSetDef getset_getset[] = {
359 {"__doc__", (getter)getset_get_doc},
360 {0}
361};
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{
366 if (descr->d_base->doc == NULL) {
367 Py_INCREF(Py_None);
368 return Py_None;
369 }
Neal Norwitza369c5a2007-08-25 07:41:59 +0000370 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[] = {
374 {"__doc__", (getter)wrapperdescr_get_doc},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000375 {0}
376};
377
Guido van Rossum048eb752001-10-02 21:24:57 +0000378static int
379descr_traverse(PyObject *self, visitproc visit, void *arg)
380{
381 PyDescrObject *descr = (PyDescrObject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 Py_VISIT(descr->d_type);
Guido van Rossum048eb752001-10-02 21:24:57 +0000383 return 0;
384}
385
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000386PyTypeObject PyMethodDescr_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000387 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000388 "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 */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000395 0, /* tp_reserved */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000396 (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 */
Guido van Rossum048eb752001-10-02 21:24:57 +0000406 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000407 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000408 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000409 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 */
Guido van Rossum6f799372001-09-20 20:46:19 +0000416 method_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000417 0, /* tp_base */
418 0, /* tp_dict */
419 (descrgetfunc)method_get, /* tp_descr_get */
420 0, /* tp_descr_set */
421};
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 = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000425 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000426 "classmethod_descriptor",
Tim Petersbca1cbc2002-12-09 22:56:13 +0000427 sizeof(PyMethodDescrObject),
428 0,
429 (destructor)descr_dealloc, /* tp_dealloc */
430 0, /* tp_print */
431 0, /* tp_getattr */
432 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000433 0, /* tp_reserved */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000434 (reprfunc)method_repr, /* tp_repr */
435 0, /* tp_as_number */
436 0, /* tp_as_sequence */
437 0, /* tp_as_mapping */
438 0, /* tp_hash */
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000439 (ternaryfunc)classmethoddescr_call, /* tp_call */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000440 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 */
459};
460
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000461PyTypeObject PyMemberDescr_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000462 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000463 "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 */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000470 0, /* tp_reserved */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000471 (reprfunc)member_repr, /* tp_repr */
472 0, /* tp_as_number */
473 0, /* tp_as_sequence */
474 0, /* tp_as_mapping */
475 0, /* tp_hash */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000476 0, /* tp_call */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000477 0, /* tp_str */
478 PyObject_GenericGetAttr, /* tp_getattro */
479 0, /* tp_setattro */
480 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000481 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000482 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000483 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000484 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 */
Guido van Rossum6f799372001-09-20 20:46:19 +0000491 member_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000492 0, /* tp_base */
493 0, /* tp_dict */
494 (descrgetfunc)member_get, /* tp_descr_get */
495 (descrsetfunc)member_set, /* tp_descr_set */
496};
497
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000498PyTypeObject PyGetSetDescr_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000499 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000500 "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 */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000507 0, /* tp_reserved */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000508 (reprfunc)getset_repr, /* tp_repr */
509 0, /* tp_as_number */
510 0, /* tp_as_sequence */
511 0, /* tp_as_mapping */
512 0, /* tp_hash */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513 0, /* tp_call */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000514 0, /* tp_str */
515 PyObject_GenericGetAttr, /* tp_getattro */
516 0, /* tp_setattro */
517 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000518 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000519 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000520 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000521 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 */
Guido van Rossum32d34c82001-09-20 21:45:26 +0000528 getset_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000529 0, /* tp_base */
530 0, /* tp_dict */
531 (descrgetfunc)getset_get, /* tp_descr_get */
532 (descrsetfunc)getset_set, /* tp_descr_set */
533};
534
Guido van Rossumf4593e02001-10-03 12:09:30 +0000535PyTypeObject PyWrapperDescr_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000536 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000537 "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 */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000544 0, /* tp_reserved */
Armin Rigoc6686b72005-11-07 08:38:00 +0000545 (reprfunc)wrapperdescr_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000546 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 */
Guido van Rossum048eb752001-10-02 21:24:57 +0000555 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000556 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000557 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000558 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 */
Armin Rigoc6686b72005-11-07 08:38:00 +0000565 wrapperdescr_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000566 0, /* tp_base */
567 0, /* tp_dict */
Armin Rigoc6686b72005-11-07 08:38:00 +0000568 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000569 0, /* tp_descr_set */
570};
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{
575 PyDescrObject *descr;
576
577 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
578 if (descr != NULL) {
579 Py_XINCREF(type);
580 descr->d_type = type;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000581 descr->d_name = PyUnicode_InternFromString(name);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000582 if (descr->d_name == NULL) {
583 Py_DECREF(descr);
584 descr = NULL;
585 }
586 }
587 return descr;
588}
589
590PyObject *
591PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
592{
593 PyMethodDescrObject *descr;
594
595 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
596 type, method->ml_name);
597 if (descr != NULL)
598 descr->d_method = method;
599 return (PyObject *)descr;
600}
601
602PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000603PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
604{
605 PyMethodDescrObject *descr;
606
607 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
608 type, method->ml_name);
609 if (descr != NULL)
610 descr->d_method = method;
611 return (PyObject *)descr;
612}
613
614PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000615PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000616{
617 PyMemberDescrObject *descr;
618
619 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
620 type, member->name);
621 if (descr != NULL)
622 descr->d_member = member;
623 return (PyObject *)descr;
624}
625
626PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000627PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000628{
629 PyGetSetDescrObject *descr;
630
631 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
632 type, getset->name);
633 if (descr != NULL)
634 descr->d_getset = getset;
635 return (PyObject *)descr;
636}
637
638PyObject *
639PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
640{
641 PyWrapperDescrObject *descr;
642
643 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;
650}
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 {
659 PyObject_HEAD
660 PyObject *dict;
661} 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{
666 return PyObject_Size(pp->dict);
667}
668
669static PyObject *
670proxy_getitem(proxyobject *pp, PyObject *key)
671{
672 return PyObject_GetItem(pp->dict, key);
673}
674
675static PyMappingMethods proxy_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000676 (lenfunc)proxy_len, /* mp_length */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677 (binaryfunc)proxy_getitem, /* mp_subscript */
678 0, /* mp_ass_subscript */
679};
680
681static int
682proxy_contains(proxyobject *pp, PyObject *key)
683{
Raymond Hettingerd3ae6722003-12-13 11:58:56 +0000684 return PyDict_Contains(pp->dict, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000685}
686
687static PySequenceMethods proxy_as_sequence = {
688 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 */
698};
699
700static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000701proxy_get(proxyobject *pp, PyObject *args)
702{
703 PyObject *key, *def = Py_None;
704
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000705 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706 return NULL;
707 return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
708}
709
710static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000711proxy_keys(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000713 return PyMapping_Keys(pp->dict);
714}
715
716static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000717proxy_values(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000718{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719 return PyMapping_Values(pp->dict);
720}
721
722static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000723proxy_items(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000724{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725 return PyMapping_Items(pp->dict);
726}
727
728static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000729proxy_copy(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731 return PyObject_CallMethod(pp->dict, "copy", NULL);
732}
733
734static PyMethodDef proxy_methods[] = {
Guido van Rossum59610422002-04-13 14:06:36 +0000735 {"get", (PyCFunction)proxy_get, METH_VARARGS,
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000736 PyDoc_STR("D.get(k[,d]) -> D[k] if k in D, else d."
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000737 " d defaults to None.")},
Guido van Rossum59610422002-04-13 14:06:36 +0000738 {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000739 PyDoc_STR("D.keys() -> list of D's keys")},
Guido van Rossum59610422002-04-13 14:06:36 +0000740 {"values", (PyCFunction)proxy_values, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000741 PyDoc_STR("D.values() -> list of D's values")},
Guido van Rossum59610422002-04-13 14:06:36 +0000742 {"items", (PyCFunction)proxy_items, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000743 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Guido van Rossum59610422002-04-13 14:06:36 +0000744 {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000745 PyDoc_STR("D.copy() -> a shallow copy of D")},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746 {0}
747};
748
749static void
750proxy_dealloc(proxyobject *pp)
751{
Guido van Rossum048eb752001-10-02 21:24:57 +0000752 _PyObject_GC_UNTRACK(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753 Py_DECREF(pp->dict);
Guido van Rossum048eb752001-10-02 21:24:57 +0000754 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755}
756
757static PyObject *
758proxy_getiter(proxyobject *pp)
759{
760 return PyObject_GetIter(pp->dict);
761}
762
Neil Schemenauer26775122001-10-21 22:26:43 +0000763static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000764proxy_str(proxyobject *pp)
765{
766 return PyObject_Str(pp->dict);
767}
768
Guido van Rossum048eb752001-10-02 21:24:57 +0000769static int
770proxy_traverse(PyObject *self, visitproc visit, void *arg)
771{
772 proxyobject *pp = (proxyobject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000773 Py_VISIT(pp->dict);
Guido van Rossum048eb752001-10-02 21:24:57 +0000774 return 0;
775}
776
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000777static PyObject *
778proxy_richcompare(proxyobject *v, PyObject *w, int op)
779{
780 return PyObject_RichCompare(v->dict, w, op);
781}
782
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000783PyTypeObject PyDictProxy_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000784 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumf77c9292007-11-30 23:30:40 +0000785 "dict_proxy", /* tp_name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 sizeof(proxyobject), /* tp_basicsize */
787 0, /* tp_itemsize */
788 /* methods */
789 (destructor)proxy_dealloc, /* tp_dealloc */
790 0, /* tp_print */
791 0, /* tp_getattr */
792 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000793 0, /* tp_reserved */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000794 0, /* tp_repr */
795 0, /* tp_as_number */
796 &proxy_as_sequence, /* tp_as_sequence */
797 &proxy_as_mapping, /* tp_as_mapping */
798 0, /* tp_hash */
799 0, /* tp_call */
800 (reprfunc)proxy_str, /* tp_str */
801 PyObject_GenericGetAttr, /* tp_getattro */
802 0, /* tp_setattro */
803 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000804 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000806 proxy_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000807 0, /* tp_clear */
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000808 (richcmpfunc)proxy_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000809 0, /* tp_weaklistoffset */
810 (getiterfunc)proxy_getiter, /* tp_iter */
811 0, /* tp_iternext */
812 proxy_methods, /* tp_methods */
813 0, /* tp_members */
814 0, /* tp_getset */
815 0, /* tp_base */
816 0, /* tp_dict */
817 0, /* tp_descr_get */
818 0, /* tp_descr_set */
819};
820
821PyObject *
822PyDictProxy_New(PyObject *dict)
823{
824 proxyobject *pp;
825
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000826 pp = PyObject_GC_New(proxyobject, &PyDictProxy_Type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000827 if (pp != NULL) {
828 Py_INCREF(dict);
829 pp->dict = dict;
Guido van Rossum048eb752001-10-02 21:24:57 +0000830 _PyObject_GC_TRACK(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000831 }
832 return (PyObject *)pp;
833}
834
835
836/* --- Wrapper object for "slot" methods --- */
837
838/* This has no reason to be in this file except that adding new files is a
839 bit of a pain */
840
Mark Dickinson211c6252009-02-01 10:28:51 +0000841/* forward */
842static PyTypeObject wrappertype;
843
Tim Peters6d6c1a32001-08-02 04:15:00 +0000844typedef struct {
845 PyObject_HEAD
846 PyWrapperDescrObject *descr;
847 PyObject *self;
848} wrapperobject;
849
Mark Dickinson211c6252009-02-01 10:28:51 +0000850#define Wrapper_Check(v) (Py_TYPE(v) == &wrappertype)
851
Tim Peters6d6c1a32001-08-02 04:15:00 +0000852static void
853wrapper_dealloc(wrapperobject *wp)
854{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000855 PyObject_GC_UnTrack(wp);
856 Py_TRASHCAN_SAFE_BEGIN(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000857 Py_XDECREF(wp->descr);
858 Py_XDECREF(wp->self);
Guido van Rossum048eb752001-10-02 21:24:57 +0000859 PyObject_GC_Del(wp);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000860 Py_TRASHCAN_SAFE_END(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000861}
862
Mark Dickinson211c6252009-02-01 10:28:51 +0000863#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
864
865static PyObject *
866wrapper_richcompare(PyObject *a, PyObject *b, int op)
Armin Rigoc6686b72005-11-07 08:38:00 +0000867{
Mark Dickinson211c6252009-02-01 10:28:51 +0000868 int result;
869 PyObject *v;
870 PyWrapperDescrObject *a_descr, *b_descr;
871
872 assert(a != NULL && b != NULL);
873
874 /* both arguments should be wrapperobjects */
875 if (!Wrapper_Check(a) || !Wrapper_Check(b)) {
876 v = Py_NotImplemented;
877 Py_INCREF(v);
878 return v;
879 }
880
881 /* compare by descriptor address; if the descriptors are the same,
882 compare by the objects they're bound to */
883 a_descr = ((wrapperobject *)a)->descr;
884 b_descr = ((wrapperobject *)b)->descr;
885 if (a_descr == b_descr) {
886 a = ((wrapperobject *)a)->self;
887 b = ((wrapperobject *)b)->self;
888 return PyObject_RichCompare(a, b, op);
889 }
890
891 result = a_descr - b_descr;
892 switch (op) {
893 case Py_EQ:
894 v = TEST_COND(result == 0);
895 break;
896 case Py_NE:
897 v = TEST_COND(result != 0);
898 break;
899 case Py_LE:
900 v = TEST_COND(result <= 0);
901 break;
902 case Py_GE:
903 v = TEST_COND(result >= 0);
904 break;
905 case Py_LT:
906 v = TEST_COND(result < 0);
907 break;
908 case Py_GT:
909 v = TEST_COND(result > 0);
910 break;
911 default:
912 PyErr_BadArgument();
913 return NULL;
914 }
915 Py_INCREF(v);
916 return v;
Armin Rigoc6686b72005-11-07 08:38:00 +0000917}
918
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000919static long
920wrapper_hash(wrapperobject *wp)
921{
922 int x, y;
923 x = _Py_HashPointer(wp->descr);
924 if (x == -1)
925 return -1;
926 y = PyObject_Hash(wp->self);
927 if (y == -1)
928 return -1;
929 x = x ^ y;
930 if (x == -1)
931 x = -2;
932 return x;
933}
934
Armin Rigoc6686b72005-11-07 08:38:00 +0000935static PyObject *
936wrapper_repr(wrapperobject *wp)
937{
Walter Dörwald1ab83302007-05-18 17:15:44 +0000938 return PyUnicode_FromFormat("<method-wrapper '%s' of %s object at %p>",
Armin Rigoc6686b72005-11-07 08:38:00 +0000939 wp->descr->d_base->name,
940 wp->self->ob_type->tp_name,
941 wp->self);
942}
943
944static PyMemberDef wrapper_members[] = {
945 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946 {0}
947};
948
949static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000950wrapper_objclass(wrapperobject *wp)
951{
952 PyObject *c = (PyObject *)wp->descr->d_type;
953
954 Py_INCREF(c);
955 return c;
956}
957
958static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000959wrapper_name(wrapperobject *wp)
960{
Neal Norwitza369c5a2007-08-25 07:41:59 +0000961 const char *s = wp->descr->d_base->name;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000962
Neal Norwitza369c5a2007-08-25 07:41:59 +0000963 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000964}
965
966static PyObject *
967wrapper_doc(wrapperobject *wp)
968{
Neal Norwitza369c5a2007-08-25 07:41:59 +0000969 const char *s = wp->descr->d_base->doc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000970
971 if (s == NULL) {
972 Py_INCREF(Py_None);
973 return Py_None;
974 }
975 else {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000976 return PyUnicode_FromString(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000977 }
978}
979
Guido van Rossum32d34c82001-09-20 21:45:26 +0000980static PyGetSetDef wrapper_getsets[] = {
Armin Rigoc6686b72005-11-07 08:38:00 +0000981 {"__objclass__", (getter)wrapper_objclass},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982 {"__name__", (getter)wrapper_name},
983 {"__doc__", (getter)wrapper_doc},
984 {0}
985};
986
987static PyObject *
988wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
989{
990 wrapperfunc wrapper = wp->descr->d_base->wrapper;
991 PyObject *self = wp->self;
992
Guido van Rossumc8e56452001-10-22 00:43:43 +0000993 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
994 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
995 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
996 }
997
998 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
999 PyErr_Format(PyExc_TypeError,
1000 "wrapper %s doesn't take keyword arguments",
1001 wp->descr->d_base->name);
1002 return NULL;
1003 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001004 return (*wrapper)(self, args, wp->descr->d_wrapped);
1005}
1006
Guido van Rossum048eb752001-10-02 21:24:57 +00001007static int
1008wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1009{
1010 wrapperobject *wp = (wrapperobject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001011 Py_VISIT(wp->descr);
1012 Py_VISIT(wp->self);
Guido van Rossum048eb752001-10-02 21:24:57 +00001013 return 0;
1014}
1015
Neil Schemenauer26775122001-10-21 22:26:43 +00001016static PyTypeObject wrappertype = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001017 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001018 "method-wrapper", /* tp_name */
1019 sizeof(wrapperobject), /* tp_basicsize */
1020 0, /* tp_itemsize */
1021 /* methods */
1022 (destructor)wrapper_dealloc, /* tp_dealloc */
1023 0, /* tp_print */
1024 0, /* tp_getattr */
1025 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001026 0, /* tp_reserved */
Armin Rigoc6686b72005-11-07 08:38:00 +00001027 (reprfunc)wrapper_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001028 0, /* tp_as_number */
1029 0, /* tp_as_sequence */
1030 0, /* tp_as_mapping */
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001031 (hashfunc)wrapper_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032 (ternaryfunc)wrapper_call, /* tp_call */
1033 0, /* tp_str */
1034 PyObject_GenericGetAttr, /* tp_getattro */
1035 0, /* tp_setattro */
1036 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001037 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001038 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001039 wrapper_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001040 0, /* tp_clear */
Mark Dickinson211c6252009-02-01 10:28:51 +00001041 wrapper_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001042 0, /* tp_weaklistoffset */
1043 0, /* tp_iter */
1044 0, /* tp_iternext */
Armin Rigoc6686b72005-11-07 08:38:00 +00001045 0, /* tp_methods */
1046 wrapper_members, /* tp_members */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001047 wrapper_getsets, /* tp_getset */
1048 0, /* tp_base */
1049 0, /* tp_dict */
1050 0, /* tp_descr_get */
1051 0, /* tp_descr_set */
1052};
1053
1054PyObject *
1055PyWrapper_New(PyObject *d, PyObject *self)
1056{
1057 wrapperobject *wp;
1058 PyWrapperDescrObject *descr;
1059
1060 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1061 descr = (PyWrapperDescrObject *)d;
1062 assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
1063
Guido van Rossum048eb752001-10-02 21:24:57 +00001064 wp = PyObject_GC_New(wrapperobject, &wrappertype);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001065 if (wp != NULL) {
1066 Py_INCREF(descr);
1067 wp->descr = descr;
1068 Py_INCREF(self);
1069 wp->self = self;
Guido van Rossum048eb752001-10-02 21:24:57 +00001070 _PyObject_GC_TRACK(wp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001071 }
1072 return (PyObject *)wp;
1073}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001074
1075
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001076/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001077
1078/*
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001079 class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001080
Guido van Rossumba2485f2001-12-10 18:03:34 +00001081 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
Georg Brandl533ff6f2006-03-08 18:09:27 +00001082 if doc is None and fget is not None and hasattr(fget, "__doc__"):
1083 doc = fget.__doc__
Guido van Rossumba2485f2001-12-10 18:03:34 +00001084 self.__get = fget
1085 self.__set = fset
1086 self.__del = fdel
1087 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001088
Guido van Rossumba2485f2001-12-10 18:03:34 +00001089 def __get__(self, inst, type=None):
Guido van Rossumba2485f2001-12-10 18:03:34 +00001090 if inst is None:
1091 return self
Guido van Rossum7171f1c2001-12-10 18:06:21 +00001092 if self.__get is None:
1093 raise AttributeError, "unreadable attribute"
Guido van Rossumba2485f2001-12-10 18:03:34 +00001094 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001095
Guido van Rossumba2485f2001-12-10 18:03:34 +00001096 def __set__(self, inst, value):
1097 if self.__set is None:
1098 raise AttributeError, "can't set attribute"
1099 return self.__set(inst, value)
1100
1101 def __delete__(self, inst):
1102 if self.__del is None:
1103 raise AttributeError, "can't delete attribute"
1104 return self.__del(inst)
1105
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001106*/
1107
1108typedef struct {
1109 PyObject_HEAD
Tim Peters66c1a522001-09-24 21:17:50 +00001110 PyObject *prop_get;
1111 PyObject *prop_set;
1112 PyObject *prop_del;
1113 PyObject *prop_doc;
Christian Heimes0449f632007-12-15 01:27:15 +00001114 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001115} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001116
Christian Heimes0449f632007-12-15 01:27:15 +00001117static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
1118 PyObject *, PyObject *);
1119
Tim Peters66c1a522001-09-24 21:17:50 +00001120static PyMemberDef property_members[] = {
1121 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1122 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1123 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1124 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1125 {0}
1126};
1127
Christian Heimes0449f632007-12-15 01:27:15 +00001128
Guido van Rossum58da9312007-11-10 23:39:45 +00001129PyDoc_STRVAR(getter_doc,
1130 "Descriptor to change the getter on a property.");
1131
Neal Norwitz32dde222008-04-15 06:43:13 +00001132static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001133property_getter(PyObject *self, PyObject *getter)
1134{
Christian Heimes0449f632007-12-15 01:27:15 +00001135 return property_copy(self, getter, NULL, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001136}
1137
Christian Heimes0449f632007-12-15 01:27:15 +00001138
Guido van Rossum58da9312007-11-10 23:39:45 +00001139PyDoc_STRVAR(setter_doc,
Christian Heimes0449f632007-12-15 01:27:15 +00001140 "Descriptor to change the setter on a property.");
Guido van Rossum58da9312007-11-10 23:39:45 +00001141
Neal Norwitz32dde222008-04-15 06:43:13 +00001142static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001143property_setter(PyObject *self, PyObject *setter)
1144{
Christian Heimes0449f632007-12-15 01:27:15 +00001145 return property_copy(self, NULL, setter, NULL, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001146}
1147
Christian Heimes0449f632007-12-15 01:27:15 +00001148
Guido van Rossum58da9312007-11-10 23:39:45 +00001149PyDoc_STRVAR(deleter_doc,
1150 "Descriptor to change the deleter on a property.");
1151
Neal Norwitz32dde222008-04-15 06:43:13 +00001152static PyObject *
Guido van Rossum58da9312007-11-10 23:39:45 +00001153property_deleter(PyObject *self, PyObject *deleter)
1154{
Christian Heimes0449f632007-12-15 01:27:15 +00001155 return property_copy(self, NULL, NULL, deleter, NULL);
Guido van Rossum58da9312007-11-10 23:39:45 +00001156}
1157
1158
Guido van Rossum58da9312007-11-10 23:39:45 +00001159static PyMethodDef property_methods[] = {
1160 {"getter", property_getter, METH_O, getter_doc},
1161 {"setter", property_setter, METH_O, setter_doc},
1162 {"deleter", property_deleter, METH_O, deleter_doc},
1163 {0}
1164};
1165
Tim Peters66c1a522001-09-24 21:17:50 +00001166
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001167static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001168property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001169{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001170 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001171
Guido van Rossum048eb752001-10-02 21:24:57 +00001172 _PyObject_GC_UNTRACK(self);
Tim Peters66c1a522001-09-24 21:17:50 +00001173 Py_XDECREF(gs->prop_get);
1174 Py_XDECREF(gs->prop_set);
1175 Py_XDECREF(gs->prop_del);
1176 Py_XDECREF(gs->prop_doc);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001177 self->ob_type->tp_free(self);
1178}
1179
1180static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001181property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001182{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001183 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001184
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001185 if (obj == NULL || obj == Py_None) {
1186 Py_INCREF(self);
1187 return self;
1188 }
Guido van Rossumb75ba912001-12-10 18:00:15 +00001189 if (gs->prop_get == NULL) {
1190 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1191 return NULL;
1192 }
Tim Peters66c1a522001-09-24 21:17:50 +00001193 return PyObject_CallFunction(gs->prop_get, "(O)", obj);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001194}
1195
1196static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001197property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001198{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001199 propertyobject *gs = (propertyobject *)self;
Guido van Rossum271410a2001-08-24 15:23:20 +00001200 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001201
Guido van Rossum271410a2001-08-24 15:23:20 +00001202 if (value == NULL)
Tim Peters66c1a522001-09-24 21:17:50 +00001203 func = gs->prop_del;
Guido van Rossum271410a2001-08-24 15:23:20 +00001204 else
Tim Peters66c1a522001-09-24 21:17:50 +00001205 func = gs->prop_set;
Guido van Rossum271410a2001-08-24 15:23:20 +00001206 if (func == NULL) {
1207 PyErr_SetString(PyExc_AttributeError,
1208 value == NULL ?
1209 "can't delete attribute" :
1210 "can't set attribute");
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001211 return -1;
1212 }
Guido van Rossum845fc482001-08-24 10:17:36 +00001213 if (value == NULL)
Guido van Rossum271410a2001-08-24 15:23:20 +00001214 res = PyObject_CallFunction(func, "(O)", obj);
Guido van Rossum845fc482001-08-24 10:17:36 +00001215 else
Guido van Rossum271410a2001-08-24 15:23:20 +00001216 res = PyObject_CallFunction(func, "(OO)", obj, value);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001217 if (res == NULL)
1218 return -1;
1219 Py_DECREF(res);
1220 return 0;
1221}
1222
Christian Heimes0449f632007-12-15 01:27:15 +00001223static PyObject *
1224property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del,
1225 PyObject *doc)
1226{
1227 propertyobject *pold = (propertyobject *)old;
Christian Heimes0449f632007-12-15 01:27:15 +00001228 PyObject *new, *type;
1229
1230 type = PyObject_Type(old);
1231 if (type == NULL)
1232 return NULL;
1233
1234 if (get == NULL || get == Py_None) {
1235 Py_XDECREF(get);
1236 get = pold->prop_get ? pold->prop_get : Py_None;
1237 }
1238 if (set == NULL || set == Py_None) {
1239 Py_XDECREF(set);
1240 set = pold->prop_set ? pold->prop_set : Py_None;
1241 }
1242 if (del == NULL || del == Py_None) {
1243 Py_XDECREF(del);
1244 del = pold->prop_del ? pold->prop_del : Py_None;
1245 }
1246 if (doc == NULL || doc == Py_None) {
1247 Py_XDECREF(doc);
R. David Murrayb18500d2009-05-04 22:59:07 +00001248 if (pold->getter_doc && get != Py_None) {
1249 /* make _init use __doc__ from getter */
1250 doc = Py_None;
1251 }
1252 else {
1253 doc = pold->prop_doc ? pold->prop_doc : Py_None;
1254 }
Christian Heimes0449f632007-12-15 01:27:15 +00001255 }
R. David Murrayb18500d2009-05-04 22:59:07 +00001256
Christian Heimes0449f632007-12-15 01:27:15 +00001257 new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
Christian Heimesd9a4d1d2008-01-01 14:42:15 +00001258 Py_DECREF(type);
Christian Heimes0449f632007-12-15 01:27:15 +00001259 if (new == NULL)
1260 return NULL;
Christian Heimes0449f632007-12-15 01:27:15 +00001261 return new;
1262}
1263
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001264static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001265property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001266{
Tim Peters66c1a522001-09-24 21:17:50 +00001267 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001268 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
Christian Heimes0449f632007-12-15 01:27:15 +00001269 propertyobject *prop = (propertyobject *)self;
1270
Tim Peters66c1a522001-09-24 21:17:50 +00001271 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
Christian Heimes0449f632007-12-15 01:27:15 +00001272 kwlist, &get, &set, &del, &doc))
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001273 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001274
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001275 if (get == Py_None)
1276 get = NULL;
1277 if (set == Py_None)
1278 set = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001279 if (del == Py_None)
1280 del = NULL;
1281
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001282 Py_XINCREF(get);
1283 Py_XINCREF(set);
Guido van Rossum271410a2001-08-24 15:23:20 +00001284 Py_XINCREF(del);
Tim Peters66c1a522001-09-24 21:17:50 +00001285 Py_XINCREF(doc);
1286
Christian Heimes0449f632007-12-15 01:27:15 +00001287 prop->prop_get = get;
1288 prop->prop_set = set;
1289 prop->prop_del = del;
1290 prop->prop_doc = doc;
1291 prop->getter_doc = 0;
1292
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001293 /* if no docstring given and the getter has one, use that one */
1294 if ((doc == NULL || doc == Py_None) && get != NULL) {
1295 PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");
Benjamin Peterson8f6713f2009-11-13 02:29:35 +00001296 if (get_doc) {
1297 if (Py_TYPE(self) == &PyProperty_Type) {
R. David Murrayb18500d2009-05-04 22:59:07 +00001298 Py_XDECREF(prop->prop_doc);
1299 prop->prop_doc = get_doc;
Benjamin Peterson8f6713f2009-11-13 02:29:35 +00001300 }
1301 else {
1302 /* If this is a property subclass, put __doc__
1303 in dict of the subclass instance instead,
1304 otherwise it gets shadowed by __doc__ in the
1305 class's dict. */
1306 int err = PyObject_SetAttrString(self, "__doc__", get_doc);
1307 Py_DECREF(get_doc);
1308 if (err < 0)
R. David Murrayb18500d2009-05-04 22:59:07 +00001309 return -1;
R. David Murrayb18500d2009-05-04 22:59:07 +00001310 }
Christian Heimes0449f632007-12-15 01:27:15 +00001311 prop->getter_doc = 1;
Benjamin Peterson8f6713f2009-11-13 02:29:35 +00001312 }
1313 else if (PyErr_ExceptionMatches(PyExc_Exception)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001314 PyErr_Clear();
1315 }
Benjamin Peterson8f6713f2009-11-13 02:29:35 +00001316 else {
1317 return -1;
1318 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001319 }
1320
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001321 return 0;
1322}
1323
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001324PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001325"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1326"\n"
1327"fget is a function to be used for getting an attribute value, and likewise\n"
1328"fset is a function for setting, and fdel a function for del'ing, an\n"
1329"attribute. Typical use is to define a managed attribute x:\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001330"class C(object):\n"
Christian Heimes96f31632007-11-12 01:32:03 +00001331" def getx(self): return self._x\n"
1332" def setx(self, value): self._x = value\n"
1333" def delx(self): del self._x\n"
1334" x = property(getx, setx, delx, \"I'm the 'x' property.\")\n"
1335"\n"
1336"Decorators make defining new properties or modifying existing ones easy:\n"
1337"class C(object):\n"
1338" @property\n"
1339" def x(self): return self._x\n"
1340" @x.setter\n"
1341" def x(self, value): self._x = value\n"
1342" @x.deleter\n"
1343" def x(self): del self._x\n"
1344);
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001345
Guido van Rossum048eb752001-10-02 21:24:57 +00001346static int
1347property_traverse(PyObject *self, visitproc visit, void *arg)
1348{
1349 propertyobject *pp = (propertyobject *)self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001350 Py_VISIT(pp->prop_get);
1351 Py_VISIT(pp->prop_set);
1352 Py_VISIT(pp->prop_del);
1353 Py_VISIT(pp->prop_doc);
Guido van Rossum048eb752001-10-02 21:24:57 +00001354 return 0;
1355}
1356
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001357PyTypeObject PyProperty_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001358 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001359 "property", /* tp_name */
1360 sizeof(propertyobject), /* tp_basicsize */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001361 0, /* tp_itemsize */
1362 /* methods */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001363 property_dealloc, /* tp_dealloc */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001364 0, /* tp_print */
1365 0, /* tp_getattr */
1366 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001367 0, /* tp_reserved */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001368 0, /* tp_repr */
1369 0, /* tp_as_number */
1370 0, /* tp_as_sequence */
1371 0, /* tp_as_mapping */
1372 0, /* tp_hash */
1373 0, /* tp_call */
1374 0, /* tp_str */
1375 PyObject_GenericGetAttr, /* tp_getattro */
1376 0, /* tp_setattro */
1377 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001378 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1379 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001380 property_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001381 property_traverse, /* tp_traverse */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001382 0, /* tp_clear */
1383 0, /* tp_richcompare */
1384 0, /* tp_weaklistoffset */
1385 0, /* tp_iter */
1386 0, /* tp_iternext */
Guido van Rossum58da9312007-11-10 23:39:45 +00001387 property_methods, /* tp_methods */
Tim Peters66c1a522001-09-24 21:17:50 +00001388 property_members, /* tp_members */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001389 0, /* tp_getset */
1390 0, /* tp_base */
1391 0, /* tp_dict */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001392 property_descr_get, /* tp_descr_get */
1393 property_descr_set, /* tp_descr_set */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001394 0, /* tp_dictoffset */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001395 property_init, /* tp_init */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001396 PyType_GenericAlloc, /* tp_alloc */
1397 PyType_GenericNew, /* tp_new */
Neil Schemenauer99b5d282002-04-12 02:44:22 +00001398 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001399};