blob: 3cfdb973489a0a0154ec66d32115e81cae874f8e [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 *
Armin Rigoc6686b72005-11-07 08:38:00 +000053wrapperdescr_repr(PyWrapperDescrObject *descr)
Tim Peters6d6c1a32001-08-02 04:15:00 +000054{
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 *
Armin Rigoc6686b72005-11-07 08:38:00 +0000155wrapperdescr_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{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000212 Py_ssize_t argc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000213 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{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000270 Py_ssize_t argc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000271 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 *
Armin Rigoc6686b72005-11-07 08:38:00 +0000362wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000363{
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
Armin Rigoc6686b72005-11-07 08:38:00 +0000371static PyGetSetDef wrapperdescr_getset[] = {
372 {"__doc__", (getter)wrapperdescr_get_doc},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000373 {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;
Thomas Woutersc6e55062006-04-15 21:47:09 +0000380 Py_VISIT(descr->d_type);
Guido van Rossum048eb752001-10-02 21:24:57 +0000381 return 0;
382}
383
Tim Peters6d6c1a32001-08-02 04:15:00 +0000384static PyTypeObject PyMethodDescr_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000385 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000386 "method_descriptor",
387 sizeof(PyMethodDescrObject),
388 0,
389 (destructor)descr_dealloc, /* tp_dealloc */
390 0, /* tp_print */
391 0, /* tp_getattr */
392 0, /* tp_setattr */
393 0, /* tp_compare */
394 (reprfunc)method_repr, /* tp_repr */
395 0, /* tp_as_number */
396 0, /* tp_as_sequence */
397 0, /* tp_as_mapping */
398 0, /* tp_hash */
399 (ternaryfunc)methoddescr_call, /* tp_call */
400 0, /* tp_str */
401 PyObject_GenericGetAttr, /* tp_getattro */
402 0, /* tp_setattro */
403 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000404 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000405 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000406 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000407 0, /* tp_clear */
408 0, /* tp_richcompare */
409 0, /* tp_weaklistoffset */
410 0, /* tp_iter */
411 0, /* tp_iternext */
412 0, /* tp_methods */
413 descr_members, /* tp_members */
Guido van Rossum6f799372001-09-20 20:46:19 +0000414 method_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000415 0, /* tp_base */
416 0, /* tp_dict */
417 (descrgetfunc)method_get, /* tp_descr_get */
418 0, /* tp_descr_set */
419};
420
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000421/* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000422static PyTypeObject PyClassMethodDescr_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000423 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000424 "classmethod_descriptor",
Tim Petersbca1cbc2002-12-09 22:56:13 +0000425 sizeof(PyMethodDescrObject),
426 0,
427 (destructor)descr_dealloc, /* tp_dealloc */
428 0, /* tp_print */
429 0, /* tp_getattr */
430 0, /* tp_setattr */
431 0, /* tp_compare */
432 (reprfunc)method_repr, /* tp_repr */
433 0, /* tp_as_number */
434 0, /* tp_as_sequence */
435 0, /* tp_as_mapping */
436 0, /* tp_hash */
Guido van Rossumb6e5a0c2003-02-11 18:44:42 +0000437 (ternaryfunc)classmethoddescr_call, /* tp_call */
Tim Petersbca1cbc2002-12-09 22:56:13 +0000438 0, /* tp_str */
439 PyObject_GenericGetAttr, /* tp_getattro */
440 0, /* tp_setattro */
441 0, /* tp_as_buffer */
442 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
443 0, /* tp_doc */
444 descr_traverse, /* tp_traverse */
445 0, /* tp_clear */
446 0, /* tp_richcompare */
447 0, /* tp_weaklistoffset */
448 0, /* tp_iter */
449 0, /* tp_iternext */
450 0, /* tp_methods */
451 descr_members, /* tp_members */
452 method_getset, /* tp_getset */
453 0, /* tp_base */
454 0, /* tp_dict */
455 (descrgetfunc)classmethod_get, /* tp_descr_get */
456 0, /* tp_descr_set */
457};
458
Tim Peters6d6c1a32001-08-02 04:15:00 +0000459static PyTypeObject PyMemberDescr_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000460 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000461 "member_descriptor",
462 sizeof(PyMemberDescrObject),
463 0,
464 (destructor)descr_dealloc, /* tp_dealloc */
465 0, /* tp_print */
466 0, /* tp_getattr */
467 0, /* tp_setattr */
468 0, /* tp_compare */
469 (reprfunc)member_repr, /* tp_repr */
470 0, /* tp_as_number */
471 0, /* tp_as_sequence */
472 0, /* tp_as_mapping */
473 0, /* tp_hash */
Georg Brandl347b3002006-03-30 11:57:00 +0000474 0, /* tp_call */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000475 0, /* tp_str */
476 PyObject_GenericGetAttr, /* tp_getattro */
477 0, /* tp_setattro */
478 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000479 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000480 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000481 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000482 0, /* tp_clear */
483 0, /* tp_richcompare */
484 0, /* tp_weaklistoffset */
485 0, /* tp_iter */
486 0, /* tp_iternext */
487 0, /* tp_methods */
488 descr_members, /* tp_members */
Guido van Rossum6f799372001-09-20 20:46:19 +0000489 member_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000490 0, /* tp_base */
491 0, /* tp_dict */
492 (descrgetfunc)member_get, /* tp_descr_get */
493 (descrsetfunc)member_set, /* tp_descr_set */
494};
495
496static PyTypeObject PyGetSetDescr_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000497 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000498 "getset_descriptor",
499 sizeof(PyGetSetDescrObject),
500 0,
501 (destructor)descr_dealloc, /* tp_dealloc */
502 0, /* tp_print */
503 0, /* tp_getattr */
504 0, /* tp_setattr */
505 0, /* tp_compare */
506 (reprfunc)getset_repr, /* tp_repr */
507 0, /* tp_as_number */
508 0, /* tp_as_sequence */
509 0, /* tp_as_mapping */
510 0, /* tp_hash */
Georg Brandl347b3002006-03-30 11:57:00 +0000511 0, /* tp_call */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000512 0, /* tp_str */
513 PyObject_GenericGetAttr, /* tp_getattro */
514 0, /* tp_setattro */
515 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000516 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000517 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000518 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000519 0, /* tp_clear */
520 0, /* tp_richcompare */
521 0, /* tp_weaklistoffset */
522 0, /* tp_iter */
523 0, /* tp_iternext */
524 0, /* tp_methods */
525 descr_members, /* tp_members */
Guido van Rossum32d34c82001-09-20 21:45:26 +0000526 getset_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000527 0, /* tp_base */
528 0, /* tp_dict */
529 (descrgetfunc)getset_get, /* tp_descr_get */
530 (descrsetfunc)getset_set, /* tp_descr_set */
531};
532
Guido van Rossumf4593e02001-10-03 12:09:30 +0000533PyTypeObject PyWrapperDescr_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000534 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000535 "wrapper_descriptor",
536 sizeof(PyWrapperDescrObject),
537 0,
538 (destructor)descr_dealloc, /* tp_dealloc */
539 0, /* tp_print */
540 0, /* tp_getattr */
541 0, /* tp_setattr */
542 0, /* tp_compare */
Armin Rigoc6686b72005-11-07 08:38:00 +0000543 (reprfunc)wrapperdescr_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000544 0, /* tp_as_number */
545 0, /* tp_as_sequence */
546 0, /* tp_as_mapping */
547 0, /* tp_hash */
548 (ternaryfunc)wrapperdescr_call, /* tp_call */
549 0, /* tp_str */
550 PyObject_GenericGetAttr, /* tp_getattro */
551 0, /* tp_setattro */
552 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000553 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000554 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000555 descr_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000556 0, /* tp_clear */
557 0, /* tp_richcompare */
558 0, /* tp_weaklistoffset */
559 0, /* tp_iter */
560 0, /* tp_iternext */
561 0, /* tp_methods */
562 descr_members, /* tp_members */
Armin Rigoc6686b72005-11-07 08:38:00 +0000563 wrapperdescr_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000564 0, /* tp_base */
565 0, /* tp_dict */
Armin Rigoc6686b72005-11-07 08:38:00 +0000566 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000567 0, /* tp_descr_set */
568};
569
570static PyDescrObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000571descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000572{
573 PyDescrObject *descr;
574
575 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
576 if (descr != NULL) {
577 Py_XINCREF(type);
578 descr->d_type = type;
579 descr->d_name = PyString_InternFromString(name);
580 if (descr->d_name == NULL) {
581 Py_DECREF(descr);
582 descr = NULL;
583 }
584 }
585 return descr;
586}
587
588PyObject *
589PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
590{
591 PyMethodDescrObject *descr;
592
593 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
594 type, method->ml_name);
595 if (descr != NULL)
596 descr->d_method = method;
597 return (PyObject *)descr;
598}
599
600PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +0000601PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
602{
603 PyMethodDescrObject *descr;
604
605 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
606 type, method->ml_name);
607 if (descr != NULL)
608 descr->d_method = method;
609 return (PyObject *)descr;
610}
611
612PyObject *
Guido van Rossum6f799372001-09-20 20:46:19 +0000613PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000614{
615 PyMemberDescrObject *descr;
616
617 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
618 type, member->name);
619 if (descr != NULL)
620 descr->d_member = member;
621 return (PyObject *)descr;
622}
623
624PyObject *
Guido van Rossum32d34c82001-09-20 21:45:26 +0000625PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000626{
627 PyGetSetDescrObject *descr;
628
629 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
630 type, getset->name);
631 if (descr != NULL)
632 descr->d_getset = getset;
633 return (PyObject *)descr;
634}
635
636PyObject *
637PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
638{
639 PyWrapperDescrObject *descr;
640
641 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
642 type, base->name);
643 if (descr != NULL) {
644 descr->d_base = base;
645 descr->d_wrapped = wrapped;
646 }
647 return (PyObject *)descr;
648}
649
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650
651/* --- Readonly proxy for dictionaries (actually any mapping) --- */
652
653/* This has no reason to be in this file except that adding new files is a
654 bit of a pain */
655
656typedef struct {
657 PyObject_HEAD
658 PyObject *dict;
659} proxyobject;
660
Martin v. Löwis18e16552006-02-15 17:27:45 +0000661static Py_ssize_t
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662proxy_len(proxyobject *pp)
663{
664 return PyObject_Size(pp->dict);
665}
666
667static PyObject *
668proxy_getitem(proxyobject *pp, PyObject *key)
669{
670 return PyObject_GetItem(pp->dict, key);
671}
672
673static PyMappingMethods proxy_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000674 (lenfunc)proxy_len, /* mp_length */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000675 (binaryfunc)proxy_getitem, /* mp_subscript */
676 0, /* mp_ass_subscript */
677};
678
679static int
680proxy_contains(proxyobject *pp, PyObject *key)
681{
Raymond Hettingerd3ae6722003-12-13 11:58:56 +0000682 return PyDict_Contains(pp->dict, key);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000683}
684
685static PySequenceMethods proxy_as_sequence = {
686 0, /* sq_length */
687 0, /* sq_concat */
688 0, /* sq_repeat */
689 0, /* sq_item */
690 0, /* sq_slice */
691 0, /* sq_ass_item */
692 0, /* sq_ass_slice */
693 (objobjproc)proxy_contains, /* sq_contains */
694 0, /* sq_inplace_concat */
695 0, /* sq_inplace_repeat */
696};
697
698static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000699proxy_has_key(proxyobject *pp, PyObject *key)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000700{
Raymond Hettingerd3ae6722003-12-13 11:58:56 +0000701 int res = PyDict_Contains(pp->dict, key);
Guido van Rossum98c65be2003-10-09 03:47:08 +0000702 if (res < 0)
703 return NULL;
704 return PyBool_FromLong(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000705}
706
707static PyObject *
708proxy_get(proxyobject *pp, PyObject *args)
709{
710 PyObject *key, *def = Py_None;
711
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000712 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000713 return NULL;
714 return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
715}
716
717static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000718proxy_keys(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000720 return PyMapping_Keys(pp->dict);
721}
722
723static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000724proxy_values(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726 return PyMapping_Values(pp->dict);
727}
728
729static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000730proxy_items(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000732 return PyMapping_Items(pp->dict);
733}
734
735static PyObject *
Walter Dörwalde990c792002-03-25 17:43:22 +0000736proxy_iterkeys(proxyobject *pp)
737{
738 return PyObject_CallMethod(pp->dict, "iterkeys", NULL);
739}
740
741static PyObject *
742proxy_itervalues(proxyobject *pp)
743{
744 return PyObject_CallMethod(pp->dict, "itervalues", NULL);
745}
746
747static PyObject *
748proxy_iteritems(proxyobject *pp)
749{
750 return PyObject_CallMethod(pp->dict, "iteritems", NULL);
751}
752static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000753proxy_copy(proxyobject *pp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000754{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755 return PyObject_CallMethod(pp->dict, "copy", NULL);
756}
757
758static PyMethodDef proxy_methods[] = {
Guido van Rossum59610422002-04-13 14:06:36 +0000759 {"has_key", (PyCFunction)proxy_has_key, METH_O,
Raymond Hettinger574aa322003-09-01 22:12:08 +0000760 PyDoc_STR("D.has_key(k) -> True if D has a key k, else False")},
Guido van Rossum59610422002-04-13 14:06:36 +0000761 {"get", (PyCFunction)proxy_get, METH_VARARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000762 PyDoc_STR("D.get(k[,d]) -> D[k] if D.has_key(k), else d."
763 " d defaults to None.")},
Guido van Rossum59610422002-04-13 14:06:36 +0000764 {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000765 PyDoc_STR("D.keys() -> list of D's keys")},
Guido van Rossum59610422002-04-13 14:06:36 +0000766 {"values", (PyCFunction)proxy_values, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000767 PyDoc_STR("D.values() -> list of D's values")},
Guido van Rossum59610422002-04-13 14:06:36 +0000768 {"items", (PyCFunction)proxy_items, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000769 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
Guido van Rossum59610422002-04-13 14:06:36 +0000770 {"iterkeys", (PyCFunction)proxy_iterkeys, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000771 PyDoc_STR("D.iterkeys() -> an iterator over the keys of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000772 {"itervalues",(PyCFunction)proxy_itervalues, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000773 PyDoc_STR("D.itervalues() -> an iterator over the values of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000774 {"iteritems", (PyCFunction)proxy_iteritems, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000775 PyDoc_STR("D.iteritems() ->"
776 " an iterator over the (key, value) items of D")},
Guido van Rossum59610422002-04-13 14:06:36 +0000777 {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +0000778 PyDoc_STR("D.copy() -> a shallow copy of D")},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000779 {0}
780};
781
782static void
783proxy_dealloc(proxyobject *pp)
784{
Guido van Rossum048eb752001-10-02 21:24:57 +0000785 _PyObject_GC_UNTRACK(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 Py_DECREF(pp->dict);
Guido van Rossum048eb752001-10-02 21:24:57 +0000787 PyObject_GC_Del(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000788}
789
790static PyObject *
791proxy_getiter(proxyobject *pp)
792{
793 return PyObject_GetIter(pp->dict);
794}
795
Neil Schemenauer26775122001-10-21 22:26:43 +0000796static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000797proxy_str(proxyobject *pp)
798{
799 return PyObject_Str(pp->dict);
800}
801
Guido van Rossum048eb752001-10-02 21:24:57 +0000802static int
803proxy_traverse(PyObject *self, visitproc visit, void *arg)
804{
805 proxyobject *pp = (proxyobject *)self;
Thomas Woutersc6e55062006-04-15 21:47:09 +0000806 Py_VISIT(pp->dict);
Guido van Rossum048eb752001-10-02 21:24:57 +0000807 return 0;
808}
809
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000810static int
811proxy_compare(proxyobject *v, PyObject *w)
812{
813 return PyObject_Compare(v->dict, w);
814}
815
816static PyObject *
817proxy_richcompare(proxyobject *v, PyObject *w, int op)
818{
819 return PyObject_RichCompare(v->dict, w, op);
820}
821
Neil Schemenauer26775122001-10-21 22:26:43 +0000822static PyTypeObject proxytype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000823 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000824 "dictproxy", /* tp_name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000825 sizeof(proxyobject), /* tp_basicsize */
826 0, /* tp_itemsize */
827 /* methods */
828 (destructor)proxy_dealloc, /* tp_dealloc */
829 0, /* tp_print */
830 0, /* tp_getattr */
831 0, /* tp_setattr */
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000832 (cmpfunc)proxy_compare, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000833 0, /* tp_repr */
834 0, /* tp_as_number */
835 &proxy_as_sequence, /* tp_as_sequence */
836 &proxy_as_mapping, /* tp_as_mapping */
837 0, /* tp_hash */
838 0, /* tp_call */
839 (reprfunc)proxy_str, /* tp_str */
840 PyObject_GenericGetAttr, /* tp_getattro */
841 0, /* tp_setattro */
842 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +0000843 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000844 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +0000845 proxy_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000846 0, /* tp_clear */
Raymond Hettinger29a6d442002-08-31 15:51:04 +0000847 (richcmpfunc)proxy_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000848 0, /* tp_weaklistoffset */
849 (getiterfunc)proxy_getiter, /* tp_iter */
850 0, /* tp_iternext */
851 proxy_methods, /* tp_methods */
852 0, /* tp_members */
853 0, /* tp_getset */
854 0, /* tp_base */
855 0, /* tp_dict */
856 0, /* tp_descr_get */
857 0, /* tp_descr_set */
858};
859
860PyObject *
861PyDictProxy_New(PyObject *dict)
862{
863 proxyobject *pp;
864
Guido van Rossum048eb752001-10-02 21:24:57 +0000865 pp = PyObject_GC_New(proxyobject, &proxytype);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866 if (pp != NULL) {
867 Py_INCREF(dict);
868 pp->dict = dict;
Guido van Rossum048eb752001-10-02 21:24:57 +0000869 _PyObject_GC_TRACK(pp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000870 }
871 return (PyObject *)pp;
872}
873
874
875/* --- Wrapper object for "slot" methods --- */
876
877/* This has no reason to be in this file except that adding new files is a
878 bit of a pain */
879
880typedef struct {
881 PyObject_HEAD
882 PyWrapperDescrObject *descr;
883 PyObject *self;
884} wrapperobject;
885
886static void
887wrapper_dealloc(wrapperobject *wp)
888{
Martin v. Löwisd5cfa542006-07-03 13:47:40 +0000889 PyObject_GC_UnTrack(wp);
890 Py_TRASHCAN_SAFE_BEGIN(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000891 Py_XDECREF(wp->descr);
892 Py_XDECREF(wp->self);
Guido van Rossum048eb752001-10-02 21:24:57 +0000893 PyObject_GC_Del(wp);
Martin v. Löwisd5cfa542006-07-03 13:47:40 +0000894 Py_TRASHCAN_SAFE_END(wp)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000895}
896
Armin Rigoc6686b72005-11-07 08:38:00 +0000897static int
898wrapper_compare(wrapperobject *a, wrapperobject *b)
899{
Armin Rigofd01d792006-06-08 10:56:24 +0000900 if (a->descr == b->descr)
901 return PyObject_Compare(a->self, b->self);
Armin Rigoc6686b72005-11-07 08:38:00 +0000902 else
903 return (a->descr < b->descr) ? -1 : 1;
904}
905
Armin Rigofd01d792006-06-08 10:56:24 +0000906static long
907wrapper_hash(wrapperobject *wp)
908{
909 int x, y;
910 x = _Py_HashPointer(wp->descr);
911 if (x == -1)
912 return -1;
913 y = PyObject_Hash(wp->self);
914 if (y == -1)
915 return -1;
916 x = x ^ y;
917 if (x == -1)
918 x = -2;
919 return x;
920}
921
Armin Rigoc6686b72005-11-07 08:38:00 +0000922static PyObject *
923wrapper_repr(wrapperobject *wp)
924{
925 return PyString_FromFormat("<method-wrapper '%s' of %s object at %p>",
926 wp->descr->d_base->name,
927 wp->self->ob_type->tp_name,
928 wp->self);
929}
930
931static PyMemberDef wrapper_members[] = {
932 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000933 {0}
934};
935
936static PyObject *
Armin Rigoc6686b72005-11-07 08:38:00 +0000937wrapper_objclass(wrapperobject *wp)
938{
939 PyObject *c = (PyObject *)wp->descr->d_type;
940
941 Py_INCREF(c);
942 return c;
943}
944
945static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946wrapper_name(wrapperobject *wp)
947{
948 char *s = wp->descr->d_base->name;
949
950 return PyString_FromString(s);
951}
952
953static PyObject *
954wrapper_doc(wrapperobject *wp)
955{
956 char *s = wp->descr->d_base->doc;
957
958 if (s == NULL) {
959 Py_INCREF(Py_None);
960 return Py_None;
961 }
962 else {
963 return PyString_FromString(s);
964 }
965}
966
Guido van Rossum32d34c82001-09-20 21:45:26 +0000967static PyGetSetDef wrapper_getsets[] = {
Armin Rigoc6686b72005-11-07 08:38:00 +0000968 {"__objclass__", (getter)wrapper_objclass},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000969 {"__name__", (getter)wrapper_name},
970 {"__doc__", (getter)wrapper_doc},
971 {0}
972};
973
974static PyObject *
975wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
976{
977 wrapperfunc wrapper = wp->descr->d_base->wrapper;
978 PyObject *self = wp->self;
979
Guido van Rossumc8e56452001-10-22 00:43:43 +0000980 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
981 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
982 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
983 }
984
985 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
986 PyErr_Format(PyExc_TypeError,
987 "wrapper %s doesn't take keyword arguments",
988 wp->descr->d_base->name);
989 return NULL;
990 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991 return (*wrapper)(self, args, wp->descr->d_wrapped);
992}
993
Guido van Rossum048eb752001-10-02 21:24:57 +0000994static int
995wrapper_traverse(PyObject *self, visitproc visit, void *arg)
996{
997 wrapperobject *wp = (wrapperobject *)self;
Thomas Woutersc6e55062006-04-15 21:47:09 +0000998 Py_VISIT(wp->descr);
999 Py_VISIT(wp->self);
Guido van Rossum048eb752001-10-02 21:24:57 +00001000 return 0;
1001}
1002
Neil Schemenauer26775122001-10-21 22:26:43 +00001003static PyTypeObject wrappertype = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001004 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001005 "method-wrapper", /* tp_name */
1006 sizeof(wrapperobject), /* tp_basicsize */
1007 0, /* tp_itemsize */
1008 /* methods */
1009 (destructor)wrapper_dealloc, /* tp_dealloc */
1010 0, /* tp_print */
1011 0, /* tp_getattr */
1012 0, /* tp_setattr */
Armin Rigoc6686b72005-11-07 08:38:00 +00001013 (cmpfunc)wrapper_compare, /* tp_compare */
1014 (reprfunc)wrapper_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015 0, /* tp_as_number */
1016 0, /* tp_as_sequence */
1017 0, /* tp_as_mapping */
Armin Rigofd01d792006-06-08 10:56:24 +00001018 (hashfunc)wrapper_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001019 (ternaryfunc)wrapper_call, /* tp_call */
1020 0, /* tp_str */
1021 PyObject_GenericGetAttr, /* tp_getattro */
1022 0, /* tp_setattro */
1023 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001024 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001025 0, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001026 wrapper_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001027 0, /* tp_clear */
1028 0, /* tp_richcompare */
1029 0, /* tp_weaklistoffset */
1030 0, /* tp_iter */
1031 0, /* tp_iternext */
Armin Rigoc6686b72005-11-07 08:38:00 +00001032 0, /* tp_methods */
1033 wrapper_members, /* tp_members */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034 wrapper_getsets, /* tp_getset */
1035 0, /* tp_base */
1036 0, /* tp_dict */
1037 0, /* tp_descr_get */
1038 0, /* tp_descr_set */
1039};
1040
1041PyObject *
1042PyWrapper_New(PyObject *d, PyObject *self)
1043{
1044 wrapperobject *wp;
1045 PyWrapperDescrObject *descr;
1046
1047 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1048 descr = (PyWrapperDescrObject *)d;
1049 assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
1050
Guido van Rossum048eb752001-10-02 21:24:57 +00001051 wp = PyObject_GC_New(wrapperobject, &wrappertype);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001052 if (wp != NULL) {
1053 Py_INCREF(descr);
1054 wp->descr = descr;
1055 Py_INCREF(self);
1056 wp->self = self;
Guido van Rossum048eb752001-10-02 21:24:57 +00001057 _PyObject_GC_TRACK(wp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001058 }
1059 return (PyObject *)wp;
1060}
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001061
1062
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001063/* A built-in 'property' type */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001064
1065/*
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001066 class property(object):
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001067
Guido van Rossumba2485f2001-12-10 18:03:34 +00001068 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
Georg Brandl533ff6f2006-03-08 18:09:27 +00001069 if doc is None and fget is not None and hasattr(fget, "__doc__"):
1070 doc = fget.__doc__
Guido van Rossumba2485f2001-12-10 18:03:34 +00001071 self.__get = fget
1072 self.__set = fset
1073 self.__del = fdel
1074 self.__doc__ = doc
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001075
Guido van Rossumba2485f2001-12-10 18:03:34 +00001076 def __get__(self, inst, type=None):
Guido van Rossumba2485f2001-12-10 18:03:34 +00001077 if inst is None:
1078 return self
Guido van Rossum7171f1c2001-12-10 18:06:21 +00001079 if self.__get is None:
1080 raise AttributeError, "unreadable attribute"
Guido van Rossumba2485f2001-12-10 18:03:34 +00001081 return self.__get(inst)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001082
Guido van Rossumba2485f2001-12-10 18:03:34 +00001083 def __set__(self, inst, value):
1084 if self.__set is None:
1085 raise AttributeError, "can't set attribute"
1086 return self.__set(inst, value)
1087
1088 def __delete__(self, inst):
1089 if self.__del is None:
1090 raise AttributeError, "can't delete attribute"
1091 return self.__del(inst)
1092
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001093*/
1094
1095typedef struct {
1096 PyObject_HEAD
Tim Peters66c1a522001-09-24 21:17:50 +00001097 PyObject *prop_get;
1098 PyObject *prop_set;
1099 PyObject *prop_del;
1100 PyObject *prop_doc;
Christian Heimes90e10e72007-12-14 02:35:23 +00001101 int getter_doc;
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001102} propertyobject;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001103
Christian Heimes90e10e72007-12-14 02:35:23 +00001104static PyObject * property_copy(PyObject *, PyObject *, PyObject *,
1105 PyObject *, PyObject *);
1106
Tim Peters66c1a522001-09-24 21:17:50 +00001107static PyMemberDef property_members[] = {
1108 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1109 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1110 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1111 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1112 {0}
1113};
1114
Christian Heimes90e10e72007-12-14 02:35:23 +00001115
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001116PyDoc_STRVAR(getter_doc,
1117 "Descriptor to change the getter on a property.");
1118
1119PyObject *
1120property_getter(PyObject *self, PyObject *getter)
1121{
Christian Heimes90e10e72007-12-14 02:35:23 +00001122 return property_copy(self, getter, NULL, NULL, NULL);
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001123}
1124
Christian Heimes90e10e72007-12-14 02:35:23 +00001125
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001126PyDoc_STRVAR(setter_doc,
Christian Heimes90e10e72007-12-14 02:35:23 +00001127 "Descriptor to change the setter on a property.");
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001128
1129PyObject *
1130property_setter(PyObject *self, PyObject *setter)
1131{
Christian Heimes90e10e72007-12-14 02:35:23 +00001132 return property_copy(self, NULL, setter, NULL, NULL);
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001133}
1134
Christian Heimes90e10e72007-12-14 02:35:23 +00001135
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001136PyDoc_STRVAR(deleter_doc,
1137 "Descriptor to change the deleter on a property.");
1138
1139PyObject *
1140property_deleter(PyObject *self, PyObject *deleter)
1141{
Christian Heimes90e10e72007-12-14 02:35:23 +00001142 return property_copy(self, NULL, NULL, deleter, NULL);
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001143}
1144
1145
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001146static PyMethodDef property_methods[] = {
1147 {"getter", property_getter, METH_O, getter_doc},
1148 {"setter", property_setter, METH_O, setter_doc},
1149 {"deleter", property_deleter, METH_O, deleter_doc},
1150 {0}
1151};
1152
Tim Peters66c1a522001-09-24 21:17:50 +00001153
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001154static void
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001155property_dealloc(PyObject *self)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001156{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001157 propertyobject *gs = (propertyobject *)self;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001158
Guido van Rossum048eb752001-10-02 21:24:57 +00001159 _PyObject_GC_UNTRACK(self);
Tim Peters66c1a522001-09-24 21:17:50 +00001160 Py_XDECREF(gs->prop_get);
1161 Py_XDECREF(gs->prop_set);
1162 Py_XDECREF(gs->prop_del);
1163 Py_XDECREF(gs->prop_doc);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001164 self->ob_type->tp_free(self);
1165}
1166
1167static PyObject *
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001168property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
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 Rossum29a62dd2001-08-23 21:40:38 +00001172 if (obj == NULL || obj == Py_None) {
1173 Py_INCREF(self);
1174 return self;
1175 }
Guido van Rossumb75ba912001-12-10 18:00:15 +00001176 if (gs->prop_get == NULL) {
1177 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1178 return NULL;
1179 }
Tim Peters66c1a522001-09-24 21:17:50 +00001180 return PyObject_CallFunction(gs->prop_get, "(O)", obj);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001181}
1182
1183static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001184property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001185{
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001186 propertyobject *gs = (propertyobject *)self;
Guido van Rossum271410a2001-08-24 15:23:20 +00001187 PyObject *func, *res;
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001188
Guido van Rossum271410a2001-08-24 15:23:20 +00001189 if (value == NULL)
Tim Peters66c1a522001-09-24 21:17:50 +00001190 func = gs->prop_del;
Guido van Rossum271410a2001-08-24 15:23:20 +00001191 else
Tim Peters66c1a522001-09-24 21:17:50 +00001192 func = gs->prop_set;
Guido van Rossum271410a2001-08-24 15:23:20 +00001193 if (func == NULL) {
1194 PyErr_SetString(PyExc_AttributeError,
1195 value == NULL ?
1196 "can't delete attribute" :
1197 "can't set attribute");
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001198 return -1;
1199 }
Guido van Rossum845fc482001-08-24 10:17:36 +00001200 if (value == NULL)
Guido van Rossum271410a2001-08-24 15:23:20 +00001201 res = PyObject_CallFunction(func, "(O)", obj);
Guido van Rossum845fc482001-08-24 10:17:36 +00001202 else
Guido van Rossum271410a2001-08-24 15:23:20 +00001203 res = PyObject_CallFunction(func, "(OO)", obj, value);
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001204 if (res == NULL)
1205 return -1;
1206 Py_DECREF(res);
1207 return 0;
1208}
1209
Christian Heimes90e10e72007-12-14 02:35:23 +00001210static PyObject *
1211property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del,
1212 PyObject *doc)
1213{
1214 propertyobject *pold = (propertyobject *)old;
1215 propertyobject *pnew = NULL;
1216 PyObject *new, *type;
1217
1218 type = PyObject_Type(old);
1219 if (type == NULL)
1220 return NULL;
1221
1222 if (get == NULL || get == Py_None) {
1223 Py_XDECREF(get);
1224 get = pold->prop_get ? pold->prop_get : Py_None;
1225 }
1226 if (set == NULL || set == Py_None) {
1227 Py_XDECREF(set);
1228 set = pold->prop_set ? pold->prop_set : Py_None;
1229 }
1230 if (del == NULL || del == Py_None) {
1231 Py_XDECREF(del);
1232 del = pold->prop_del ? pold->prop_del : Py_None;
1233 }
1234 if (doc == NULL || doc == Py_None) {
1235 Py_XDECREF(doc);
1236 doc = pold->prop_doc ? pold->prop_doc : Py_None;
1237 }
1238
1239 new = PyObject_CallFunction(type, "OOOO", get, set, del, doc);
1240 if (new == NULL)
1241 return NULL;
1242 pnew = (propertyobject *)new;
1243
1244 if (pold->getter_doc && get != Py_None) {
1245 PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");
1246 if (get_doc != NULL) {
1247 Py_XDECREF(pnew->prop_doc);
1248 pnew->prop_doc = get_doc; /* get_doc already INCREF'd by GetAttr */
1249 pnew->getter_doc = 1;
1250 } else {
1251 PyErr_Clear();
1252 }
1253 }
1254 return new;
1255}
1256
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001257static int
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001258property_init(PyObject *self, PyObject *args, PyObject *kwds)
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001259{
Tim Peters66c1a522001-09-24 21:17:50 +00001260 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001261 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
Christian Heimes90e10e72007-12-14 02:35:23 +00001262 propertyobject *prop = (propertyobject *)self;
1263
Tim Peters66c1a522001-09-24 21:17:50 +00001264 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
Christian Heimes90e10e72007-12-14 02:35:23 +00001265 kwlist, &get, &set, &del, &doc))
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001266 return -1;
Tim Peters66c1a522001-09-24 21:17:50 +00001267
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001268 if (get == Py_None)
1269 get = NULL;
1270 if (set == Py_None)
1271 set = NULL;
Tim Peters66c1a522001-09-24 21:17:50 +00001272 if (del == Py_None)
1273 del = NULL;
1274
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001275 Py_XINCREF(get);
1276 Py_XINCREF(set);
Guido van Rossum271410a2001-08-24 15:23:20 +00001277 Py_XINCREF(del);
Georg Brandle9462c72006-08-04 18:03:37 +00001278 Py_XINCREF(doc);
1279
Christian Heimes90e10e72007-12-14 02:35:23 +00001280 prop->prop_get = get;
1281 prop->prop_set = set;
1282 prop->prop_del = del;
1283 prop->prop_doc = doc;
1284 prop->getter_doc = 0;
1285
Georg Brandle9462c72006-08-04 18:03:37 +00001286 /* if no docstring given and the getter has one, use that one */
1287 if ((doc == NULL || doc == Py_None) && get != NULL) {
1288 PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");
1289 if (get_doc != NULL) {
Christian Heimes90e10e72007-12-14 02:35:23 +00001290 Py_XDECREF(prop->prop_doc);
1291 prop->prop_doc = get_doc; /* get_doc already INCREF'd by GetAttr */
1292 prop->getter_doc = 1;
Georg Brandle9462c72006-08-04 18:03:37 +00001293 } else {
1294 PyErr_Clear();
1295 }
1296 }
Tim Peters66c1a522001-09-24 21:17:50 +00001297
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001298 return 0;
1299}
1300
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001301PyDoc_STRVAR(property_doc,
Tim Peters66c1a522001-09-24 21:17:50 +00001302"property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1303"\n"
1304"fget is a function to be used for getting an attribute value, and likewise\n"
1305"fset is a function for setting, and fdel a function for del'ing, an\n"
1306"attribute. Typical use is to define a managed attribute x:\n"
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001307"class C(object):\n"
Christian Heimes3d4c3162007-11-12 01:15:40 +00001308" def getx(self): return self._x\n"
1309" def setx(self, value): self._x = value\n"
1310" def delx(self): del self._x\n"
1311" x = property(getx, setx, delx, \"I'm the 'x' property.\")\n"
1312"\n"
Andrew M. Kuchlingabfe4532007-11-12 01:25:21 +00001313"Decorators make defining new properties or modifying existing ones easy:\n"
Christian Heimes3d4c3162007-11-12 01:15:40 +00001314"class C(object):\n"
1315" @property\n"
1316" def x(self): return self._x\n"
1317" @x.setter\n"
1318" def x(self, value): self._x = value\n"
1319" @x.deleter\n"
1320" def x(self): del self._x\n"
1321);
Guido van Rossum91c0d8a2001-08-24 09:55:51 +00001322
Guido van Rossum048eb752001-10-02 21:24:57 +00001323static int
1324property_traverse(PyObject *self, visitproc visit, void *arg)
1325{
1326 propertyobject *pp = (propertyobject *)self;
Thomas Woutersc6e55062006-04-15 21:47:09 +00001327 Py_VISIT(pp->prop_get);
1328 Py_VISIT(pp->prop_set);
1329 Py_VISIT(pp->prop_del);
1330 Py_VISIT(pp->prop_doc);
Guido van Rossum048eb752001-10-02 21:24:57 +00001331 return 0;
1332}
1333
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001334PyTypeObject PyProperty_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001335 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001336 "property", /* tp_name */
1337 sizeof(propertyobject), /* tp_basicsize */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001338 0, /* tp_itemsize */
1339 /* methods */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001340 property_dealloc, /* tp_dealloc */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001341 0, /* tp_print */
1342 0, /* tp_getattr */
1343 0, /* tp_setattr */
1344 0, /* tp_compare */
1345 0, /* tp_repr */
1346 0, /* tp_as_number */
1347 0, /* tp_as_sequence */
1348 0, /* tp_as_mapping */
1349 0, /* tp_hash */
1350 0, /* tp_call */
1351 0, /* tp_str */
1352 PyObject_GenericGetAttr, /* tp_getattro */
1353 0, /* tp_setattro */
1354 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001355 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1356 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001357 property_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001358 property_traverse, /* tp_traverse */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001359 0, /* tp_clear */
1360 0, /* tp_richcompare */
1361 0, /* tp_weaklistoffset */
1362 0, /* tp_iter */
1363 0, /* tp_iternext */
Guido van Rossumd1ef7892007-11-10 22:12:24 +00001364 property_methods, /* tp_methods */
Tim Peters66c1a522001-09-24 21:17:50 +00001365 property_members, /* tp_members */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001366 0, /* tp_getset */
1367 0, /* tp_base */
1368 0, /* tp_dict */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001369 property_descr_get, /* tp_descr_get */
1370 property_descr_set, /* tp_descr_set */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001371 0, /* tp_dictoffset */
Guido van Rossum8bce4ac2001-09-06 21:56:42 +00001372 property_init, /* tp_init */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001373 PyType_GenericAlloc, /* tp_alloc */
1374 PyType_GenericNew, /* tp_new */
Neil Schemenauer99b5d282002-04-12 02:44:22 +00001375 PyObject_GC_Del, /* tp_free */
Guido van Rossum29a62dd2001-08-23 21:40:38 +00001376};