Document PyErr_WarnEx. (Bad Neal! No biscuit!)
[pytest.git] / Objects / descrobject.c
blob51676f6b0622628b77eda297d2332cf45db12aa7
1 /* Descriptors -- a new, flexible way to describe attributes */
3 #include "Python.h"
4 #include "structmember.h" /* Why is this not included in Python.h? */
6 static void
7 descr_dealloc(PyDescrObject *descr)
9 _PyObject_GC_UNTRACK(descr);
10 Py_XDECREF(descr->d_type);
11 Py_XDECREF(descr->d_name);
12 PyObject_GC_Del(descr);
15 static char *
16 descr_name(PyDescrObject *descr)
18 if (descr->d_name != NULL && PyString_Check(descr->d_name))
19 return PyString_AS_STRING(descr->d_name);
20 else
21 return "?";
24 static PyObject *
25 descr_repr(PyDescrObject *descr, char *format)
27 return PyString_FromFormat(format, descr_name(descr),
28 descr->d_type->tp_name);
31 static PyObject *
32 method_repr(PyMethodDescrObject *descr)
34 return descr_repr((PyDescrObject *)descr,
35 "<method '%s' of '%s' objects>");
38 static PyObject *
39 member_repr(PyMemberDescrObject *descr)
41 return descr_repr((PyDescrObject *)descr,
42 "<member '%s' of '%s' objects>");
45 static PyObject *
46 getset_repr(PyGetSetDescrObject *descr)
48 return descr_repr((PyDescrObject *)descr,
49 "<attribute '%s' of '%s' objects>");
52 static PyObject *
53 wrapperdescr_repr(PyWrapperDescrObject *descr)
55 return descr_repr((PyDescrObject *)descr,
56 "<slot wrapper '%s' of '%s' objects>");
59 static int
60 descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
62 if (obj == NULL) {
63 Py_INCREF(descr);
64 *pres = (PyObject *)descr;
65 return 1;
67 if (!PyObject_TypeCheck(obj, descr->d_type)) {
68 PyErr_Format(PyExc_TypeError,
69 "descriptor '%s' for '%s' objects "
70 "doesn't apply to '%s' object",
71 descr_name((PyDescrObject *)descr),
72 descr->d_type->tp_name,
73 obj->ob_type->tp_name);
74 *pres = NULL;
75 return 1;
77 return 0;
80 static PyObject *
81 classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
83 /* 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;
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;
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;
115 return PyCFunction_New(descr->d_method, type);
118 static PyObject *
119 method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
121 PyObject *res;
123 if (descr_check((PyDescrObject *)descr, obj, &res))
124 return res;
125 return PyCFunction_New(descr->d_method, obj);
128 static PyObject *
129 member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
131 PyObject *res;
133 if (descr_check((PyDescrObject *)descr, obj, &res))
134 return res;
135 return PyMember_GetOne((char *)obj, descr->d_member);
138 static PyObject *
139 getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
141 PyObject *res;
143 if (descr_check((PyDescrObject *)descr, obj, &res))
144 return res;
145 if (descr->d_getset->get != NULL)
146 return descr->d_getset->get(obj, descr->d_getset->closure);
147 PyErr_Format(PyExc_AttributeError,
148 "attribute '%.300s' of '%.100s' objects is not readable",
149 descr_name((PyDescrObject *)descr),
150 descr->d_type->tp_name);
151 return NULL;
154 static PyObject *
155 wrapperdescr_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
157 PyObject *res;
159 if (descr_check((PyDescrObject *)descr, obj, &res))
160 return res;
161 return PyWrapper_New((PyObject *)descr, obj);
164 static int
165 descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
166 int *pres)
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;
179 return 0;
182 static int
183 member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
185 int res;
187 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
188 return res;
189 return PyMember_SetOne((char *)obj, descr->d_member, value);
192 static int
193 getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
195 int res;
197 if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
198 return res;
199 if (descr->d_getset->set != NULL)
200 return descr->d_getset->set(obj, value,
201 descr->d_getset->closure);
202 PyErr_Format(PyExc_AttributeError,
203 "attribute '%.300s' of '%.100s' objects is not writable",
204 descr_name((PyDescrObject *)descr),
205 descr->d_type->tp_name);
206 return -1;
209 static PyObject *
210 methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
212 Py_ssize_t argc;
213 PyObject *self, *func, *result;
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;
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;
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;
246 result = PyEval_CallObjectWithKeywords(func, args, kwds);
247 Py_DECREF(args);
248 Py_DECREF(func);
249 return result;
252 static PyObject *
253 classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
254 PyObject *kwds)
256 PyObject *func, *result;
258 func = PyCFunction_New(descr->d_method, (PyObject *)descr->d_type);
259 if (func == NULL)
260 return NULL;
262 result = PyEval_CallObjectWithKeywords(func, args, kwds);
263 Py_DECREF(func);
264 return result;
267 static PyObject *
268 wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
270 Py_ssize_t argc;
271 PyObject *self, *func, *result;
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;
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;
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;
304 result = PyEval_CallObjectWithKeywords(func, args, kwds);
305 Py_DECREF(args);
306 Py_DECREF(func);
307 return result;
310 static PyObject *
311 method_get_doc(PyMethodDescrObject *descr, void *closure)
313 if (descr->d_method->ml_doc == NULL) {
314 Py_INCREF(Py_None);
315 return Py_None;
317 return PyString_FromString(descr->d_method->ml_doc);
320 static PyMemberDef descr_members[] = {
321 {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
322 {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
326 static PyGetSetDef method_getset[] = {
327 {"__doc__", (getter)method_get_doc},
331 static PyObject *
332 member_get_doc(PyMemberDescrObject *descr, void *closure)
334 if (descr->d_member->doc == NULL) {
335 Py_INCREF(Py_None);
336 return Py_None;
338 return PyString_FromString(descr->d_member->doc);
341 static PyGetSetDef member_getset[] = {
342 {"__doc__", (getter)member_get_doc},
346 static PyObject *
347 getset_get_doc(PyGetSetDescrObject *descr, void *closure)
349 if (descr->d_getset->doc == NULL) {
350 Py_INCREF(Py_None);
351 return Py_None;
353 return PyString_FromString(descr->d_getset->doc);
356 static PyGetSetDef getset_getset[] = {
357 {"__doc__", (getter)getset_get_doc},
361 static PyObject *
362 wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
364 if (descr->d_base->doc == NULL) {
365 Py_INCREF(Py_None);
366 return Py_None;
368 return PyString_FromString(descr->d_base->doc);
371 static PyGetSetDef wrapperdescr_getset[] = {
372 {"__doc__", (getter)wrapperdescr_get_doc},
376 static int
377 descr_traverse(PyObject *self, visitproc visit, void *arg)
379 PyDescrObject *descr = (PyDescrObject *)self;
380 Py_VISIT(descr->d_type);
381 return 0;
384 static PyTypeObject PyMethodDescr_Type = {
385 PyObject_HEAD_INIT(&PyType_Type)
387 "method_descriptor",
388 sizeof(PyMethodDescrObject),
390 (destructor)descr_dealloc, /* tp_dealloc */
391 0, /* tp_print */
392 0, /* tp_getattr */
393 0, /* tp_setattr */
394 0, /* tp_compare */
395 (reprfunc)method_repr, /* tp_repr */
396 0, /* tp_as_number */
397 0, /* tp_as_sequence */
398 0, /* tp_as_mapping */
399 0, /* tp_hash */
400 (ternaryfunc)methoddescr_call, /* tp_call */
401 0, /* tp_str */
402 PyObject_GenericGetAttr, /* tp_getattro */
403 0, /* tp_setattro */
404 0, /* tp_as_buffer */
405 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
406 0, /* tp_doc */
407 descr_traverse, /* tp_traverse */
408 0, /* tp_clear */
409 0, /* tp_richcompare */
410 0, /* tp_weaklistoffset */
411 0, /* tp_iter */
412 0, /* tp_iternext */
413 0, /* tp_methods */
414 descr_members, /* tp_members */
415 method_getset, /* tp_getset */
416 0, /* tp_base */
417 0, /* tp_dict */
418 (descrgetfunc)method_get, /* tp_descr_get */
419 0, /* tp_descr_set */
422 /* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
423 static PyTypeObject PyClassMethodDescr_Type = {
424 PyObject_HEAD_INIT(&PyType_Type)
426 "classmethod_descriptor",
427 sizeof(PyMethodDescrObject),
429 (destructor)descr_dealloc, /* tp_dealloc */
430 0, /* tp_print */
431 0, /* tp_getattr */
432 0, /* tp_setattr */
433 0, /* tp_compare */
434 (reprfunc)method_repr, /* tp_repr */
435 0, /* tp_as_number */
436 0, /* tp_as_sequence */
437 0, /* tp_as_mapping */
438 0, /* tp_hash */
439 (ternaryfunc)classmethoddescr_call, /* tp_call */
440 0, /* tp_str */
441 PyObject_GenericGetAttr, /* tp_getattro */
442 0, /* tp_setattro */
443 0, /* tp_as_buffer */
444 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
445 0, /* tp_doc */
446 descr_traverse, /* tp_traverse */
447 0, /* tp_clear */
448 0, /* tp_richcompare */
449 0, /* tp_weaklistoffset */
450 0, /* tp_iter */
451 0, /* tp_iternext */
452 0, /* tp_methods */
453 descr_members, /* tp_members */
454 method_getset, /* tp_getset */
455 0, /* tp_base */
456 0, /* tp_dict */
457 (descrgetfunc)classmethod_get, /* tp_descr_get */
458 0, /* tp_descr_set */
461 static PyTypeObject PyMemberDescr_Type = {
462 PyObject_HEAD_INIT(&PyType_Type)
464 "member_descriptor",
465 sizeof(PyMemberDescrObject),
467 (destructor)descr_dealloc, /* tp_dealloc */
468 0, /* tp_print */
469 0, /* tp_getattr */
470 0, /* tp_setattr */
471 0, /* tp_compare */
472 (reprfunc)member_repr, /* tp_repr */
473 0, /* tp_as_number */
474 0, /* tp_as_sequence */
475 0, /* tp_as_mapping */
476 0, /* tp_hash */
477 0, /* tp_call */
478 0, /* tp_str */
479 PyObject_GenericGetAttr, /* tp_getattro */
480 0, /* tp_setattro */
481 0, /* tp_as_buffer */
482 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
483 0, /* tp_doc */
484 descr_traverse, /* tp_traverse */
485 0, /* tp_clear */
486 0, /* tp_richcompare */
487 0, /* tp_weaklistoffset */
488 0, /* tp_iter */
489 0, /* tp_iternext */
490 0, /* tp_methods */
491 descr_members, /* tp_members */
492 member_getset, /* tp_getset */
493 0, /* tp_base */
494 0, /* tp_dict */
495 (descrgetfunc)member_get, /* tp_descr_get */
496 (descrsetfunc)member_set, /* tp_descr_set */
499 static PyTypeObject PyGetSetDescr_Type = {
500 PyObject_HEAD_INIT(&PyType_Type)
502 "getset_descriptor",
503 sizeof(PyGetSetDescrObject),
505 (destructor)descr_dealloc, /* tp_dealloc */
506 0, /* tp_print */
507 0, /* tp_getattr */
508 0, /* tp_setattr */
509 0, /* tp_compare */
510 (reprfunc)getset_repr, /* tp_repr */
511 0, /* tp_as_number */
512 0, /* tp_as_sequence */
513 0, /* tp_as_mapping */
514 0, /* tp_hash */
515 0, /* tp_call */
516 0, /* tp_str */
517 PyObject_GenericGetAttr, /* tp_getattro */
518 0, /* tp_setattro */
519 0, /* tp_as_buffer */
520 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
521 0, /* tp_doc */
522 descr_traverse, /* tp_traverse */
523 0, /* tp_clear */
524 0, /* tp_richcompare */
525 0, /* tp_weaklistoffset */
526 0, /* tp_iter */
527 0, /* tp_iternext */
528 0, /* tp_methods */
529 descr_members, /* tp_members */
530 getset_getset, /* tp_getset */
531 0, /* tp_base */
532 0, /* tp_dict */
533 (descrgetfunc)getset_get, /* tp_descr_get */
534 (descrsetfunc)getset_set, /* tp_descr_set */
537 PyTypeObject PyWrapperDescr_Type = {
538 PyObject_HEAD_INIT(&PyType_Type)
540 "wrapper_descriptor",
541 sizeof(PyWrapperDescrObject),
543 (destructor)descr_dealloc, /* tp_dealloc */
544 0, /* tp_print */
545 0, /* tp_getattr */
546 0, /* tp_setattr */
547 0, /* tp_compare */
548 (reprfunc)wrapperdescr_repr, /* tp_repr */
549 0, /* tp_as_number */
550 0, /* tp_as_sequence */
551 0, /* tp_as_mapping */
552 0, /* tp_hash */
553 (ternaryfunc)wrapperdescr_call, /* tp_call */
554 0, /* tp_str */
555 PyObject_GenericGetAttr, /* tp_getattro */
556 0, /* tp_setattro */
557 0, /* tp_as_buffer */
558 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
559 0, /* tp_doc */
560 descr_traverse, /* tp_traverse */
561 0, /* tp_clear */
562 0, /* tp_richcompare */
563 0, /* tp_weaklistoffset */
564 0, /* tp_iter */
565 0, /* tp_iternext */
566 0, /* tp_methods */
567 descr_members, /* tp_members */
568 wrapperdescr_getset, /* tp_getset */
569 0, /* tp_base */
570 0, /* tp_dict */
571 (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
572 0, /* tp_descr_set */
575 static PyDescrObject *
576 descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
578 PyDescrObject *descr;
580 descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
581 if (descr != NULL) {
582 Py_XINCREF(type);
583 descr->d_type = type;
584 descr->d_name = PyString_InternFromString(name);
585 if (descr->d_name == NULL) {
586 Py_DECREF(descr);
587 descr = NULL;
590 return descr;
593 PyObject *
594 PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
596 PyMethodDescrObject *descr;
598 descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
599 type, method->ml_name);
600 if (descr != NULL)
601 descr->d_method = method;
602 return (PyObject *)descr;
605 PyObject *
606 PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
608 PyMethodDescrObject *descr;
610 descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
611 type, method->ml_name);
612 if (descr != NULL)
613 descr->d_method = method;
614 return (PyObject *)descr;
617 PyObject *
618 PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
620 PyMemberDescrObject *descr;
622 descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
623 type, member->name);
624 if (descr != NULL)
625 descr->d_member = member;
626 return (PyObject *)descr;
629 PyObject *
630 PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
632 PyGetSetDescrObject *descr;
634 descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
635 type, getset->name);
636 if (descr != NULL)
637 descr->d_getset = getset;
638 return (PyObject *)descr;
641 PyObject *
642 PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
644 PyWrapperDescrObject *descr;
646 descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
647 type, base->name);
648 if (descr != NULL) {
649 descr->d_base = base;
650 descr->d_wrapped = wrapped;
652 return (PyObject *)descr;
656 /* --- Readonly proxy for dictionaries (actually any mapping) --- */
658 /* This has no reason to be in this file except that adding new files is a
659 bit of a pain */
661 typedef struct {
662 PyObject_HEAD
663 PyObject *dict;
664 } proxyobject;
666 static Py_ssize_t
667 proxy_len(proxyobject *pp)
669 return PyObject_Size(pp->dict);
672 static PyObject *
673 proxy_getitem(proxyobject *pp, PyObject *key)
675 return PyObject_GetItem(pp->dict, key);
678 static PyMappingMethods proxy_as_mapping = {
679 (lenfunc)proxy_len, /* mp_length */
680 (binaryfunc)proxy_getitem, /* mp_subscript */
681 0, /* mp_ass_subscript */
684 static int
685 proxy_contains(proxyobject *pp, PyObject *key)
687 return PyDict_Contains(pp->dict, key);
690 static PySequenceMethods proxy_as_sequence = {
691 0, /* sq_length */
692 0, /* sq_concat */
693 0, /* sq_repeat */
694 0, /* sq_item */
695 0, /* sq_slice */
696 0, /* sq_ass_item */
697 0, /* sq_ass_slice */
698 (objobjproc)proxy_contains, /* sq_contains */
699 0, /* sq_inplace_concat */
700 0, /* sq_inplace_repeat */
703 static PyObject *
704 proxy_has_key(proxyobject *pp, PyObject *key)
706 int res = PyDict_Contains(pp->dict, key);
707 if (res < 0)
708 return NULL;
709 return PyBool_FromLong(res);
712 static PyObject *
713 proxy_get(proxyobject *pp, PyObject *args)
715 PyObject *key, *def = Py_None;
717 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
718 return NULL;
719 return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
722 static PyObject *
723 proxy_keys(proxyobject *pp)
725 return PyMapping_Keys(pp->dict);
728 static PyObject *
729 proxy_values(proxyobject *pp)
731 return PyMapping_Values(pp->dict);
734 static PyObject *
735 proxy_items(proxyobject *pp)
737 return PyMapping_Items(pp->dict);
740 static PyObject *
741 proxy_iterkeys(proxyobject *pp)
743 return PyObject_CallMethod(pp->dict, "iterkeys", NULL);
746 static PyObject *
747 proxy_itervalues(proxyobject *pp)
749 return PyObject_CallMethod(pp->dict, "itervalues", NULL);
752 static PyObject *
753 proxy_iteritems(proxyobject *pp)
755 return PyObject_CallMethod(pp->dict, "iteritems", NULL);
757 static PyObject *
758 proxy_copy(proxyobject *pp)
760 return PyObject_CallMethod(pp->dict, "copy", NULL);
763 static PyMethodDef proxy_methods[] = {
764 {"has_key", (PyCFunction)proxy_has_key, METH_O,
765 PyDoc_STR("D.has_key(k) -> True if D has a key k, else False")},
766 {"get", (PyCFunction)proxy_get, METH_VARARGS,
767 PyDoc_STR("D.get(k[,d]) -> D[k] if D.has_key(k), else d."
768 " d defaults to None.")},
769 {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
770 PyDoc_STR("D.keys() -> list of D's keys")},
771 {"values", (PyCFunction)proxy_values, METH_NOARGS,
772 PyDoc_STR("D.values() -> list of D's values")},
773 {"items", (PyCFunction)proxy_items, METH_NOARGS,
774 PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
775 {"iterkeys", (PyCFunction)proxy_iterkeys, METH_NOARGS,
776 PyDoc_STR("D.iterkeys() -> an iterator over the keys of D")},
777 {"itervalues",(PyCFunction)proxy_itervalues, METH_NOARGS,
778 PyDoc_STR("D.itervalues() -> an iterator over the values of D")},
779 {"iteritems", (PyCFunction)proxy_iteritems, METH_NOARGS,
780 PyDoc_STR("D.iteritems() ->"
781 " an iterator over the (key, value) items of D")},
782 {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
783 PyDoc_STR("D.copy() -> a shallow copy of D")},
787 static void
788 proxy_dealloc(proxyobject *pp)
790 _PyObject_GC_UNTRACK(pp);
791 Py_DECREF(pp->dict);
792 PyObject_GC_Del(pp);
795 static PyObject *
796 proxy_getiter(proxyobject *pp)
798 return PyObject_GetIter(pp->dict);
801 static PyObject *
802 proxy_str(proxyobject *pp)
804 return PyObject_Str(pp->dict);
807 static int
808 proxy_traverse(PyObject *self, visitproc visit, void *arg)
810 proxyobject *pp = (proxyobject *)self;
811 Py_VISIT(pp->dict);
812 return 0;
815 static int
816 proxy_compare(proxyobject *v, PyObject *w)
818 return PyObject_Compare(v->dict, w);
821 static PyObject *
822 proxy_richcompare(proxyobject *v, PyObject *w, int op)
824 return PyObject_RichCompare(v->dict, w, op);
827 static PyTypeObject proxytype = {
828 PyObject_HEAD_INIT(&PyType_Type)
829 0, /* ob_size */
830 "dictproxy", /* tp_name */
831 sizeof(proxyobject), /* tp_basicsize */
832 0, /* tp_itemsize */
833 /* methods */
834 (destructor)proxy_dealloc, /* tp_dealloc */
835 0, /* tp_print */
836 0, /* tp_getattr */
837 0, /* tp_setattr */
838 (cmpfunc)proxy_compare, /* tp_compare */
839 0, /* tp_repr */
840 0, /* tp_as_number */
841 &proxy_as_sequence, /* tp_as_sequence */
842 &proxy_as_mapping, /* tp_as_mapping */
843 0, /* tp_hash */
844 0, /* tp_call */
845 (reprfunc)proxy_str, /* tp_str */
846 PyObject_GenericGetAttr, /* tp_getattro */
847 0, /* tp_setattro */
848 0, /* tp_as_buffer */
849 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
850 0, /* tp_doc */
851 proxy_traverse, /* tp_traverse */
852 0, /* tp_clear */
853 (richcmpfunc)proxy_richcompare, /* tp_richcompare */
854 0, /* tp_weaklistoffset */
855 (getiterfunc)proxy_getiter, /* tp_iter */
856 0, /* tp_iternext */
857 proxy_methods, /* tp_methods */
858 0, /* tp_members */
859 0, /* tp_getset */
860 0, /* tp_base */
861 0, /* tp_dict */
862 0, /* tp_descr_get */
863 0, /* tp_descr_set */
866 PyObject *
867 PyDictProxy_New(PyObject *dict)
869 proxyobject *pp;
871 pp = PyObject_GC_New(proxyobject, &proxytype);
872 if (pp != NULL) {
873 Py_INCREF(dict);
874 pp->dict = dict;
875 _PyObject_GC_TRACK(pp);
877 return (PyObject *)pp;
881 /* --- Wrapper object for "slot" methods --- */
883 /* This has no reason to be in this file except that adding new files is a
884 bit of a pain */
886 typedef struct {
887 PyObject_HEAD
888 PyWrapperDescrObject *descr;
889 PyObject *self;
890 } wrapperobject;
892 static void
893 wrapper_dealloc(wrapperobject *wp)
895 PyObject_GC_UnTrack(wp);
896 Py_TRASHCAN_SAFE_BEGIN(wp)
897 Py_XDECREF(wp->descr);
898 Py_XDECREF(wp->self);
899 PyObject_GC_Del(wp);
900 Py_TRASHCAN_SAFE_END(wp)
903 static int
904 wrapper_compare(wrapperobject *a, wrapperobject *b)
906 if (a->descr == b->descr)
907 return PyObject_Compare(a->self, b->self);
908 else
909 return (a->descr < b->descr) ? -1 : 1;
912 static long
913 wrapper_hash(wrapperobject *wp)
915 int x, y;
916 x = _Py_HashPointer(wp->descr);
917 if (x == -1)
918 return -1;
919 y = PyObject_Hash(wp->self);
920 if (y == -1)
921 return -1;
922 x = x ^ y;
923 if (x == -1)
924 x = -2;
925 return x;
928 static PyObject *
929 wrapper_repr(wrapperobject *wp)
931 return PyString_FromFormat("<method-wrapper '%s' of %s object at %p>",
932 wp->descr->d_base->name,
933 wp->self->ob_type->tp_name,
934 wp->self);
937 static PyMemberDef wrapper_members[] = {
938 {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
942 static PyObject *
943 wrapper_objclass(wrapperobject *wp)
945 PyObject *c = (PyObject *)wp->descr->d_type;
947 Py_INCREF(c);
948 return c;
951 static PyObject *
952 wrapper_name(wrapperobject *wp)
954 char *s = wp->descr->d_base->name;
956 return PyString_FromString(s);
959 static PyObject *
960 wrapper_doc(wrapperobject *wp)
962 char *s = wp->descr->d_base->doc;
964 if (s == NULL) {
965 Py_INCREF(Py_None);
966 return Py_None;
968 else {
969 return PyString_FromString(s);
973 static PyGetSetDef wrapper_getsets[] = {
974 {"__objclass__", (getter)wrapper_objclass},
975 {"__name__", (getter)wrapper_name},
976 {"__doc__", (getter)wrapper_doc},
980 static PyObject *
981 wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
983 wrapperfunc wrapper = wp->descr->d_base->wrapper;
984 PyObject *self = wp->self;
986 if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
987 wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
988 return (*wk)(self, args, wp->descr->d_wrapped, kwds);
991 if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
992 PyErr_Format(PyExc_TypeError,
993 "wrapper %s doesn't take keyword arguments",
994 wp->descr->d_base->name);
995 return NULL;
997 return (*wrapper)(self, args, wp->descr->d_wrapped);
1000 static int
1001 wrapper_traverse(PyObject *self, visitproc visit, void *arg)
1003 wrapperobject *wp = (wrapperobject *)self;
1004 Py_VISIT(wp->descr);
1005 Py_VISIT(wp->self);
1006 return 0;
1009 static PyTypeObject wrappertype = {
1010 PyObject_HEAD_INIT(&PyType_Type)
1011 0, /* ob_size */
1012 "method-wrapper", /* tp_name */
1013 sizeof(wrapperobject), /* tp_basicsize */
1014 0, /* tp_itemsize */
1015 /* methods */
1016 (destructor)wrapper_dealloc, /* tp_dealloc */
1017 0, /* tp_print */
1018 0, /* tp_getattr */
1019 0, /* tp_setattr */
1020 (cmpfunc)wrapper_compare, /* tp_compare */
1021 (reprfunc)wrapper_repr, /* tp_repr */
1022 0, /* tp_as_number */
1023 0, /* tp_as_sequence */
1024 0, /* tp_as_mapping */
1025 (hashfunc)wrapper_hash, /* tp_hash */
1026 (ternaryfunc)wrapper_call, /* tp_call */
1027 0, /* tp_str */
1028 PyObject_GenericGetAttr, /* tp_getattro */
1029 0, /* tp_setattro */
1030 0, /* tp_as_buffer */
1031 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1032 0, /* tp_doc */
1033 wrapper_traverse, /* tp_traverse */
1034 0, /* tp_clear */
1035 0, /* tp_richcompare */
1036 0, /* tp_weaklistoffset */
1037 0, /* tp_iter */
1038 0, /* tp_iternext */
1039 0, /* tp_methods */
1040 wrapper_members, /* tp_members */
1041 wrapper_getsets, /* tp_getset */
1042 0, /* tp_base */
1043 0, /* tp_dict */
1044 0, /* tp_descr_get */
1045 0, /* tp_descr_set */
1048 PyObject *
1049 PyWrapper_New(PyObject *d, PyObject *self)
1051 wrapperobject *wp;
1052 PyWrapperDescrObject *descr;
1054 assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
1055 descr = (PyWrapperDescrObject *)d;
1056 assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
1058 wp = PyObject_GC_New(wrapperobject, &wrappertype);
1059 if (wp != NULL) {
1060 Py_INCREF(descr);
1061 wp->descr = descr;
1062 Py_INCREF(self);
1063 wp->self = self;
1064 _PyObject_GC_TRACK(wp);
1066 return (PyObject *)wp;
1070 /* A built-in 'property' type */
1073 class property(object):
1075 def __init__(self, fget=None, fset=None, fdel=None, doc=None):
1076 if doc is None and fget is not None and hasattr(fget, "__doc__"):
1077 doc = fget.__doc__
1078 self.__get = fget
1079 self.__set = fset
1080 self.__del = fdel
1081 self.__doc__ = doc
1083 def __get__(self, inst, type=None):
1084 if inst is None:
1085 return self
1086 if self.__get is None:
1087 raise AttributeError, "unreadable attribute"
1088 return self.__get(inst)
1090 def __set__(self, inst, value):
1091 if self.__set is None:
1092 raise AttributeError, "can't set attribute"
1093 return self.__set(inst, value)
1095 def __delete__(self, inst):
1096 if self.__del is None:
1097 raise AttributeError, "can't delete attribute"
1098 return self.__del(inst)
1102 typedef struct {
1103 PyObject_HEAD
1104 PyObject *prop_get;
1105 PyObject *prop_set;
1106 PyObject *prop_del;
1107 PyObject *prop_doc;
1108 } propertyobject;
1110 static PyMemberDef property_members[] = {
1111 {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
1112 {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
1113 {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
1114 {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
1119 static void
1120 property_dealloc(PyObject *self)
1122 propertyobject *gs = (propertyobject *)self;
1124 _PyObject_GC_UNTRACK(self);
1125 Py_XDECREF(gs->prop_get);
1126 Py_XDECREF(gs->prop_set);
1127 Py_XDECREF(gs->prop_del);
1128 Py_XDECREF(gs->prop_doc);
1129 self->ob_type->tp_free(self);
1132 static PyObject *
1133 property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
1135 propertyobject *gs = (propertyobject *)self;
1137 if (obj == NULL || obj == Py_None) {
1138 Py_INCREF(self);
1139 return self;
1141 if (gs->prop_get == NULL) {
1142 PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
1143 return NULL;
1145 return PyObject_CallFunction(gs->prop_get, "(O)", obj);
1148 static int
1149 property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
1151 propertyobject *gs = (propertyobject *)self;
1152 PyObject *func, *res;
1154 if (value == NULL)
1155 func = gs->prop_del;
1156 else
1157 func = gs->prop_set;
1158 if (func == NULL) {
1159 PyErr_SetString(PyExc_AttributeError,
1160 value == NULL ?
1161 "can't delete attribute" :
1162 "can't set attribute");
1163 return -1;
1165 if (value == NULL)
1166 res = PyObject_CallFunction(func, "(O)", obj);
1167 else
1168 res = PyObject_CallFunction(func, "(OO)", obj, value);
1169 if (res == NULL)
1170 return -1;
1171 Py_DECREF(res);
1172 return 0;
1175 static int
1176 property_init(PyObject *self, PyObject *args, PyObject *kwds)
1178 PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
1179 PyObject *get_doc = NULL;
1180 static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
1181 propertyobject *gs = (propertyobject *)self;
1183 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
1184 kwlist, &get, &set, &del, &doc))
1185 return -1;
1187 if (get == Py_None)
1188 get = NULL;
1189 if (set == Py_None)
1190 set = NULL;
1191 if (del == Py_None)
1192 del = NULL;
1194 /* if no docstring given and the getter has one, use that one */
1195 if ((doc == NULL || doc == Py_None) && get != NULL &&
1196 PyObject_HasAttrString(get, "__doc__")) {
1197 if (!(get_doc = PyObject_GetAttrString(get, "__doc__")))
1198 return -1;
1199 Py_DECREF(get_doc); /* it is INCREF'd again below */
1200 doc = get_doc;
1203 Py_XINCREF(get);
1204 Py_XINCREF(set);
1205 Py_XINCREF(del);
1206 Py_XINCREF(doc);
1208 gs->prop_get = get;
1209 gs->prop_set = set;
1210 gs->prop_del = del;
1211 gs->prop_doc = doc;
1213 return 0;
1216 PyDoc_STRVAR(property_doc,
1217 "property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
1218 "\n"
1219 "fget is a function to be used for getting an attribute value, and likewise\n"
1220 "fset is a function for setting, and fdel a function for del'ing, an\n"
1221 "attribute. Typical use is to define a managed attribute x:\n"
1222 "class C(object):\n"
1223 " def getx(self): return self.__x\n"
1224 " def setx(self, value): self.__x = value\n"
1225 " def delx(self): del self.__x\n"
1226 " x = property(getx, setx, delx, \"I'm the 'x' property.\")");
1228 static int
1229 property_traverse(PyObject *self, visitproc visit, void *arg)
1231 propertyobject *pp = (propertyobject *)self;
1232 Py_VISIT(pp->prop_get);
1233 Py_VISIT(pp->prop_set);
1234 Py_VISIT(pp->prop_del);
1235 Py_VISIT(pp->prop_doc);
1236 return 0;
1239 PyTypeObject PyProperty_Type = {
1240 PyObject_HEAD_INIT(&PyType_Type)
1241 0, /* ob_size */
1242 "property", /* tp_name */
1243 sizeof(propertyobject), /* tp_basicsize */
1244 0, /* tp_itemsize */
1245 /* methods */
1246 property_dealloc, /* tp_dealloc */
1247 0, /* tp_print */
1248 0, /* tp_getattr */
1249 0, /* tp_setattr */
1250 0, /* tp_compare */
1251 0, /* tp_repr */
1252 0, /* tp_as_number */
1253 0, /* tp_as_sequence */
1254 0, /* tp_as_mapping */
1255 0, /* tp_hash */
1256 0, /* tp_call */
1257 0, /* tp_str */
1258 PyObject_GenericGetAttr, /* tp_getattro */
1259 0, /* tp_setattro */
1260 0, /* tp_as_buffer */
1261 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1262 Py_TPFLAGS_BASETYPE, /* tp_flags */
1263 property_doc, /* tp_doc */
1264 property_traverse, /* tp_traverse */
1265 0, /* tp_clear */
1266 0, /* tp_richcompare */
1267 0, /* tp_weaklistoffset */
1268 0, /* tp_iter */
1269 0, /* tp_iternext */
1270 0, /* tp_methods */
1271 property_members, /* tp_members */
1272 0, /* tp_getset */
1273 0, /* tp_base */
1274 0, /* tp_dict */
1275 property_descr_get, /* tp_descr_get */
1276 property_descr_set, /* tp_descr_set */
1277 0, /* tp_dictoffset */
1278 property_init, /* tp_init */
1279 PyType_GenericAlloc, /* tp_alloc */
1280 PyType_GenericNew, /* tp_new */
1281 PyObject_GC_Del, /* tp_free */