Code comprehensibility patch

A long time ago I wrote to Guido suggesting that I might like to submit some patches to improve the comprehensibility of the Python source. He was receptive, so my first one is attached (I will also post to the SF patch manager). I did this in an effort to stop replicating the effort of understanding how the new descriptor mechanism works on types. I'm also testing the waters a bit, here: I hope it looks like an improvement to people, but please let me know if it does not. Regards, Dave --- typeobject.c Mon Dec 17 12:14:22 2001 +++ typeobject.c.new Thu Mar 28 13:46:03 2002 @@ -1186,8 +1186,8 @@ type_getattro(PyTypeObject *type, PyObject *name) { PyTypeObject *metatype = type->ob_type; - PyObject *descr, *res; - descrgetfunc f; + PyObject *meta_attribute, *attribute; + descrgetfunc meta_get; /* Initialize this type (we'll assume the metatype is initialized) */ if (type->tp_dict == NULL) { @@ -1195,34 +1195,50 @@ return NULL; } - /* Get a descriptor from the metatype */ - descr = _PyType_Lookup(metatype, name); - f = NULL; - if (descr != NULL) { - f = descr->ob_type->tp_descr_get; - if (f != NULL && PyDescr_IsData(descr)) - return f(descr, - (PyObject *)type, (PyObject *)metatype); - } + /* No readable descriptor found yet */ + meta_get = NULL; + + /* Look for the attribute in the metatype */ + meta_attribute = _PyType_Lookup(metatype, name); - /* Look in tp_dict of this type and its bases */ - res = _PyType_Lookup(type, name); - if (res != NULL) { - f = res->ob_type->tp_descr_get; - if (f != NULL) - return f(res, (PyObject *)NULL, (PyObject *)type); - Py_INCREF(res); - return res; + if (meta_attribute != NULL) { + meta_get = meta_attribute->ob_type->tp_descr_get; + + if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { + /* Data descriptors implement tp_descr_set to intercept + * writes. Assume the attribute is not overridden in + * type's tp_dict (and bases): call the descriptor now. + */ + return meta_get(meta_attribute, + (PyObject *)type, (PyObject *)metatype); + } } - /* Use the descriptor from the metatype */ - if (f != NULL) { - res = f(descr, (PyObject *)type, (PyObject *)metatype); - return res; + /* No data descriptor found on metatype. Look in tp_dict of this + * type and its bases */ + attribute = _PyType_Lookup(type, name); + if (attribute != NULL) { + /* Implement descriptor functionality, if any */ + descrgetfunc local_get = attribute->ob_type->tp_descr_get; + if (local_get != NULL) { + /* NULL 2nd argument indicates the descriptor was found on + * the target object itself (or a base) */ + return local_get(attribute, (PyObject *)NULL, (PyObject *)type); + } + + Py_INCREF(attribute); + return attribute; } - if (descr != NULL) { - Py_INCREF(descr); - return descr; + + /* No attribute found in local __dict__ (or bases): use the + * descriptor from the metatype, if any */ + if (meta_get != NULL) + return meta_get(meta_attribute, (PyObject *)type, (PyObject *)metatype); + + /* If an ordinary attribute was found on the metatype, return it now. */ + if (meta_attribute != NULL) { + Py_INCREF(meta_attribute); + return meta_attribute; } /* Give up */ +---------------------------------------------------------------+ David Abrahams C++ Booster (http://www.boost.org) O__ == Pythonista (http://www.python.org) c/ /'_ == resume: http://users.rcn.com/abrahams/resume.html (*) \(*) == email: david.abrahams@rcn.com +---------------------------------------------------------------+

A long time ago I wrote to Guido suggesting that I might like to submit some patches to improve the comprehensibility of the Python source. ... I hope it looks like an improvement to people, but please let me know if it does not.
I assume there was something wrong with the patch as posted, as it looked like the new code used 1-space indents. It also looked like it was made against an out-of-date version of typeobject.c (please use current CVS as a base -- the newer type/class code is unusually volatile). Apart from those, go for it! It reminds me of something my first boss in the computer biz said. He looked at one of my checkins and remarked "Tim, you seemed to add comments to parts of the code you didn't understand!". The difference is that he was complaining <wink>.
/* Give up */
Indeed.

----- Original Message ----- From: "Tim Peters" <tim.one@comcast.net>
A long time ago I wrote to Guido suggesting that I might like to submit some patches to improve the comprehensibility of the Python source. ... I hope it looks like an improvement to people, but please let me know if it does not.
I assume there was something wrong with the patch as posted, as it looked like the new code used 1-space indents. It also looked like it was made against an out-of-date version of typeobject.c (please use current CVS as a base -- the newer type/class code is unusually volatile).
Fixed. Comments appreciated. The new code is appended for easy perusal -Dave ----- static PyObject * type_getattro(PyTypeObject *type, PyObject *name) { PyTypeObject *metatype = type->ob_type; PyObject *meta_attribute, *attribute; descrgetfunc meta_get; /* Initialize this type (we'll assume the metatype is initialized) */ if (type->tp_dict == NULL) { if (PyType_Ready(type) < 0) return NULL; } /* No readable descriptor found yet */ meta_get = NULL; /* Look for the attribute in the metatype */ meta_attribute = _PyType_Lookup(metatype, name); if (meta_attribute != NULL) { meta_get = meta_attribute->ob_type->tp_descr_get; if (meta_get != NULL && PyDescr_IsData(meta_attribute)) { /* Data descriptors implement tp_descr_set to intercept * writes. Assume the attribute is not overridden in * type's tp_dict (and bases): call the descriptor now. */ return meta_get(meta_attribute, (PyObject *)type, (PyObject *)metatype); } } /* No data descriptor found on metatype. Look in tp_dict of this * type and its bases */ attribute = _PyType_Lookup(type, name); if (attribute != NULL) { /* Implement descriptor functionality, if any */ descrgetfunc local_get = attribute->ob_type->tp_descr_get; if (local_get != NULL) { /* NULL 2nd argument indicates the descriptor was found on * the target object itself (or a base) */ return local_get(attribute, (PyObject *)NULL, (PyObject *)type); } Py_INCREF(attribute); return attribute; } /* No attribute found in local __dict__ (or bases): use the * descriptor from the metatype, if any */ if (meta_get != NULL) return meta_get(meta_attribute, (PyObject *)type, (PyObject *)metatype); /* If an ordinary attribute was found on the metatype, return it now. */ if (meta_attribute != NULL) { Py_INCREF(meta_attribute); return meta_attribute; } /* Give up */ PyErr_Format(PyExc_AttributeError, "type object '%.50s' has no attribute '%.400s'", type->tp_name, PyString_AS_STRING(name)); return NULL; }

[David Abrahams]
Fixed. Comments appreciated. The new code is appended for easy perusal
Something in your email chain replaces tab characters with spaces. You can see this yourself here: http://mail.python.org/pipermail/python-dev/2002-March/021924.html So please attach patches *as* attachments to the SourceForge report. SourceForge does its own kinds of whitespace destruction in description and comment fields. Attachments don't suffer. This will go really easy when you stop fighting the system <wink>.
participants (2)
-
David Abrahams
-
Tim Peters