blob: 77ef3599800ef6e7c8641e5acd9434abd0cdec67 [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
15static char *
16descr_name(PyDescrObject *descr)
17{
18 if (descr->d_name != NULL && PyString_Check(descr->d_name))
19 return PyString_AS_STRING(descr->d_name);
20 else
21 return "?";
22}
23
24static PyObject *
25descr_repr(PyDescrObject *descr, char *format)
26{
Barry Warsaw7ce36942001-08-24 18:34:26 +000027 return PyString_FromFormat(format, descr_name(descr),
28 descr->d_type->tp_name);
Tim Peters6d6c1a32001-08-02 04:15:00 +000029}
30
31static PyObject *
32method_repr(PyMethodDescrObject *descr)
33{
34 return descr_repr((PyDescrObject *)descr,
Barry Warsaw7ce36942001-08-24 18:34:26 +000035 "<method '%s' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000036}
37
38static PyObject *
39member_repr(PyMemberDescrObject *descr)
40{
41 return descr_repr((PyDescrObject *)descr,
Barry Warsaw7ce36942001-08-24 18:34:26 +000042 "<member '%s' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000043}
44
45static PyObject *
46getset_repr(PyGetSetDescrObject *descr)
47{
48 return descr_repr((PyDescrObject *)descr,
Barry Warsaw7ce36942001-08-24 18:34:26 +000049 "<attribute '%s' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000050}
51
52static PyObject *
53wrapper_repr(PyWrapperDescrObject *descr)
54{
55 return descr_repr((PyDescrObject *)descr,
Barry Warsaw7ce36942001-08-24 18:34:26 +000056 "<slot wrapper '%s' of '%s' objects>");
Tim Peters6d6c1a32001-08-02 04:15:00 +000057}
58
59static int
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000060descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
Tim Peters6d6c1a32001-08-02 04:15:00 +000061{
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000062 if (obj == NULL) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000063 Py_INCREF(descr);
64 *pres = (PyObject *)descr;
65 return 1;
66 }
Guido van Rossumc588e902002-08-19 16:02:33 +000067 if (!PyObject_TypeCheck(obj, descr->d_type)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000068 PyErr_Format(PyExc_TypeError,
Barry Warsaw7ce36942001-08-24 18:34:26 +000069 "descriptor '%s' for '%s' objects "
70 "doesn't apply to '%s' object",
Tim Peters6d6c1a32001-08-02 04:15:00 +000071 descr_name((PyDescrObject *)descr),
72 descr->d_type->tp_name,
73 obj->ob_type->tp_name);
74 *pres = NULL;
75 return 1;
76 }
77 return 0;
78}
79
80static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000081classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Petersbca1cbc2002-12-09 22:56:13 +000082{
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +000083 /* Ensure a valid type. Class methods ignore obj. */
84 if (type == NULL) {
85 if (obj != NULL)
86 type = (PyObject *)obj->ob_type;
87 else {
88 /* Wot - no type?! */
89 PyErr_Format(PyExc_TypeError,
90 "descriptor '%s' for type '%s' "
91 "needs either an object or a type",
92 descr_name((PyDescrObject *)descr),
93 descr->d_type->tp_name);
94 return NULL;
95 }
96 }
97 if (!PyType_Check(type)) {
98 PyErr_Format(PyExc_TypeError,
99 "descriptor '%s' for type '%s' "
100 "needs a type, not a '%s' as arg 2",
101 descr_name((PyDescrObject *)descr),
102 descr->d_type->tp_name,
103 type->ob_type->tp_name);
104 return NULL;
105 }
106 if (!PyType_IsSubtype((PyTypeObject *)type, descr->d_type)) {
107 PyErr_Format(PyExc_TypeError,
108 "descriptor '%s' for type '%s' "
109 "doesn't apply to type '%s'",
110 descr_name((PyDescrObject *)descr),
111 descr->d_type->tp_name,
112 ((PyTypeObject *)type)->tp_name);
113 return NULL;
114 }
115 return PyCFunction_New(descr->d_method, type);
Tim Petersbca1cbc2002-12-09 22:56:13 +0000116}
117
118static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000119method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000120{
121 PyObject *res;
122
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000123 if (descr_check((PyDescrObject *)descr, obj, &res))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000124 return res;
125 return PyCFunction_New(descr->d_method, obj);
126}
127
128static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000129member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000130{
131 PyObject *res;
132
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000133 if (descr_check((PyDescrObject *)descr, obj, &res))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000134 return res;
Guido van Rossum6f799372001-09-20 20:46:19 +0000135 return PyMember_GetOne((char *)obj, descr->d_member);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000136}
137
138static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000139getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000140{
141 PyObject *res;
142
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000143 if (descr_check((PyDescrObject *)descr, obj, &res))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000144 return res;
145 if (descr->d_getset->get != NULL)
146 return descr->d_getset->get(obj, descr->d_getset->closure);
147 PyErr_Format(PyExc_TypeError,
Guido van Rossum2b8235e2001-12-15 05:00:30 +0000148 "attribute '%.300s' of '%.100s' objects is not readable",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000149 descr_name((PyDescrObject *)descr),
150 descr->d_type->tp_name);
151 return NULL;
152}
153
154static PyObject *
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000155wrapper_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000156{
157 PyObject *res;
158
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000159 if (descr_check((PyDescrObject *)descr, obj, &res))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000160 return res;
161 return PyWrapper_New((PyObject *)descr, obj);
162}
163
164static int
165descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
166 int *pres)
167{
168 assert(obj != NULL);
169 if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) {
170 PyErr_Format(PyExc_TypeError,
171 "descriptor '%.200s' for '%.100s' objects "
172 "doesn't apply to '%.100s' object",
173 descr_name(descr),
174 descr->d_type->tp_name,
175 obj->ob_type->tp_name);
176 *pres = -1;
177 return 1;
178 }
179 return 0;
180}
181
182static int
183member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
184{
185 int res;
186
187 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
188 return res;
Guido van Rossum6f799372001-09-20 20:46:19 +0000189 return PyMember_SetOne((char *)obj, descr->d_member, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000190}
191
192static int
193getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
194{
195 int res;
196
197 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
198 return res;
199 if (descr->d_getset->set != NULL)
200 return descr->d_getset->set(obj, value,
201 descr->d_getset->closure);
202 PyErr_Format(PyExc_TypeError,
Guido van Rossum2b8235e2001-12-15 05:00:30 +0000203 "attribute '%.300s' of '%.100s' objects is not writable",
Tim Peters6d6c1a32001-08-02 04:15:00 +0000204 descr_name((PyDescrObject *)descr),
205 descr->d_type->tp_name);
206 return -1;
207}
208
209static PyObject *
210methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
211{
212 int argc;
213 PyObject *self, *func, *result;
214
215 /* Make sure that the first argument is acceptable as 'self' */
216 assert(PyTuple_Check(args));
217 argc = PyTuple_GET_SIZE(args);
218 if (argc < 1) {
219 PyErr_Format(PyExc_TypeError,
220 "descriptor '%.300s' of '%.100s' "
221 "object needs an argument",
222 descr_name((PyDescrObject *)descr),
223 descr->d_type->tp_name);
224 return NULL;
225 }
226 self = PyTuple_GET_ITEM(args, 0);
227 if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
228 PyErr_Format(PyExc_TypeError,
229 "descriptor '%.200s' "
230 "requires a '%.100s' object "
231 "but received a '%.100s'",
232 descr_name((PyDescrObject *)descr),
233 descr->d_type->tp_name,
234 self->ob_type->tp_name);
235 return NULL;
236 }
237
238 func = PyCFunction_New(descr->d_method, self);
239 if (func == NULL)
240 return NULL;
241 args = PyTuple_GetSlice(args, 1, argc);
242 if (args == NULL) {
243 Py_DECREF(func);
244 return NULL;
245 }
246 result = PyEval_CallObjectWithKeywords(func, args, kwds);
247 Py_DECREF(args);
248 Py_DECREF(func);
249 return result;
250}
251
252static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000253classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
254 PyObject *kwds)
255{
256 PyObject *func, *result;
257
258 func = PyCFunction_New(descr->d_method, (PyObject *)descr->d_type);
259 if (func == NULL)
260 return NULL;
261
262 result = PyEval_CallObjectWithKeywords(func, args, kwds);
263 Py_DECREF(func);
264 return result;
265}
266
267static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000268wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
269{
270 int argc;
271 PyObject *self, *func, *result;
272
273 /* Make sure that the first argument is acceptable as 'self' */
274 assert(PyTuple_Check(args));
275 argc = PyTuple_GET_SIZE(args);
276 if (argc < 1) {
277 PyErr_Format(PyExc_TypeError,
278 "descriptor '%.300s' of '%.100s' "
279 "object needs an argument",
280 descr_name((PyDescrObject *)descr),
281 descr->d_type->tp_name);
282 return NULL;
283 }
284 self = PyTuple_GET_ITEM(args, 0);
285 if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
286 PyErr_Format(PyExc_TypeError,
287 "descriptor '%.200s' "
288 "requires a '%.100s' object "
289 "but received a '%.100s'",
290 descr_name((PyDescrObject *)descr),
291 descr->d_type->tp_name,
292 self->ob_type->tp_name);
293 return NULL;
294 }
295
296 func = PyWrapper_New((PyObject *)descr, self);
297 if (func == NULL)
298 return NULL;
299 args = PyTuple_GetSlice(args, 1, argc);
300 if (args == NULL) {
301 Py_DECREF(func);
302 return NULL;
303 }
304 result = PyEval_CallObjectWithKeywords(func, args, kwds);
305 Py_DECREF(args);
306 Py_DECREF(func);
307 return result;
308}
309
310static PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000311method_get_doc(PyMethodDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000312{
313 if (descr->d_method->ml_doc == NULL) {
314 Py_INCREF(Py_None);
315 return Py_None;
316 }
317 return PyString_FromString(descr->d_method->ml_doc);
318}
319
Guido van Rossum6f799372001-09-20 20:46:19 +0000320static PyMemberDef descr_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000321 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
322 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
323 {0}
324};
325
Guido van Rossum32d34c82001-09-20 21:45:26 +0000326static PyGetSetDef method_getset[] = {
Guido van Rossum6f799372001-09-20 20:46:19 +0000327 {"__doc__", (getter)method_get_doc},
328 {0}
329};
330
331static PyObject *
332member_get_doc(PyMemberDescrObject *descr, void *closure)
333{
334 if (descr->d_member->doc == NULL) {
335 Py_INCREF(Py_None);
336 return Py_None;
337 }
338 return PyString_FromString(descr->d_member->doc);
339}
340
Guido van Rossum32d34c82001-09-20 21:45:26 +0000341static PyGetSetDef member_getset[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000342 {"__doc__", (getter)member_get_doc},
343 {0}
344};
345
346static PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000347getset_get_doc(PyGetSetDescrObject *descr, void *closure)
348{
349 if (descr->d_getset->doc == NULL) {
350 Py_INCREF(Py_None);
351 return Py_None;
352 }
353 return PyString_FromString(descr->d_getset->doc);
354}
355
356static PyGetSetDef getset_getset[] = {
357 {"__doc__", (getter)getset_get_doc},
358 {0}
359};
360
361static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000362wrapper_get_doc(PyWrapperDescrObject *descr, void *closure)
363{
364 if (descr->d_base->doc == NULL) {
365 Py_INCREF(Py_None);
366 return Py_None;
367 }
368 return PyString_FromString(descr->d_base->doc);
369}
370
Guido van Rossum32d34c82001-09-20 21:45:26 +0000371static PyGetSetDef wrapper_getset[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000372 {"__doc__", (getter)wrapper_get_doc},
373 {0}
374};
375
Guido van Rossum048eb752001-10-02 21:24:57 +0000376static int
377descr_traverse(PyObject *self, visitproc visit, void *arg)
378{
379 PyDescrObject *descr = (PyDescrObject *)self;
380 int err;
381
382 if (descr->d_type) {
383 err = visit((PyObject *)(descr->d_type), arg);
384 if (err)
385 return err;
386 }
387 return 0;
388}
389
Tim Peters6d6c1a32001-08-02 04:15:00 +0000390static PyTypeObject PyMethodDescr_Type = {
391 PyObject_HEAD_INIT(&PyType_Type)
392 0,
393 "method_descriptor",
394 sizeof(PyMethodDescrObject),
395 0,
396 (destructor)descr_dealloc, /* tp_dealloc */
397 0, /* tp_print */
398 0, /* tp_getattr */
399 0, /* tp_setattr */
400 0, /* tp_compare */
401 (reprfunc)method_repr, /* tp_repr */
402 0, /* tp_as_number */
403 0, /* tp_as_sequence */
404 0, /* tp_as_mapping */
405 0, /* tp_hash */
406 (ternaryfunc)methoddescr_call, /* tp_call */
407 0, /* tp_str */
408 PyObject_GenericGetAttr, /* tp_getattro */
409 0, /* tp_setattro */
410 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000411 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000412 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000413 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000414 0, /* tp_clear */
415 0, /* tp_richcompare */
416 0, /* tp_weaklistoffset */
417 0, /* tp_iter */
418 0, /* tp_iternext */
419 0, /* tp_methods */
420 descr_members, /* tp_members */
Guido van Rossum6f799372001-09-20 20:46:19 +0000421 method_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000422 0, /* tp_base */
423 0, /* tp_dict */
424 (descrgetfunc)method_get, /* tp_descr_get */
425 0, /* tp_descr_set */
426};
427
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000428/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000429static PyTypeObject PyClassMethodDescr_Type = {
430 PyObject_HEAD_INIT(&PyType_Type)
431 0,
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000432 "classmethod_descriptor",
Tim Petersbca1cbc2002-12-09 22:56:13 +0000433 sizeof(PyMethodDescrObject),
434 0,
435 (destructor)descr_dealloc, /* tp_dealloc */
436 0, /* tp_print */
437 0, /* tp_getattr */
438 0, /* tp_setattr */
439 0, /* tp_compare */
440 (reprfunc)method_repr, /* tp_repr */
441 0, /* tp_as_number */
442 0, /* tp_as_sequence */
443 0, /* tp_as_mapping */
444 0, /* tp_hash */
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000445 (ternaryfunc)classmethoddescr_call, /* tp_call */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000446 0, /* tp_str */
447 PyObject_GenericGetAttr, /* tp_getattro */
448 0, /* tp_setattro */
449 0, /* tp_as_buffer */
450 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
451 0, /* tp_doc */
452 descr_traverse, /* tp_traverse */
453 0, /* tp_clear */
454 0, /* tp_richcompare */
455 0, /* tp_weaklistoffset */
456 0, /* tp_iter */
457 0, /* tp_iternext */
458 0, /* tp_methods */
459 descr_members, /* tp_members */
460 method_getset, /* tp_getset */
461 0, /* tp_base */
462 0, /* tp_dict */
463 (descrgetfunc)classmethod_get, /* tp_descr_get */
464 0, /* tp_descr_set */
465};
466
Tim Peters6d6c1a32001-08-02 04:15:00 +0000467static PyTypeObject PyMemberDescr_Type = {
468 PyObject_HEAD_INIT(&PyType_Type)
469 0,
470 "member_descriptor",
471 sizeof(PyMemberDescrObject),
472 0,
473 (destructor)descr_dealloc, /* tp_dealloc */
474 0, /* tp_print */
475 0, /* tp_getattr */
476 0, /* tp_setattr */
477 0, /* tp_compare */
478 (reprfunc)member_repr, /* tp_repr */
479 0, /* tp_as_number */
480 0, /* tp_as_sequence */
481 0, /* tp_as_mapping */
482 0, /* tp_hash */
483 (ternaryfunc)0, /* tp_call */
484 0, /* tp_str */
485 PyObject_GenericGetAttr, /* tp_getattro */
486 0, /* tp_setattro */
487 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000488 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000489 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000490 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000491 0, /* tp_clear */
492 0, /* tp_richcompare */
493 0, /* tp_weaklistoffset */
494 0, /* tp_iter */
495 0, /* tp_iternext */
496 0, /* tp_methods */
497 descr_members, /* tp_members */
Guido van Rossum6f799372001-09-20 20:46:19 +0000498 member_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000499 0, /* tp_base */
500 0, /* tp_dict */
501 (descrgetfunc)member_get, /* tp_descr_get */
502 (descrsetfunc)member_set, /* tp_descr_set */
503};
504
505static PyTypeObject PyGetSetDescr_Type = {
506 PyObject_HEAD_INIT(&PyType_Type)
507 0,
508 "getset_descriptor",
509 sizeof(PyGetSetDescrObject),
510 0,
511 (destructor)descr_dealloc, /* tp_dealloc */
512 0, /* tp_print */
513 0, /* tp_getattr */
514 0, /* tp_setattr */
515 0, /* tp_compare */
516 (reprfunc)getset_repr, /* tp_repr */
517 0, /* tp_as_number */
518 0, /* tp_as_sequence */
519 0, /* tp_as_mapping */
520 0, /* tp_hash */
521 (ternaryfunc)0, /* tp_call */
522 0, /* tp_str */
523 PyObject_GenericGetAttr, /* tp_getattro */
524 0, /* tp_setattro */
525 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000526 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000527 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000528 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000529 0, /* tp_clear */
530 0, /* tp_richcompare */
531 0, /* tp_weaklistoffset */
532 0, /* tp_iter */
533 0, /* tp_iternext */
534 0, /* tp_methods */
535 descr_members, /* tp_members */
Guido van Rossum32d34c82001-09-20 21:45:26 +0000536 getset_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000537 0, /* tp_base */
538 0, /* tp_dict */
539 (descrgetfunc)getset_get, /* tp_descr_get */
540 (descrsetfunc)getset_set, /* tp_descr_set */
541};
542
Guido van Rossumf4593e02001-10-03 12:09:30 +0000543PyTypeObject PyWrapperDescr_Type = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000544 PyObject_HEAD_INIT(&PyType_Type)
545 0,
546 "wrapper_descriptor",
547 sizeof(PyWrapperDescrObject),
548 0,
549 (destructor)descr_dealloc, /* tp_dealloc */
550 0, /* tp_print */
551 0, /* tp_getattr */
552 0, /* tp_setattr */
553 0, /* tp_compare */
554 (reprfunc)wrapper_repr, /* tp_repr */
555 0, /* tp_as_number */
556 0, /* tp_as_sequence */
557 0, /* tp_as_mapping */
558 0, /* tp_hash */
559 (ternaryfunc)wrapperdescr_call, /* tp_call */
560 0, /* tp_str */
561 PyObject_GenericGetAttr, /* tp_getattro */
562 0, /* tp_setattro */
563 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000564 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000565 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000566 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000567 0, /* tp_clear */
568 0, /* tp_richcompare */
569 0, /* tp_weaklistoffset */
570 0, /* tp_iter */
571 0, /* tp_iternext */
572 0, /* tp_methods */
573 descr_members, /* tp_members */
574 wrapper_getset, /* tp_getset */
575 0, /* tp_base */
576 0, /* tp_dict */
577 (descrgetfunc)wrapper_get, /* tp_descr_get */
578 0, /* tp_descr_set */
579};
580
581static PyDescrObject *
582descr_new(PyTypeObject *descrtype, PyTypeObject *type, char *name)
583{
584 PyDescrObject *descr;
585
586 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
587 if (descr != NULL) {
588 Py_XINCREF(type);
589 descr->d_type = type;
590 descr->d_name = PyString_InternFromString(name);
591 if (descr->d_name == NULL) {
592 Py_DECREF(descr);
593 descr = NULL;
594 }
595 }
596 return descr;
597}
598
599PyObject *
600PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
601{
602 PyMethodDescrObject *descr;
603
604 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
605 type, method->ml_name);
606 if (descr != NULL)
607 descr->d_method = method;
608 return (PyObject *)descr;
609}
610
611PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000612PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
613{
614 PyMethodDescrObject *descr;
615
616 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
617 type, method->ml_name);
618 if (descr != NULL)
619 descr->d_method = method;
620 return (PyObject *)descr;
621}
622
623PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000624PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000625{
626 PyMemberDescrObject *descr;
627
628 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
629 type, member->name);
630 if (descr != NULL)
631 descr->d_member = member;
632 return (PyObject *)descr;
633}
634
635PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000636PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637{
638 PyGetSetDescrObject *descr;
639
640 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
641 type, getset->name);
642 if (descr != NULL)
643 descr->d_getset = getset;
644 return (PyObject *)descr;
645}
646
647PyObject *
648PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
649{
650 PyWrapperDescrObject *descr;
651
652 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
653 type, base->name);
654 if (descr != NULL) {
655 descr->d_base = base;
656 descr->d_wrapped = wrapped;
657 }
658 return (PyObject *)descr;
659}
660
Tim Peters6d6c1a32001-08-02 04:15:00 +0000661
662/* --- Readonly proxy for dictionaries (actually any mapping) --- */
663
664/* This has no reason to be in this file except that adding new files is a
665 bit of a pain */
666
667typedef struct {
668 PyObject_HEAD
669 PyObject *dict;
670} proxyobject;
671
672static int
673proxy_len(proxyobject *pp)
674{
675 return PyObject_Size(pp->dict);
676}
677
678static PyObject *
679proxy_getitem(proxyobject *pp, PyObject *key)
680{
681 return PyObject_GetItem(pp->dict, key);
682}
683
684static PyMappingMethods proxy_as_mapping = {
685 (inquiry)proxy_len, /* mp_length */
686 (binaryfunc)proxy_getitem, /* mp_subscript */
687 0, /* mp_ass_subscript */
688};
689
690static int
691proxy_contains(proxyobject *pp, PyObject *key)
692{
693 return PySequence_Contains(pp->dict, key);
694}
695
696static PySequenceMethods proxy_as_sequence = {
697 0, /* sq_length */
698 0, /* sq_concat */
699 0, /* sq_repeat */
700 0, /* sq_item */
701 0, /* sq_slice */
702 0, /* sq_ass_item */
703 0, /* sq_ass_slice */
704 (objobjproc)proxy_contains, /* sq_contains */
705 0, /* sq_inplace_concat */
706 0, /* sq_inplace_repeat */
707};
708
709static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000710proxy_has_key(proxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000711{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712 return PyInt_FromLong(PySequence_Contains(pp->dict, key));
713}
714
715static PyObject *
716proxy_get(proxyobject *pp, PyObject *args)
717{
718 PyObject *key, *def = Py_None;
719
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000720 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000721 return NULL;
722 return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
723}
724
725static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000726proxy_keys(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000727{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728 return PyMapping_Keys(pp->dict);
729}
730
731static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000732proxy_values(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000734 return PyMapping_Values(pp->dict);
735}
736
737static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000738proxy_items(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000739{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740 return PyMapping_Items(pp->dict);
741}
742
743static PyObject *
Walter Dörwalde990c792002-03-25 17:43:22 +0000744proxy_iterkeys(proxyobject *pp)
745{
746 return PyObject_CallMethod(pp->dict, "iterkeys", NULL);
747}
748
749static PyObject *
750proxy_itervalues(proxyobject *pp)
751{
752 return PyObject_CallMethod(pp->dict, "itervalues", NULL);
753}
754
755static PyObject *
756proxy_iteritems(proxyobject *pp)
757{
758 return PyObject_CallMethod(pp->dict, "iteritems", NULL);
759}
760static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000761proxy_copy(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763 return PyObject_CallMethod(pp->dict, "copy", NULL);
764}
765
766static PyMethodDef proxy_methods[] = {
Guido van Rossum59610422002-04-13 14:06:36 +0000767 {"has_key", (PyCFunction)proxy_has_key, METH_O,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000768 PyDoc_STR("D.has_key(k) -> 1 if D has a key k, else 0")},
Guido van Rossum59610422002-04-13 14:06:36 +0000769 {"get", (PyCFunction)proxy_get, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000770 PyDoc_STR("D.get(k[,d]) -> D[k] if D.has_key(k), else d."
771 " d defaults to None.")},
Guido van Rossum59610422002-04-13 14:06:36 +0000772 {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000773 PyDoc_STR("D.keys() -> list of D's keys")},
Guido van Rossum59610422002-04-13 14:06:36 +0000774 {"values", (PyCFunction)proxy_values, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000775 PyDoc_STR("D.values() -> list of D's values")},
Guido van Rossum59610422002-04-13 14:06:36 +0000776 {"items", (PyCFunction)proxy_items, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000777 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Guido van Rossum59610422002-04-13 14:06:36 +0000778 {"iterkeys", (PyCFunction)proxy_iterkeys, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000779 PyDoc_STR("D.iterkeys() -> an iterator over the keys of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000780 {"itervalues",(PyCFunction)proxy_itervalues, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000781 PyDoc_STR("D.itervalues() -> an iterator over the values of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000782 {"iteritems", (PyCFunction)proxy_iteritems, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000783 PyDoc_STR("D.iteritems() ->"
784 " an iterator over the (key, value) items of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000785 {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000786 PyDoc_STR("D.copy() -> a shallow copy of D")},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000787 {0}
788};
789
790static void
791proxy_dealloc(proxyobject *pp)
792{
Guido van Rossum048eb752001-10-02 21:24:57 +0000793 _PyObject_GC_UNTRACK(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000794 Py_DECREF(pp->dict);
Guido van Rossum048eb752001-10-02 21:24:57 +0000795 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000796}
797
798static PyObject *
799proxy_getiter(proxyobject *pp)
800{
801 return PyObject_GetIter(pp->dict);
802}
803
Neil Schemenauer26775122001-10-21 22:26:43 +0000804static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805proxy_str(proxyobject *pp)
806{
807 return PyObject_Str(pp->dict);
808}
809
Guido van Rossum048eb752001-10-02 21:24:57 +0000810static int
811proxy_traverse(PyObject *self, visitproc visit, void *arg)
812{
813 proxyobject *pp = (proxyobject *)self;
814 int err;
815
816 if (pp->dict) {
817 err = visit(pp->dict, arg);
818 if (err)
819 return err;
820 }
821 return 0;
822}
823
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000824static int
825proxy_compare(proxyobject *v, PyObject *w)
826{
827 return PyObject_Compare(v->dict, w);
828}
829
830static PyObject *
831proxy_richcompare(proxyobject *v, PyObject *w, int op)
832{
833 return PyObject_RichCompare(v->dict, w, op);
834}
835
Neil Schemenauer26775122001-10-21 22:26:43 +0000836static PyTypeObject proxytype = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000837 PyObject_HEAD_INIT(&PyType_Type)
838 0, /* ob_size */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000839 "dictproxy", /* tp_name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000840 sizeof(proxyobject), /* tp_basicsize */
841 0, /* tp_itemsize */
842 /* methods */
843 (destructor)proxy_dealloc, /* tp_dealloc */
844 0, /* tp_print */
845 0, /* tp_getattr */
846 0, /* tp_setattr */
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000847 (cmpfunc)proxy_compare, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000848 0, /* tp_repr */
849 0, /* tp_as_number */
850 &proxy_as_sequence, /* tp_as_sequence */
851 &proxy_as_mapping, /* tp_as_mapping */
852 0, /* tp_hash */
853 0, /* tp_call */
854 (reprfunc)proxy_str, /* tp_str */
855 PyObject_GenericGetAttr, /* tp_getattro */
856 0, /* tp_setattro */
857 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000858 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000859 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000860 proxy_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000861 0, /* tp_clear */
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000862 (richcmpfunc)proxy_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000863 0, /* tp_weaklistoffset */
864 (getiterfunc)proxy_getiter, /* tp_iter */
865 0, /* tp_iternext */
866 proxy_methods, /* tp_methods */
867 0, /* tp_members */
868 0, /* tp_getset */
869 0, /* tp_base */
870 0, /* tp_dict */
871 0, /* tp_descr_get */
872 0, /* tp_descr_set */
873};
874
875PyObject *
876PyDictProxy_New(PyObject *dict)
877{
878 proxyobject *pp;
879
Guido van Rossum048eb752001-10-02 21:24:57 +0000880 pp = PyObject_GC_New(proxyobject, &proxytype);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000881 if (pp != NULL) {
882 Py_INCREF(dict);
883 pp->dict = dict;
Guido van Rossum048eb752001-10-02 21:24:57 +0000884 _PyObject_GC_TRACK(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000885 }
886 return (PyObject *)pp;
887}
888
889
890/* --- Wrapper object for "slot" methods --- */
891
892/* This has no reason to be in this file except that adding new files is a
893 bit of a pain */
894
895typedef struct {
896 PyObject_HEAD
897 PyWrapperDescrObject *descr;
898 PyObject *self;
899} wrapperobject;
900
901static void
902wrapper_dealloc(wrapperobject *wp)
903{
Guido van Rossum048eb752001-10-02 21:24:57 +0000904 _PyObject_GC_UNTRACK(wp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000905 Py_XDECREF(wp->descr);
906 Py_XDECREF(wp->self);
Guido van Rossum048eb752001-10-02 21:24:57 +0000907 PyObject_GC_Del(wp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908}
909
910static PyMethodDef wrapper_methods[] = {
911 {0}
912};
913
914static PyObject *
915wrapper_name(wrapperobject *wp)
916{
917 char *s = wp->descr->d_base->name;
918
919 return PyString_FromString(s);
920}
921
922static PyObject *
923wrapper_doc(wrapperobject *wp)
924{
925 char *s = wp->descr->d_base->doc;
926
927 if (s == NULL) {
928 Py_INCREF(Py_None);
929 return Py_None;
930 }
931 else {
932 return PyString_FromString(s);
933 }
934}
935
Guido van Rossum32d34c82001-09-20 21:45:26 +0000936static PyGetSetDef wrapper_getsets[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000937 {"__name__", (getter)wrapper_name},
938 {"__doc__", (getter)wrapper_doc},
939 {0}
940};
941
942static PyObject *
943wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
944{
945 wrapperfunc wrapper = wp->descr->d_base->wrapper;
946 PyObject *self = wp->self;
947
Guido van Rossumc8e56452001-10-22 00:43:43 +0000948 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
949 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
950 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
951 }
952
953 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
954 PyErr_Format(PyExc_TypeError,
955 "wrapper %s doesn't take keyword arguments",
956 wp->descr->d_base->name);
957 return NULL;
958 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000959 return (*wrapper)(self, args, wp->descr->d_wrapped);
960}
961
Guido van Rossum048eb752001-10-02 21:24:57 +0000962static int
963wrapper_traverse(PyObject *self, visitproc visit, void *arg)
964{
965 wrapperobject *wp = (wrapperobject *)self;
966 int err;
967
968 if (wp->descr) {
969 err = visit((PyObject *)(wp->descr), arg);
970 if (err)
971 return err;
972 }
973 if (wp->self) {
974 err = visit(wp->self, arg);
975 if (err)
976 return err;
977 }
978 return 0;
979}
980
Neil Schemenauer26775122001-10-21 22:26:43 +0000981static PyTypeObject wrappertype = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982 PyObject_HEAD_INIT(&PyType_Type)
983 0, /* ob_size */
984 "method-wrapper", /* tp_name */
985 sizeof(wrapperobject), /* tp_basicsize */
986 0, /* tp_itemsize */
987 /* methods */
988 (destructor)wrapper_dealloc, /* tp_dealloc */
989 0, /* tp_print */
990 0, /* tp_getattr */
991 0, /* tp_setattr */
992 0, /* tp_compare */
993 0, /* tp_repr */
994 0, /* tp_as_number */
995 0, /* tp_as_sequence */
996 0, /* tp_as_mapping */
997 0, /* tp_hash */
998 (ternaryfunc)wrapper_call, /* tp_call */
999 0, /* tp_str */
1000 PyObject_GenericGetAttr, /* tp_getattro */
1001 0, /* tp_setattro */
1002 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001003 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001004 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001005 wrapper_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001006 0, /* tp_clear */
1007 0, /* tp_richcompare */
1008 0, /* tp_weaklistoffset */
1009 0, /* tp_iter */
1010 0, /* tp_iternext */
1011 wrapper_methods, /* tp_methods */
1012 0, /* tp_members */
1013 wrapper_getsets, /* tp_getset */
1014 0, /* tp_base */
1015 0, /* tp_dict */
1016 0, /* tp_descr_get */
1017 0, /* tp_descr_set */
1018};
1019
1020PyObject *
1021PyWrapper_New(PyObject *d, PyObject *self)
1022{
1023 wrapperobject *wp;
1024 PyWrapperDescrObject *descr;
1025
1026 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1027 descr = (PyWrapperDescrObject *)d;
1028 assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
1029
Guido van Rossum048eb752001-10-02 21:24:57 +00001030 wp = PyObject_GC_New(wrapperobject, &wrappertype);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031 if (wp != NULL) {
1032 Py_INCREF(descr);
1033 wp->descr = descr;
1034 Py_INCREF(self);
1035 wp->self = self;
Guido van Rossum048eb752001-10-02 21:24:57 +00001036 _PyObject_GC_TRACK(wp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001037 }
1038 return (PyObject *)wp;
1039}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001040
1041
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001042/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001043
1044/*
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001045 class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001046
Guido van Rossumba2485f2001-12-10 18:03:34 +00001047 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1048 self.__get = fget
1049 self.__set = fset
1050 self.__del = fdel
1051 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001052
Guido van Rossumba2485f2001-12-10 18:03:34 +00001053 def __get__(self, inst, type=None):
Guido van Rossumba2485f2001-12-10 18:03:34 +00001054 if inst is None:
1055 return self
Guido van Rossum7171f1c2001-12-10 18:06:21 +00001056 if self.__get is None:
1057 raise AttributeError, "unreadable attribute"
Guido van Rossumba2485f2001-12-10 18:03:34 +00001058 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001059
Guido van Rossumba2485f2001-12-10 18:03:34 +00001060 def __set__(self, inst, value):
1061 if self.__set is None:
1062 raise AttributeError, "can't set attribute"
1063 return self.__set(inst, value)
1064
1065 def __delete__(self, inst):
1066 if self.__del is None:
1067 raise AttributeError, "can't delete attribute"
1068 return self.__del(inst)
1069
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001070*/
1071
1072typedef struct {
1073 PyObject_HEAD
Tim Peters66c1a522001-09-24 21:17:50 +00001074 PyObject *prop_get;
1075 PyObject *prop_set;
1076 PyObject *prop_del;
1077 PyObject *prop_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001078} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001079
Tim Peters66c1a522001-09-24 21:17:50 +00001080static PyMemberDef property_members[] = {
1081 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1082 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1083 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1084 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1085 {0}
1086};
1087
1088
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001089static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001090property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001091{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001092 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001093
Guido van Rossum048eb752001-10-02 21:24:57 +00001094 _PyObject_GC_UNTRACK(self);
Tim Peters66c1a522001-09-24 21:17:50 +00001095 Py_XDECREF(gs->prop_get);
1096 Py_XDECREF(gs->prop_set);
1097 Py_XDECREF(gs->prop_del);
1098 Py_XDECREF(gs->prop_doc);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001099 self->ob_type->tp_free(self);
1100}
1101
1102static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001103property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001104{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001105 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001106
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001107 if (obj == NULL || obj == Py_None) {
1108 Py_INCREF(self);
1109 return self;
1110 }
Guido van Rossumb75ba912001-12-10 18:00:15 +00001111 if (gs->prop_get == NULL) {
1112 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1113 return NULL;
1114 }
Tim Peters66c1a522001-09-24 21:17:50 +00001115 return PyObject_CallFunction(gs->prop_get, "(O)", obj);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001116}
1117
1118static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001119property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001120{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001121 propertyobject *gs = (propertyobject *)self;
Guido van Rossum271410a2001-08-24 15:23:20 +00001122 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001123
Guido van Rossum271410a2001-08-24 15:23:20 +00001124 if (value == NULL)
Tim Peters66c1a522001-09-24 21:17:50 +00001125 func = gs->prop_del;
Guido van Rossum271410a2001-08-24 15:23:20 +00001126 else
Tim Peters66c1a522001-09-24 21:17:50 +00001127 func = gs->prop_set;
Guido van Rossum271410a2001-08-24 15:23:20 +00001128 if (func == NULL) {
1129 PyErr_SetString(PyExc_AttributeError,
1130 value == NULL ?
1131 "can't delete attribute" :
1132 "can't set attribute");
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001133 return -1;
1134 }
Guido van Rossum845fc482001-08-24 10:17:36 +00001135 if (value == NULL)
Guido van Rossum271410a2001-08-24 15:23:20 +00001136 res = PyObject_CallFunction(func, "(O)", obj);
Guido van Rossum845fc482001-08-24 10:17:36 +00001137 else
Guido van Rossum271410a2001-08-24 15:23:20 +00001138 res = PyObject_CallFunction(func, "(OO)", obj, value);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001139 if (res == NULL)
1140 return -1;
1141 Py_DECREF(res);
1142 return 0;
1143}
1144
1145static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001146property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001147{
Tim Peters66c1a522001-09-24 21:17:50 +00001148 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
1149 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001150 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001151
Tim Peters66c1a522001-09-24 21:17:50 +00001152 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
1153 kwlist, &get, &set, &del, &doc))
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001154 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001155
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001156 if (get == Py_None)
1157 get = NULL;
1158 if (set == Py_None)
1159 set = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001160 if (del == Py_None)
1161 del = NULL;
1162
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001163 Py_XINCREF(get);
1164 Py_XINCREF(set);
Guido van Rossum271410a2001-08-24 15:23:20 +00001165 Py_XINCREF(del);
Tim Peters66c1a522001-09-24 21:17:50 +00001166 Py_XINCREF(doc);
1167
1168 gs->prop_get = get;
1169 gs->prop_set = set;
1170 gs->prop_del = del;
1171 gs->prop_doc = doc;
1172
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001173 return 0;
1174}
1175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001176PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001177"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1178"\n"
1179"fget is a function to be used for getting an attribute value, and likewise\n"
1180"fset is a function for setting, and fdel a function for del'ing, an\n"
1181"attribute. Typical use is to define a managed attribute x:\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001182"class C(object):\n"
1183" def getx(self): return self.__x\n"
1184" def setx(self, value): self.__x = value\n"
Guido van Rossum271410a2001-08-24 15:23:20 +00001185" def delx(self): del self.__x\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001186" x = property(getx, setx, delx, \"I'm the 'x' property.\")");
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001187
Guido van Rossum048eb752001-10-02 21:24:57 +00001188static int
1189property_traverse(PyObject *self, visitproc visit, void *arg)
1190{
1191 propertyobject *pp = (propertyobject *)self;
1192 int err;
1193
1194#define VISIT(SLOT) \
1195 if (pp->SLOT) { \
1196 err = visit((PyObject *)(pp->SLOT), arg); \
1197 if (err) \
1198 return err; \
1199 }
1200
1201 VISIT(prop_get);
1202 VISIT(prop_set);
1203 VISIT(prop_del);
1204
1205 return 0;
1206}
1207
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001208PyTypeObject PyProperty_Type = {
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001209 PyObject_HEAD_INIT(&PyType_Type)
1210 0, /* ob_size */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001211 "property", /* tp_name */
1212 sizeof(propertyobject), /* tp_basicsize */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001213 0, /* tp_itemsize */
1214 /* methods */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001215 property_dealloc, /* tp_dealloc */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001216 0, /* tp_print */
1217 0, /* tp_getattr */
1218 0, /* tp_setattr */
1219 0, /* tp_compare */
1220 0, /* tp_repr */
1221 0, /* tp_as_number */
1222 0, /* tp_as_sequence */
1223 0, /* tp_as_mapping */
1224 0, /* tp_hash */
1225 0, /* tp_call */
1226 0, /* tp_str */
1227 PyObject_GenericGetAttr, /* tp_getattro */
1228 0, /* tp_setattro */
1229 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001230 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1231 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001232 property_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001233 property_traverse, /* tp_traverse */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001234 0, /* tp_clear */
1235 0, /* tp_richcompare */
1236 0, /* tp_weaklistoffset */
1237 0, /* tp_iter */
1238 0, /* tp_iternext */
1239 0, /* tp_methods */
Tim Peters66c1a522001-09-24 21:17:50 +00001240 property_members, /* tp_members */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001241 0, /* tp_getset */
1242 0, /* tp_base */
1243 0, /* tp_dict */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001244 property_descr_get, /* tp_descr_get */
1245 property_descr_set, /* tp_descr_set */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001246 0, /* tp_dictoffset */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001247 property_init, /* tp_init */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001248 PyType_GenericAlloc, /* tp_alloc */
1249 PyType_GenericNew, /* tp_new */
Neil Schemenauer99b5d282002-04-12 02:44:22 +00001250 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001251};