@@ -1074,8 +1074,8 @@ sense for the implementation's convenience. ::
10741074 getattrfunc tp_getattr; /* char * version */
10751075 setattrfunc tp_setattr;
10761076 /* ... */
1077- getattrofunc tp_getattrofunc; /* PyObject * version */
1078- setattrofunc tp_setattrofunc ;
1077+ getattrofunc tp_getattro; /* PyObject * version */
1078+ setattrofunc tp_setattro ;
10791079
10801080If accessing attributes of an object is always a simple operation (this will be
10811081explained shortly), there are generic implementations which can be used to
@@ -1204,37 +1204,28 @@ For simplicity, only the :ctype:`char\*` version will be demonstrated here; the
12041204type of the name parameter is the only difference between the :ctype: `char\* `
12051205and :ctype: `PyObject\* ` flavors of the interface. This example effectively does
12061206the same thing as the generic example above, but does not use the generic
1207- support added in Python 2.2. The value in showing this is two-fold: it
1208- demonstrates how basic attribute management can be done in a way that is
1209- portable to older versions of Python, and explains how the handler functions are
1207+ support added in Python 2.2. It explains how the handler functions are
12101208called, so that if you do need to extend their functionality, you'll understand
12111209what needs to be done.
12121210
12131211The :attr: `tp_getattr ` handler is called when the object requires an attribute
12141212look-up. It is called in the same situations where the :meth: `__getattr__ `
12151213method of a class would be called.
12161214
1217- A likely way to handle this is (1) to implement a set of functions (such as
1218- :cfunc: `newdatatype_getSize ` and :cfunc: `newdatatype_setSize ` in the example
1219- below), (2) provide a method table listing these functions, and (3) provide a
1220- getattr function that returns the result of a lookup in that table. The method
1221- table uses the same structure as the :attr: `tp_methods ` field of the type
1222- object.
1223-
12241215Here is an example::
12251216
1226- static PyMethodDef newdatatype_methods[] = {
1227- {"getSize", (PyCFunction)newdatatype_getSize, METH_VARARGS,
1228- "Return the current size."},
1229- {"setSize", (PyCFunction)newdatatype_setSize, METH_VARARGS,
1230- "Set the size."},
1231- {NULL, NULL, 0, NULL} /* sentinel */
1232- };
1233-
12341217 static PyObject *
12351218 newdatatype_getattr(newdatatypeobject *obj, char *name)
12361219 {
1237- return Py_FindMethod(newdatatype_methods, (PyObject *)obj, name);
1220+ if (strcmp(name, "data") == 0)
1221+ {
1222+ return PyInt_FromLong(obj->data);
1223+ }
1224+
1225+ PyErr_Format(PyExc_AttributeError,
1226+ "'%.50s' object has no attribute '%.400s'",
1227+ tp->tp_name, name);
1228+ return NULL;
12381229 }
12391230
12401231The :attr: `tp_setattr ` handler is called when the :meth: `__setattr__ ` or
0 commit comments