[Python-Dev] Code comprehensibility patch (original) (raw)

David Abrahams David Abrahams" <david.abrahams@rcn.com
Fri, 29 Mar 2002 16:24:41 -0500


----- 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

Py_INCREF(attribute);
return attribute;

}

/* No attribute found in local dict (or bases): use the

/* 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; }