[ctypes-commit] ctypes/source cfield.c,1.80,1.81
Brought to you by:
theller
|
From: Thomas H. <th...@us...> - 2005-03-23 20:26:05
|
Update of /cvsroot/ctypes/ctypes/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv23557 Modified Files: cfield.c Log Message: It is now (in principle) possible to customize getting structure fields by implementing a __from_field__ classmethod on the structure field type. CField_FromDesc() checks if a __from_field__ attribute exists on the type, and if so installs _overloaded_field_getfunc() in the CFieldObject's getfunc slot. The __from__field__ method receives an int/long representing the field address. The source changes are #ifdef'd out currently. Index: cfield.c =================================================================== RCS file: /cvsroot/ctypes/ctypes/source/cfield.c,v retrieving revision 1.80 retrieving revision 1.81 diff -C2 -d -r1.80 -r1.81 *** cfield.c 23 Mar 2005 18:38:14 -0000 1.80 --- cfield.c 23 Mar 2005 20:25:50 -0000 1.81 *************** *** 32,35 **** --- 32,55 ---- } + #ifdef EXPERIMENTAL + /* getfunc overloaded by a __from_field__ callable on the fieldtype */ + static PyObject * + _overloaded_field_getfunc(void *ptr, unsigned size, + PyObject *fieldtype, CDataObject *src, + int index) + { + PyObject *result; + PyObject *arg; + PyObject *func = PyObject_GetAttrString(fieldtype, "__from_field__"); + if (func == NULL) + return NULL; + result = PyObject_CallFunctionObjArgs(func, + PyLong_FromVoidPtr(ptr), NULL); + Py_DECREF(func); + return result; + } + #endif + + /* * Expects the size, index and offset for the current field in *psize and *************** *** 107,111 **** --- 127,138 ---- length = dict->length; + #ifdef EXPERIMENTAL + if (PyObject_HasAttrString(desc, "__from_field__")) + getfunc = _overloaded_field_getfunc; + else + getfunc = dict->getfunc ? dict->getfunc : _generic_field_getfunc; + #else getfunc = dict->getfunc ? dict->getfunc : _generic_field_getfunc; + #endif /* Currently, dict->getfunc is only != NULL for SimpleCData types. |