blob: 4aa131378d2b6db93104c1ee9e84598ac9c7cb62 [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);
Barry Warsawc8d907c2005-04-19 23:43:40 +0000147 PyErr_Format(PyExc_AttributeError,
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);
Barry Warsawc8d907c2005-04-19 23:43:40 +0000202 PyErr_Format(PyExc_AttributeError,
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{
Raymond Hettingerd3ae6722003-12-13 11:58:56 +0000693 return PyDict_Contains(pp->dict, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694}
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{
Raymond Hettingerd3ae6722003-12-13 11:58:56 +0000712 int res = PyDict_Contains(pp->dict, key);
Guido van Rossum98c65be2003-10-09 03:47:08 +0000713 if (res < 0)
714 return NULL;
715 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000716}
717
718static PyObject *
719proxy_get(proxyobject *pp, PyObject *args)
720{
721 PyObject *key, *def = Py_None;
722
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000723 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000724 return NULL;
725 return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
726}
727
728static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000729proxy_keys(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731 return PyMapping_Keys(pp->dict);
732}
733
734static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000735proxy_values(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000736{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000737 return PyMapping_Values(pp->dict);
738}
739
740static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000741proxy_items(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000742{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000743 return PyMapping_Items(pp->dict);
744}
745
746static PyObject *
Walter Dörwalde990c792002-03-25 17:43:22 +0000747proxy_iterkeys(proxyobject *pp)
748{
749 return PyObject_CallMethod(pp->dict, "iterkeys", NULL);
750}
751
752static PyObject *
753proxy_itervalues(proxyobject *pp)
754{
755 return PyObject_CallMethod(pp->dict, "itervalues", NULL);
756}
757
758static PyObject *
759proxy_iteritems(proxyobject *pp)
760{
761 return PyObject_CallMethod(pp->dict, "iteritems", NULL);
762}
763static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000764proxy_copy(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000766 return PyObject_CallMethod(pp->dict, "copy", NULL);
767}
768
769static PyMethodDef proxy_methods[] = {
Guido van Rossum59610422002-04-13 14:06:36 +0000770 {"has_key", (PyCFunction)proxy_has_key, METH_O,
Raymond Hettinger574aa322003-09-01 22:12:08 +0000771 PyDoc_STR("D.has_key(k) -> True if D has a key k, else False")},
Guido van Rossum59610422002-04-13 14:06:36 +0000772 {"get", (PyCFunction)proxy_get, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000773 PyDoc_STR("D.get(k[,d]) -> D[k] if D.has_key(k), else d."
774 " d defaults to None.")},
Guido van Rossum59610422002-04-13 14:06:36 +0000775 {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000776 PyDoc_STR("D.keys() -> list of D's keys")},
Guido van Rossum59610422002-04-13 14:06:36 +0000777 {"values", (PyCFunction)proxy_values, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000778 PyDoc_STR("D.values() -> list of D's values")},
Guido van Rossum59610422002-04-13 14:06:36 +0000779 {"items", (PyCFunction)proxy_items, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000780 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Guido van Rossum59610422002-04-13 14:06:36 +0000781 {"iterkeys", (PyCFunction)proxy_iterkeys, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000782 PyDoc_STR("D.iterkeys() -> an iterator over the keys of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000783 {"itervalues",(PyCFunction)proxy_itervalues, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000784 PyDoc_STR("D.itervalues() -> an iterator over the values of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000785 {"iteritems", (PyCFunction)proxy_iteritems, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000786 PyDoc_STR("D.iteritems() ->"
787 " an iterator over the (key, value) items of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000788 {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000789 PyDoc_STR("D.copy() -> a shallow copy of D")},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000790 {0}
791};
792
793static void
794proxy_dealloc(proxyobject *pp)
795{
Guido van Rossum048eb752001-10-02 21:24:57 +0000796 _PyObject_GC_UNTRACK(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000797 Py_DECREF(pp->dict);
Guido van Rossum048eb752001-10-02 21:24:57 +0000798 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000799}
800
801static PyObject *
802proxy_getiter(proxyobject *pp)
803{
804 return PyObject_GetIter(pp->dict);
805}
806
Neil Schemenauer26775122001-10-21 22:26:43 +0000807static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000808proxy_str(proxyobject *pp)
809{
810 return PyObject_Str(pp->dict);
811}
812
Guido van Rossum048eb752001-10-02 21:24:57 +0000813static int
814proxy_traverse(PyObject *self, visitproc visit, void *arg)
815{
816 proxyobject *pp = (proxyobject *)self;
817 int err;
818
819 if (pp->dict) {
820 err = visit(pp->dict, arg);
821 if (err)
822 return err;
823 }
824 return 0;
825}
826
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000827static int
828proxy_compare(proxyobject *v, PyObject *w)
829{
830 return PyObject_Compare(v->dict, w);
831}
832
833static PyObject *
834proxy_richcompare(proxyobject *v, PyObject *w, int op)
835{
836 return PyObject_RichCompare(v->dict, w, op);
837}
838
Neil Schemenauer26775122001-10-21 22:26:43 +0000839static PyTypeObject proxytype = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000840 PyObject_HEAD_INIT(&PyType_Type)
841 0, /* ob_size */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000842 "dictproxy", /* tp_name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000843 sizeof(proxyobject), /* tp_basicsize */
844 0, /* tp_itemsize */
845 /* methods */
846 (destructor)proxy_dealloc, /* tp_dealloc */
847 0, /* tp_print */
848 0, /* tp_getattr */
849 0, /* tp_setattr */
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000850 (cmpfunc)proxy_compare, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000851 0, /* tp_repr */
852 0, /* tp_as_number */
853 &proxy_as_sequence, /* tp_as_sequence */
854 &proxy_as_mapping, /* tp_as_mapping */
855 0, /* tp_hash */
856 0, /* tp_call */
857 (reprfunc)proxy_str, /* tp_str */
858 PyObject_GenericGetAttr, /* tp_getattro */
859 0, /* tp_setattro */
860 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000861 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000862 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000863 proxy_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000864 0, /* tp_clear */
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000865 (richcmpfunc)proxy_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866 0, /* tp_weaklistoffset */
867 (getiterfunc)proxy_getiter, /* tp_iter */
868 0, /* tp_iternext */
869 proxy_methods, /* tp_methods */
870 0, /* tp_members */
871 0, /* tp_getset */
872 0, /* tp_base */
873 0, /* tp_dict */
874 0, /* tp_descr_get */
875 0, /* tp_descr_set */
876};
877
878PyObject *
879PyDictProxy_New(PyObject *dict)
880{
881 proxyobject *pp;
882
Guido van Rossum048eb752001-10-02 21:24:57 +0000883 pp = PyObject_GC_New(proxyobject, &proxytype);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000884 if (pp != NULL) {
885 Py_INCREF(dict);
886 pp->dict = dict;
Guido van Rossum048eb752001-10-02 21:24:57 +0000887 _PyObject_GC_TRACK(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000888 }
889 return (PyObject *)pp;
890}
891
892
893/* --- Wrapper object for "slot" methods --- */
894
895/* This has no reason to be in this file except that adding new files is a
896 bit of a pain */
897
898typedef struct {
899 PyObject_HEAD
900 PyWrapperDescrObject *descr;
901 PyObject *self;
902} wrapperobject;
903
904static void
905wrapper_dealloc(wrapperobject *wp)
906{
Guido van Rossum048eb752001-10-02 21:24:57 +0000907 _PyObject_GC_UNTRACK(wp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908 Py_XDECREF(wp->descr);
909 Py_XDECREF(wp->self);
Guido van Rossum048eb752001-10-02 21:24:57 +0000910 PyObject_GC_Del(wp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000911}
912
913static PyMethodDef wrapper_methods[] = {
914 {0}
915};
916
917static PyObject *
918wrapper_name(wrapperobject *wp)
919{
920 char *s = wp->descr->d_base->name;
921
922 return PyString_FromString(s);
923}
924
925static PyObject *
926wrapper_doc(wrapperobject *wp)
927{
928 char *s = wp->descr->d_base->doc;
929
930 if (s == NULL) {
931 Py_INCREF(Py_None);
932 return Py_None;
933 }
934 else {
935 return PyString_FromString(s);
936 }
937}
938
Guido van Rossum32d34c82001-09-20 21:45:26 +0000939static PyGetSetDef wrapper_getsets[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000940 {"__name__", (getter)wrapper_name},
941 {"__doc__", (getter)wrapper_doc},
942 {0}
943};
944
945static PyObject *
946wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
947{
948 wrapperfunc wrapper = wp->descr->d_base->wrapper;
949 PyObject *self = wp->self;
950
Guido van Rossumc8e56452001-10-22 00:43:43 +0000951 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
952 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
953 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
954 }
955
956 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
957 PyErr_Format(PyExc_TypeError,
958 "wrapper %s doesn't take keyword arguments",
959 wp->descr->d_base->name);
960 return NULL;
961 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000962 return (*wrapper)(self, args, wp->descr->d_wrapped);
963}
964
Guido van Rossum048eb752001-10-02 21:24:57 +0000965static int
966wrapper_traverse(PyObject *self, visitproc visit, void *arg)
967{
968 wrapperobject *wp = (wrapperobject *)self;
969 int err;
970
971 if (wp->descr) {
972 err = visit((PyObject *)(wp->descr), arg);
973 if (err)
974 return err;
975 }
976 if (wp->self) {
977 err = visit(wp->self, arg);
978 if (err)
979 return err;
980 }
981 return 0;
982}
983
Neil Schemenauer26775122001-10-21 22:26:43 +0000984static PyTypeObject wrappertype = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000985 PyObject_HEAD_INIT(&PyType_Type)
986 0, /* ob_size */
987 "method-wrapper", /* tp_name */
988 sizeof(wrapperobject), /* tp_basicsize */
989 0, /* tp_itemsize */
990 /* methods */
991 (destructor)wrapper_dealloc, /* tp_dealloc */
992 0, /* tp_print */
993 0, /* tp_getattr */
994 0, /* tp_setattr */
995 0, /* tp_compare */
996 0, /* tp_repr */
997 0, /* tp_as_number */
998 0, /* tp_as_sequence */
999 0, /* tp_as_mapping */
1000 0, /* tp_hash */
1001 (ternaryfunc)wrapper_call, /* tp_call */
1002 0, /* tp_str */
1003 PyObject_GenericGetAttr, /* tp_getattro */
1004 0, /* tp_setattro */
1005 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001006 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001007 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001008 wrapper_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001009 0, /* tp_clear */
1010 0, /* tp_richcompare */
1011 0, /* tp_weaklistoffset */
1012 0, /* tp_iter */
1013 0, /* tp_iternext */
1014 wrapper_methods, /* tp_methods */
1015 0, /* tp_members */
1016 wrapper_getsets, /* tp_getset */
1017 0, /* tp_base */
1018 0, /* tp_dict */
1019 0, /* tp_descr_get */
1020 0, /* tp_descr_set */
1021};
1022
1023PyObject *
1024PyWrapper_New(PyObject *d, PyObject *self)
1025{
1026 wrapperobject *wp;
1027 PyWrapperDescrObject *descr;
1028
1029 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1030 descr = (PyWrapperDescrObject *)d;
1031 assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
1032
Guido van Rossum048eb752001-10-02 21:24:57 +00001033 wp = PyObject_GC_New(wrapperobject, &wrappertype);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034 if (wp != NULL) {
1035 Py_INCREF(descr);
1036 wp->descr = descr;
1037 Py_INCREF(self);
1038 wp->self = self;
Guido van Rossum048eb752001-10-02 21:24:57 +00001039 _PyObject_GC_TRACK(wp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001040 }
1041 return (PyObject *)wp;
1042}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001043
1044
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001045/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001046
1047/*
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001048 class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001049
Guido van Rossumba2485f2001-12-10 18:03:34 +00001050 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1051 self.__get = fget
1052 self.__set = fset
1053 self.__del = fdel
1054 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001055
Guido van Rossumba2485f2001-12-10 18:03:34 +00001056 def __get__(self, inst, type=None):
Guido van Rossumba2485f2001-12-10 18:03:34 +00001057 if inst is None:
1058 return self
Guido van Rossum7171f1c2001-12-10 18:06:21 +00001059 if self.__get is None:
1060 raise AttributeError, "unreadable attribute"
Guido van Rossumba2485f2001-12-10 18:03:34 +00001061 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001062
Guido van Rossumba2485f2001-12-10 18:03:34 +00001063 def __set__(self, inst, value):
1064 if self.__set is None:
1065 raise AttributeError, "can't set attribute"
1066 return self.__set(inst, value)
1067
1068 def __delete__(self, inst):
1069 if self.__del is None:
1070 raise AttributeError, "can't delete attribute"
1071 return self.__del(inst)
1072
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001073*/
1074
1075typedef struct {
1076 PyObject_HEAD
Tim Peters66c1a522001-09-24 21:17:50 +00001077 PyObject *prop_get;
1078 PyObject *prop_set;
1079 PyObject *prop_del;
1080 PyObject *prop_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001081} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001082
Tim Peters66c1a522001-09-24 21:17:50 +00001083static PyMemberDef property_members[] = {
1084 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1085 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1086 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1087 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1088 {0}
1089};
1090
1091
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001092static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001093property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001094{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001095 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001096
Guido van Rossum048eb752001-10-02 21:24:57 +00001097 _PyObject_GC_UNTRACK(self);
Tim Peters66c1a522001-09-24 21:17:50 +00001098 Py_XDECREF(gs->prop_get);
1099 Py_XDECREF(gs->prop_set);
1100 Py_XDECREF(gs->prop_del);
1101 Py_XDECREF(gs->prop_doc);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001102 self->ob_type->tp_free(self);
1103}
1104
1105static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001106property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001107{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001108 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001109
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001110 if (obj == NULL || obj == Py_None) {
1111 Py_INCREF(self);
1112 return self;
1113 }
Guido van Rossumb75ba912001-12-10 18:00:15 +00001114 if (gs->prop_get == NULL) {
1115 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1116 return NULL;
1117 }
Tim Peters66c1a522001-09-24 21:17:50 +00001118 return PyObject_CallFunction(gs->prop_get, "(O)", obj);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001119}
1120
1121static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001122property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001123{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001124 propertyobject *gs = (propertyobject *)self;
Guido van Rossum271410a2001-08-24 15:23:20 +00001125 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001126
Guido van Rossum271410a2001-08-24 15:23:20 +00001127 if (value == NULL)
Tim Peters66c1a522001-09-24 21:17:50 +00001128 func = gs->prop_del;
Guido van Rossum271410a2001-08-24 15:23:20 +00001129 else
Tim Peters66c1a522001-09-24 21:17:50 +00001130 func = gs->prop_set;
Guido van Rossum271410a2001-08-24 15:23:20 +00001131 if (func == NULL) {
1132 PyErr_SetString(PyExc_AttributeError,
1133 value == NULL ?
1134 "can't delete attribute" :
1135 "can't set attribute");
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001136 return -1;
1137 }
Guido van Rossum845fc482001-08-24 10:17:36 +00001138 if (value == NULL)
Guido van Rossum271410a2001-08-24 15:23:20 +00001139 res = PyObject_CallFunction(func, "(O)", obj);
Guido van Rossum845fc482001-08-24 10:17:36 +00001140 else
Guido van Rossum271410a2001-08-24 15:23:20 +00001141 res = PyObject_CallFunction(func, "(OO)", obj, value);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001142 if (res == NULL)
1143 return -1;
1144 Py_DECREF(res);
1145 return 0;
1146}
1147
1148static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001149property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001150{
Tim Peters66c1a522001-09-24 21:17:50 +00001151 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
1152 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001153 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001154
Tim Peters66c1a522001-09-24 21:17:50 +00001155 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
1156 kwlist, &get, &set, &del, &doc))
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001157 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001158
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001159 if (get == Py_None)
1160 get = NULL;
1161 if (set == Py_None)
1162 set = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001163 if (del == Py_None)
1164 del = NULL;
1165
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001166 Py_XINCREF(get);
1167 Py_XINCREF(set);
Guido van Rossum271410a2001-08-24 15:23:20 +00001168 Py_XINCREF(del);
Tim Peters66c1a522001-09-24 21:17:50 +00001169 Py_XINCREF(doc);
1170
1171 gs->prop_get = get;
1172 gs->prop_set = set;
1173 gs->prop_del = del;
1174 gs->prop_doc = doc;
1175
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001176 return 0;
1177}
1178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001179PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001180"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1181"\n"
1182"fget is a function to be used for getting an attribute value, and likewise\n"
1183"fset is a function for setting, and fdel a function for del'ing, an\n"
1184"attribute. Typical use is to define a managed attribute x:\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001185"class C(object):\n"
1186" def getx(self): return self.__x\n"
1187" def setx(self, value): self.__x = value\n"
Guido van Rossum271410a2001-08-24 15:23:20 +00001188" def delx(self): del self.__x\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001189" x = property(getx, setx, delx, \"I'm the 'x' property.\")");
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001190
Guido van Rossum048eb752001-10-02 21:24:57 +00001191static int
1192property_traverse(PyObject *self, visitproc visit, void *arg)
1193{
1194 propertyobject *pp = (propertyobject *)self;
1195 int err;
1196
1197#define VISIT(SLOT) \
1198 if (pp->SLOT) { \
1199 err = visit((PyObject *)(pp->SLOT), arg); \
1200 if (err) \
1201 return err; \
1202 }
1203
1204 VISIT(prop_get);
1205 VISIT(prop_set);
1206 VISIT(prop_del);
Guido van Rossum00bf8282003-04-09 17:05:33 +00001207 VISIT(prop_doc);
Guido van Rossum048eb752001-10-02 21:24:57 +00001208
1209 return 0;
1210}
1211
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001212PyTypeObject PyProperty_Type = {
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001213 PyObject_HEAD_INIT(&PyType_Type)
1214 0, /* ob_size */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001215 "property", /* tp_name */
1216 sizeof(propertyobject), /* tp_basicsize */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001217 0, /* tp_itemsize */
1218 /* methods */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001219 property_dealloc, /* tp_dealloc */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001220 0, /* tp_print */
1221 0, /* tp_getattr */
1222 0, /* tp_setattr */
1223 0, /* tp_compare */
1224 0, /* tp_repr */
1225 0, /* tp_as_number */
1226 0, /* tp_as_sequence */
1227 0, /* tp_as_mapping */
1228 0, /* tp_hash */
1229 0, /* tp_call */
1230 0, /* tp_str */
1231 PyObject_GenericGetAttr, /* tp_getattro */
1232 0, /* tp_setattro */
1233 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001234 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1235 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001236 property_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001237 property_traverse, /* tp_traverse */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001238 0, /* tp_clear */
1239 0, /* tp_richcompare */
1240 0, /* tp_weaklistoffset */
1241 0, /* tp_iter */
1242 0, /* tp_iternext */
1243 0, /* tp_methods */
Tim Peters66c1a522001-09-24 21:17:50 +00001244 property_members, /* tp_members */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001245 0, /* tp_getset */
1246 0, /* tp_base */
1247 0, /* tp_dict */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001248 property_descr_get, /* tp_descr_get */
1249 property_descr_set, /* tp_descr_set */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001250 0, /* tp_dictoffset */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001251 property_init, /* tp_init */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001252 PyType_GenericAlloc, /* tp_alloc */
1253 PyType_GenericNew, /* tp_new */
Neil Schemenauer99b5d282002-04-12 02:44:22 +00001254 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001255};