Subclassing ndarray in C: getitem ?

All, I'm teaching myself how to subclass ndarrays in C (not in Cython, just plain C). It's slowly coming together, but I'm now running into a problem: I need to overwrite __getitem__ and I'm not sure how to do it. I was thinking about using my own function instead of array_as_mapping.mp_subscript, but how do I import array_as_mapping in the first place ? And is it really the way to go ? Any pointer appreciated... P.

On Jul 26, 2010, at 11:58 AM, Pierre GM wrote:
All, I'm teaching myself how to subclass ndarrays in C (not in Cython, just plain C). It's slowly coming together, but I'm now running into a problem: I need to overwrite __getitem__ and I'm not sure how to do it. I was thinking about using my own function instead of array_as_mapping.mp_subscript, but how do I import array_as_mapping in the first place ? And is it really the way to go ? Any pointer appreciated...
To subtype in C, you have to populate the function pointer tables. To "over-ride" __getitem__ you have to replace the function pointer, which in the case of mp_subscript is in the array_as_mapping table --- which you have to create as a static pointer table that contains the functions you want and then point the TypeObject structure to it some-time during initialization.
I hope that helps a little bit.
-Travis

On Jul 28, 2010, at 5:52 PM, Travis Oliphant wrote:
On Jul 26, 2010, at 11:58 AM, Pierre GM wrote:
All, I'm teaching myself how to subclass ndarrays in C (not in Cython, just plain C). It's slowly coming together, but I'm now running into a problem: I need to overwrite __getitem__ and I'm not sure how to do it. I was thinking about using my own function instead of array_as_mapping.mp_subscript, but how do I import array_as_mapping in the first place ? And is it really the way to go ? Any pointer appreciated...
To subtype in C, you have to populate the function pointer tables. To "over-ride" __getitem__ you have to replace the function pointer, which in the case of mp_subscript is in the array_as_mapping table --- which you have to create as a static pointer table that contains the functions you want and then point the TypeObject structure to it some-time during initialization.
I hope that helps a little bit.
Mmh. I did create a PyMappingMethod structure called MyArray_as_mapping, and MyArray_as_mapping.mp_subscript points to the function that I want to use. However, I'd like the MyArray_as_mapping.length and MyArray.mp_ass_subscript to point to their PyArray equivalent. I was hoping to do something like MyArray_as_mapping.length = array_as_mapping.length but I don't know how to import array_as_mapping. I also tried to do MyArray_as_mapping.length = &PyArray_Type->tp_as_mapping->length but it doesn't work... And that's where I'm stuck...

Wed, 28 Jul 2010 18:43:30 -0400, Pierre GM wrote: [clip]
Mmh. I did create a PyMappingMethod structure called MyArray_as_mapping, and MyArray_as_mapping.mp_subscript points to the function that I want to use. However, I'd like the MyArray_as_mapping.length and MyArray.mp_ass_subscript to point to their PyArray equivalent. I was hoping to do something like MyArray_as_mapping.length = array_as_mapping.length but I don't know how to import array_as_mapping.
Maybe you can leave them NULL? I don't remember if they are inherited.
I also tried to do MyArray_as_mapping.length = &PyArray_Type->tp_as_mapping->length but it doesn't work... And that's where I'm stuck...
How it fails?
Note that you probably need to call import_array() first before assigning the function pointers.
Pauli

On Jul 28, 2010, at 7:52 PM, Pauli Virtanen wrote:
Wed, 28 Jul 2010 18:43:30 -0400, Pierre GM wrote: [clip]
Mmh. I did create a PyMappingMethod structure called MyArray_as_mapping, and MyArray_as_mapping.mp_subscript points to the function that I want to use. However, I'd like the MyArray_as_mapping.length and MyArray.mp_ass_subscript to point to their PyArray equivalent. I was hoping to do something like MyArray_as_mapping.length = array_as_mapping.length but I don't know how to import array_as_mapping.
Maybe you can leave them NULL? I don't remember if they are inherited.
I gonna try that, good idea.
I also tried to do MyArray_as_mapping.length = &PyArray_Type->tp_as_mapping->length but it doesn't work... And that's where I'm stuck...
How it fails?
error: initializer element is not constant
Note that you probably need to call import_array() first before assigning the function pointers.
Mmh, Not sure whether it's gonna be possible. The import_array() is in an import function called in yet another file... But thx for the pointer, I'll keep you posted
[a bit later...] Well, setting MyArray_as_mapping.length to NULL does the trick indeed. Cool !!!
participants (3)
-
Pauli Virtanen
-
Pierre GM
-
Travis Oliphant