1 /* Descriptors -- a new, flexible way to describe attributes */
4 #include "structmember.h" /* Why is this not included in Python.h? */
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
);
16 descr_name(PyDescrObject
*descr
)
18 if (descr
->d_name
!= NULL
&& PyString_Check(descr
->d_name
))
19 return PyString_AS_STRING(descr
->d_name
);
25 descr_repr(PyDescrObject
*descr
, char *format
)
27 return PyString_FromFormat(format
, descr_name(descr
),
28 descr
->d_type
->tp_name
);
32 method_repr(PyMethodDescrObject
*descr
)
34 return descr_repr((PyDescrObject
*)descr
,
35 "<method '%s' of '%s' objects>");
39 member_repr(PyMemberDescrObject
*descr
)
41 return descr_repr((PyDescrObject
*)descr
,
42 "<member '%s' of '%s' objects>");
46 getset_repr(PyGetSetDescrObject
*descr
)
48 return descr_repr((PyDescrObject
*)descr
,
49 "<attribute '%s' of '%s' objects>");
53 wrapperdescr_repr(PyWrapperDescrObject
*descr
)
55 return descr_repr((PyDescrObject
*)descr
,
56 "<slot wrapper '%s' of '%s' objects>");
60 descr_check(PyDescrObject
*descr
, PyObject
*obj
, PyObject
**pres
)
64 *pres
= (PyObject
*)descr
;
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
);
81 classmethod_get(PyMethodDescrObject
*descr
, PyObject
*obj
, PyObject
*type
)
83 /* Ensure a valid type. Class methods ignore obj. */
86 type
= (PyObject
*)obj
->ob_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
);
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
);
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
);
115 return PyCFunction_New(descr
->d_method
, type
);
119 method_get(PyMethodDescrObject
*descr
, PyObject
*obj
, PyObject
*type
)
123 if (descr_check((PyDescrObject
*)descr
, obj
, &res
))
125 return PyCFunction_New(descr
->d_method
, obj
);
129 member_get(PyMemberDescrObject
*descr
, PyObject
*obj
, PyObject
*type
)
133 if (descr_check((PyDescrObject
*)descr
, obj
, &res
))
135 return PyMember_GetOne((char *)obj
, descr
->d_member
);
139 getset_get(PyGetSetDescrObject
*descr
, PyObject
*obj
, PyObject
*type
)
143 if (descr_check((PyDescrObject
*)descr
, obj
, &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
);
155 wrapperdescr_get(PyWrapperDescrObject
*descr
, PyObject
*obj
, PyObject
*type
)
159 if (descr_check((PyDescrObject
*)descr
, obj
, &res
))
161 return PyWrapper_New((PyObject
*)descr
, obj
);
165 descr_setcheck(PyDescrObject
*descr
, PyObject
*obj
, PyObject
*value
,
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",
174 descr
->d_type
->tp_name
,
175 obj
->ob_type
->tp_name
);
183 member_set(PyMemberDescrObject
*descr
, PyObject
*obj
, PyObject
*value
)
187 if (descr_setcheck((PyDescrObject
*)descr
, obj
, value
, &res
))
189 return PyMember_SetOne((char *)obj
, descr
->d_member
, value
);
193 getset_set(PyGetSetDescrObject
*descr
, PyObject
*obj
, PyObject
*value
)
197 if (descr_setcheck((PyDescrObject
*)descr
, obj
, value
, &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
);
210 methoddescr_call(PyMethodDescrObject
*descr
, PyObject
*args
, PyObject
*kwds
)
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
);
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
);
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
);
238 func
= PyCFunction_New(descr
->d_method
, self
);
241 args
= PyTuple_GetSlice(args
, 1, argc
);
246 result
= PyEval_CallObjectWithKeywords(func
, args
, kwds
);
253 classmethoddescr_call(PyMethodDescrObject
*descr
, PyObject
*args
,
256 PyObject
*func
, *result
;
258 func
= PyCFunction_New(descr
->d_method
, (PyObject
*)descr
->d_type
);
262 result
= PyEval_CallObjectWithKeywords(func
, args
, kwds
);
268 wrapperdescr_call(PyWrapperDescrObject
*descr
, PyObject
*args
, PyObject
*kwds
)
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
);
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
);
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
);
296 func
= PyWrapper_New((PyObject
*)descr
, self
);
299 args
= PyTuple_GetSlice(args
, 1, argc
);
304 result
= PyEval_CallObjectWithKeywords(func
, args
, kwds
);
311 method_get_doc(PyMethodDescrObject
*descr
, void *closure
)
313 if (descr
->d_method
->ml_doc
== NULL
) {
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
},
332 member_get_doc(PyMemberDescrObject
*descr
, void *closure
)
334 if (descr
->d_member
->doc
== NULL
) {
338 return PyString_FromString(descr
->d_member
->doc
);
341 static PyGetSetDef member_getset
[] = {
342 {"__doc__", (getter
)member_get_doc
},
347 getset_get_doc(PyGetSetDescrObject
*descr
, void *closure
)
349 if (descr
->d_getset
->doc
== NULL
) {
353 return PyString_FromString(descr
->d_getset
->doc
);
356 static PyGetSetDef getset_getset
[] = {
357 {"__doc__", (getter
)getset_get_doc
},
362 wrapperdescr_get_doc(PyWrapperDescrObject
*descr
, void *closure
)
364 if (descr
->d_base
->doc
== NULL
) {
368 return PyString_FromString(descr
->d_base
->doc
);
371 static PyGetSetDef wrapperdescr_getset
[] = {
372 {"__doc__", (getter
)wrapperdescr_get_doc
},
377 descr_traverse(PyObject
*self
, visitproc visit
, void *arg
)
379 PyDescrObject
*descr
= (PyDescrObject
*)self
;
380 Py_VISIT(descr
->d_type
);
384 static PyTypeObject PyMethodDescr_Type
= {
385 PyObject_HEAD_INIT(&PyType_Type
)
388 sizeof(PyMethodDescrObject
),
390 (destructor
)descr_dealloc
, /* tp_dealloc */
395 (reprfunc
)method_repr
, /* tp_repr */
396 0, /* tp_as_number */
397 0, /* tp_as_sequence */
398 0, /* tp_as_mapping */
400 (ternaryfunc
)methoddescr_call
, /* tp_call */
402 PyObject_GenericGetAttr
, /* tp_getattro */
404 0, /* tp_as_buffer */
405 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
407 descr_traverse
, /* tp_traverse */
409 0, /* tp_richcompare */
410 0, /* tp_weaklistoffset */
414 descr_members
, /* tp_members */
415 method_getset
, /* tp_getset */
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 */
434 (reprfunc
)method_repr
, /* tp_repr */
435 0, /* tp_as_number */
436 0, /* tp_as_sequence */
437 0, /* tp_as_mapping */
439 (ternaryfunc
)classmethoddescr_call
, /* tp_call */
441 PyObject_GenericGetAttr
, /* tp_getattro */
443 0, /* tp_as_buffer */
444 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
446 descr_traverse
, /* tp_traverse */
448 0, /* tp_richcompare */
449 0, /* tp_weaklistoffset */
453 descr_members
, /* tp_members */
454 method_getset
, /* tp_getset */
457 (descrgetfunc
)classmethod_get
, /* tp_descr_get */
458 0, /* tp_descr_set */
461 static PyTypeObject PyMemberDescr_Type
= {
462 PyObject_HEAD_INIT(&PyType_Type
)
465 sizeof(PyMemberDescrObject
),
467 (destructor
)descr_dealloc
, /* tp_dealloc */
472 (reprfunc
)member_repr
, /* tp_repr */
473 0, /* tp_as_number */
474 0, /* tp_as_sequence */
475 0, /* tp_as_mapping */
479 PyObject_GenericGetAttr
, /* tp_getattro */
481 0, /* tp_as_buffer */
482 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
484 descr_traverse
, /* tp_traverse */
486 0, /* tp_richcompare */
487 0, /* tp_weaklistoffset */
491 descr_members
, /* tp_members */
492 member_getset
, /* tp_getset */
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
)
503 sizeof(PyGetSetDescrObject
),
505 (destructor
)descr_dealloc
, /* tp_dealloc */
510 (reprfunc
)getset_repr
, /* tp_repr */
511 0, /* tp_as_number */
512 0, /* tp_as_sequence */
513 0, /* tp_as_mapping */
517 PyObject_GenericGetAttr
, /* tp_getattro */
519 0, /* tp_as_buffer */
520 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
522 descr_traverse
, /* tp_traverse */
524 0, /* tp_richcompare */
525 0, /* tp_weaklistoffset */
529 descr_members
, /* tp_members */
530 getset_getset
, /* tp_getset */
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 */
548 (reprfunc
)wrapperdescr_repr
, /* tp_repr */
549 0, /* tp_as_number */
550 0, /* tp_as_sequence */
551 0, /* tp_as_mapping */
553 (ternaryfunc
)wrapperdescr_call
, /* tp_call */
555 PyObject_GenericGetAttr
, /* tp_getattro */
557 0, /* tp_as_buffer */
558 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
560 descr_traverse
, /* tp_traverse */
562 0, /* tp_richcompare */
563 0, /* tp_weaklistoffset */
567 descr_members
, /* tp_members */
568 wrapperdescr_getset
, /* tp_getset */
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);
583 descr
->d_type
= type
;
584 descr
->d_name
= PyString_InternFromString(name
);
585 if (descr
->d_name
== NULL
) {
594 PyDescr_NewMethod(PyTypeObject
*type
, PyMethodDef
*method
)
596 PyMethodDescrObject
*descr
;
598 descr
= (PyMethodDescrObject
*)descr_new(&PyMethodDescr_Type
,
599 type
, method
->ml_name
);
601 descr
->d_method
= method
;
602 return (PyObject
*)descr
;
606 PyDescr_NewClassMethod(PyTypeObject
*type
, PyMethodDef
*method
)
608 PyMethodDescrObject
*descr
;
610 descr
= (PyMethodDescrObject
*)descr_new(&PyClassMethodDescr_Type
,
611 type
, method
->ml_name
);
613 descr
->d_method
= method
;
614 return (PyObject
*)descr
;
618 PyDescr_NewMember(PyTypeObject
*type
, PyMemberDef
*member
)
620 PyMemberDescrObject
*descr
;
622 descr
= (PyMemberDescrObject
*)descr_new(&PyMemberDescr_Type
,
625 descr
->d_member
= member
;
626 return (PyObject
*)descr
;
630 PyDescr_NewGetSet(PyTypeObject
*type
, PyGetSetDef
*getset
)
632 PyGetSetDescrObject
*descr
;
634 descr
= (PyGetSetDescrObject
*)descr_new(&PyGetSetDescr_Type
,
637 descr
->d_getset
= getset
;
638 return (PyObject
*)descr
;
642 PyDescr_NewWrapper(PyTypeObject
*type
, struct wrapperbase
*base
, void *wrapped
)
644 PyWrapperDescrObject
*descr
;
646 descr
= (PyWrapperDescrObject
*)descr_new(&PyWrapperDescr_Type
,
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
667 proxy_len(proxyobject
*pp
)
669 return PyObject_Size(pp
->dict
);
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 */
685 proxy_contains(proxyobject
*pp
, PyObject
*key
)
687 return PyDict_Contains(pp
->dict
, key
);
690 static PySequenceMethods proxy_as_sequence
= {
697 0, /* sq_ass_slice */
698 (objobjproc
)proxy_contains
, /* sq_contains */
699 0, /* sq_inplace_concat */
700 0, /* sq_inplace_repeat */
704 proxy_has_key(proxyobject
*pp
, PyObject
*key
)
706 int res
= PyDict_Contains(pp
->dict
, key
);
709 return PyBool_FromLong(res
);
713 proxy_get(proxyobject
*pp
, PyObject
*args
)
715 PyObject
*key
, *def
= Py_None
;
717 if (!PyArg_UnpackTuple(args
, "get", 1, 2, &key
, &def
))
719 return PyObject_CallMethod(pp
->dict
, "get", "(OO)", key
, def
);
723 proxy_keys(proxyobject
*pp
)
725 return PyMapping_Keys(pp
->dict
);
729 proxy_values(proxyobject
*pp
)
731 return PyMapping_Values(pp
->dict
);
735 proxy_items(proxyobject
*pp
)
737 return PyMapping_Items(pp
->dict
);
741 proxy_iterkeys(proxyobject
*pp
)
743 return PyObject_CallMethod(pp
->dict
, "iterkeys", NULL
);
747 proxy_itervalues(proxyobject
*pp
)
749 return PyObject_CallMethod(pp
->dict
, "itervalues", NULL
);
753 proxy_iteritems(proxyobject
*pp
)
755 return PyObject_CallMethod(pp
->dict
, "iteritems", NULL
);
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")},
788 proxy_dealloc(proxyobject
*pp
)
790 _PyObject_GC_UNTRACK(pp
);
796 proxy_getiter(proxyobject
*pp
)
798 return PyObject_GetIter(pp
->dict
);
802 proxy_str(proxyobject
*pp
)
804 return PyObject_Str(pp
->dict
);
808 proxy_traverse(PyObject
*self
, visitproc visit
, void *arg
)
810 proxyobject
*pp
= (proxyobject
*)self
;
816 proxy_compare(proxyobject
*v
, PyObject
*w
)
818 return PyObject_Compare(v
->dict
, w
);
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
)
830 "dictproxy", /* tp_name */
831 sizeof(proxyobject
), /* tp_basicsize */
834 (destructor
)proxy_dealloc
, /* tp_dealloc */
838 (cmpfunc
)proxy_compare
, /* tp_compare */
840 0, /* tp_as_number */
841 &proxy_as_sequence
, /* tp_as_sequence */
842 &proxy_as_mapping
, /* tp_as_mapping */
845 (reprfunc
)proxy_str
, /* tp_str */
846 PyObject_GenericGetAttr
, /* tp_getattro */
848 0, /* tp_as_buffer */
849 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
851 proxy_traverse
, /* tp_traverse */
853 (richcmpfunc
)proxy_richcompare
, /* tp_richcompare */
854 0, /* tp_weaklistoffset */
855 (getiterfunc
)proxy_getiter
, /* tp_iter */
857 proxy_methods
, /* tp_methods */
862 0, /* tp_descr_get */
863 0, /* tp_descr_set */
867 PyDictProxy_New(PyObject
*dict
)
871 pp
= PyObject_GC_New(proxyobject
, &proxytype
);
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
888 PyWrapperDescrObject
*descr
;
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
);
900 Py_TRASHCAN_SAFE_END(wp
)
904 wrapper_compare(wrapperobject
*a
, wrapperobject
*b
)
906 if (a
->descr
== b
->descr
)
907 return PyObject_Compare(a
->self
, b
->self
);
909 return (a
->descr
< b
->descr
) ? -1 : 1;
913 wrapper_hash(wrapperobject
*wp
)
916 x
= _Py_HashPointer(wp
->descr
);
919 y
= PyObject_Hash(wp
->self
);
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
,
937 static PyMemberDef wrapper_members
[] = {
938 {"__self__", T_OBJECT
, offsetof(wrapperobject
, self
), READONLY
},
943 wrapper_objclass(wrapperobject
*wp
)
945 PyObject
*c
= (PyObject
*)wp
->descr
->d_type
;
952 wrapper_name(wrapperobject
*wp
)
954 char *s
= wp
->descr
->d_base
->name
;
956 return PyString_FromString(s
);
960 wrapper_doc(wrapperobject
*wp
)
962 char *s
= wp
->descr
->d_base
->doc
;
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
},
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
);
997 return (*wrapper
)(self
, args
, wp
->descr
->d_wrapped
);
1001 wrapper_traverse(PyObject
*self
, visitproc visit
, void *arg
)
1003 wrapperobject
*wp
= (wrapperobject
*)self
;
1004 Py_VISIT(wp
->descr
);
1009 static PyTypeObject wrappertype
= {
1010 PyObject_HEAD_INIT(&PyType_Type
)
1012 "method-wrapper", /* tp_name */
1013 sizeof(wrapperobject
), /* tp_basicsize */
1014 0, /* tp_itemsize */
1016 (destructor
)wrapper_dealloc
, /* tp_dealloc */
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 */
1028 PyObject_GenericGetAttr
, /* tp_getattro */
1029 0, /* tp_setattro */
1030 0, /* tp_as_buffer */
1031 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
, /* tp_flags */
1033 wrapper_traverse
, /* tp_traverse */
1035 0, /* tp_richcompare */
1036 0, /* tp_weaklistoffset */
1038 0, /* tp_iternext */
1040 wrapper_members
, /* tp_members */
1041 wrapper_getsets
, /* tp_getset */
1044 0, /* tp_descr_get */
1045 0, /* tp_descr_set */
1049 PyWrapper_New(PyObject
*d
, PyObject
*self
)
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
);
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__"):
1083 def __get__(self, inst, type=None):
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)
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
},
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
);
1133 property_descr_get(PyObject
*self
, PyObject
*obj
, PyObject
*type
)
1135 propertyobject
*gs
= (propertyobject
*)self
;
1137 if (obj
== NULL
|| obj
== Py_None
) {
1141 if (gs
->prop_get
== NULL
) {
1142 PyErr_SetString(PyExc_AttributeError
, "unreadable attribute");
1145 return PyObject_CallFunction(gs
->prop_get
, "(O)", obj
);
1149 property_descr_set(PyObject
*self
, PyObject
*obj
, PyObject
*value
)
1151 propertyobject
*gs
= (propertyobject
*)self
;
1152 PyObject
*func
, *res
;
1155 func
= gs
->prop_del
;
1157 func
= gs
->prop_set
;
1159 PyErr_SetString(PyExc_AttributeError
,
1161 "can't delete attribute" :
1162 "can't set attribute");
1166 res
= PyObject_CallFunction(func
, "(O)", obj
);
1168 res
= PyObject_CallFunction(func
, "(OO)", obj
, value
);
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
))
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__")))
1199 Py_DECREF(get_doc
); /* it is INCREF'd again below */
1216 PyDoc_STRVAR(property_doc
,
1217 "property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\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.\")");
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
);
1239 PyTypeObject PyProperty_Type
= {
1240 PyObject_HEAD_INIT(&PyType_Type
)
1242 "property", /* tp_name */
1243 sizeof(propertyobject
), /* tp_basicsize */
1244 0, /* tp_itemsize */
1246 property_dealloc
, /* tp_dealloc */
1252 0, /* tp_as_number */
1253 0, /* tp_as_sequence */
1254 0, /* tp_as_mapping */
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 */
1266 0, /* tp_richcompare */
1267 0, /* tp_weaklistoffset */
1269 0, /* tp_iternext */
1271 property_members
, /* tp_members */
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 */