Travis Oliphant schrieb:
r_field = PyDict_GetItemString(dtype,'r');
Actually it should read PyDict_GetItemString(dtype->fields). The r_field is a tuple (data-type object, offset). The fields attribute is (currently) a Python dictionary.
Ok. This seems to be missing in the PEP. The section titled "Attributes" seems to talk about Python-level attributes. Apparently, you are suggesting that there is also a C-level API, lower than PyObject_GetAttrString, so that you can write dtype->fields, instead of having to write PyObject_GetAttrString(dtype, "fields").
If it is indeed the intend that this kind of acccess is available for datatype objects, then the PEP should specify it. Notice that it would be uncommon for a type in Python: Most types have getter functions (such as PyComplex_RealAsDouble, rather then specifying direct access through obj->cval.real).
Going now back to your original code (and assuming proper adjustments):
dtype = img->descr; r_field = PyDict_GetItemString(dtype,'r'); g_field = PyDict_GetItemString(dtype,'g'); r_field_dtype = PyTuple_GET_ITEM(r_field, 0); r_field_offset = PyTuple_GET_ITEM(r_field, 1); g_field_dtype = PyTuple_GET_ITEM(g_field, 0); g_field_offset = PyTuple_GET_ITEM(g_field, 1); obj = PyArray_GetField(img, g_field, g_field_offset); Py_INCREF(r_field) PyArray_SetField(img, r_field, r_field_offset, obj);
In this code, where is PyArray_GetField coming from? What does it do? If I wanted to write this code from scratch, what should I write instead? Since this is all about a flat memory block, I'm surprised I need "true" Python objects for the field values in there.
But, the other option (especially for code already written) would be to just convert the data-format specification into it's own internal representation.
Ok, so your assumption is that consumers already have their own machinery, in which case ease-of-use would be the question how difficult it is to convert datatype objects into the internal representation.