[Python-Dev] Arbitrary attributes on funcs and methods
bwarsaw@python.org
bwarsaw@python.org
Mon, 10 Apr 2000 19:48:51 -0400 (EDT)
> Below is a very raw set of patches to add an attribute dictionary to
> funcs and methods. It's only been minimally tested, but if y'all like
> the idea,
>>>>> "GS" == Greg Stein <gstein@lyra.org> writes:
GS> +1 on concept, -1 on the patch :-)
Well, that's good, because I /knew/ the patch was a quick hack (which
is why I posted it to python-dev and not patches :). Since there's
been generally positive feedback on the idea, I think I'll flesh it
out a bit.
GS> And note that the getattro/setattro is preferred. It is easy
GS> to extract the char* from them; the other direction requires
GS> construction of an object.
Good point.
>...
> + rtn = PyMember_Get((char *)im, instancemethod_memberlist, name);
> + if (rtn == NULL) {
> + PyErr_Clear();
> + rtn = PyObject_GetAttrString(im->im_func, name);
> + if (rtn == NULL)
> + PyErr_SetString(PyExc_AttributeError, name);
GS> Why do you mask this second error with the AttributeError?
GS> Seems that you should just leave whatever is there (typically
GS> an AttributeError, but maybe not!).
Good point here, but...
> + rtn = PyMember_Get((char *)op, func_memberlist, name);
> + if (rtn == NULL) {
> + PyErr_Clear();
> + rtn = PyDict_GetItemString(op->func_dict, name);
> + if (rtn == NULL)
> + PyErr_SetString(PyExc_AttributeError, name);
GS> Again, with the masking...
...here I don't want the KeyError to leak through the getattr() call.
If you do "print func.non_existent_attr" wouldn't you want an
AttributeError instead of a KeyError? Maybe it should explicitly test
for KeyError rather than masking any error coming back from
PyDict_GetItemString()? Or better yet (based on your suggestion
below), it should do a PyMapping_HasKey() test, raise an
AttributeError if not, then just return PyMapping_GetItemString().
>...
> + else if (strcmp(name, "func_dict") == 0) {
> + if (value == NULL || !PyDict_Check(value)) {
> + PyErr_SetString(
> + PyExc_TypeError,
> + "func_dict must be set to a dict object");
GS> This raises an interesting thought. Why not just require the
GS> mapping protocol?
Good point again.
-Barry