Todd Miller wrote:
Chris Barker wrote:
How can I check if an Object is a NumPy array (and then use it as such), without including Numeric during compilation?
I know one option is to have condition compilation, with a NumPy and non-Numpy version, but Robin is managing a whole lot of different version as it is, and I don't think he wants to deal with twice as many!
Anyone have any ideas?
Use the Python C-API and string literals as the basis for the interface. I think the steps are something like this:
1. Import "Numeric". (PyImport_ImportModule)
2. Get the module dictionary. (PyModule_GetDict)
3. Get "array" out of the dictionary. (PyDict_GetItemString)
4. Call "isinstance" on Numeric.array and the object. (PyObject_IsInstance)
Similarly:
1. Import "numarray".
2. Get the module dictionary.
3. Get "NumArray" out of the dictionary
4. Call the C-API equivalent of "isinstance" on numarray.NumArray and the object.
The first 3 steps of both cases can be initialized once, I think, and stored in C static variables to avoid repeated fetches.
On second thought, just do two functions, one for Numeric, one for numarray. If any of the first 3 steps fail, return False. Otherwise, return the result of the isinstance call.
If it's not a Numeric array, check to see if it's a numarray.
My idea to couple these was "not good". They're not compatible at that level anyway. Since numarray and Numeric are only source level compatible, C-code can be compiled to work with one or the other, but not both at the same time. It probably makes more sense to just implement for Numeric. If you do want to implement for both, treat them as seperate cases with seperate recognizer functions and element access code. But... It's not clear to me that knowing an object is an array will help since getting data elements still has to be done fast, and that seems hard to do without knowing the arrayobject struct. Keep in mind that Numeric and numarray arrays are strided and possibly discontiguous, so there's more to data access than owning a base pointer, as would be the case in C. Todd