Movatterモバイル変換
[0]ホーム
[Python-Dev] Arbitrary attributes on funcs and methods
bwarsaw@python.orgbwarsaw@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 (whichis why I posted it to python-dev and not patches :). Since there'sbeen generally positive feedback on the idea, I think I'll flesh itout 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 anAttributeError instead of a KeyError? Maybe it should explicitly testfor KeyError rather than masking any error coming back fromPyDict_GetItemString()? Or better yet (based on your suggestionbelow), it should do a PyMapping_HasKey() test, raise anAttributeError 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
[8]ページ先頭