[C++-sig] extracting a numpy.int32 type

Ger van Diepen diepen at astron.nl
Fri Dec 11 07:43:05 CET 2009


We noticed things went fine as long as the sizes of the types at the Python and C++ side matched.
On a 32bit system the behaviour differs from a 64bit machine. Because it fails for you, I guess you are working on a 64bit machine. It will be fine on a 32bit system.
You'll also notice it always fail if you you a short at the C++ side.

Here you have the answer I got from Andreas Klockner some time ago.

Cheers,
Ger

On Donnerstag 17 April 2008, Ger van Diepen wrote:
>   ArgumentError: Python argument types in
>     Table.getcell(table, str, numpy.int32)
                                ^^^^^^
>   did not match C++ signature:
>     getcell(casa::TableProxy {lvalue}, std::string columnname, int rownr)

This is numpy's fault, as it returns what they term an 'array scalar'. This is 
because Python's "int" on a 64-bit machine is 64 bits:

>>> 2**39
549755813888
>>> 2**66
73786976294838206464L

(see when the "L" shows up)

But numpy wants to return the exact data type you have in your array, so it 
gives you something that's not an int. As BP doesn't know about numpy by 
default (and can't assume it's there), there's no support. You would need to 
install custom (from-/to-python) rvalue converters for numpy's array scalars. 
I have something in the works that might help you--watch for an announcement 
in the next few days. But for now the explicit conversion hack is probably 
your easiest route to success.

Andreas


>>> On Thu, Dec 10, 2009 at  4:17 PM, in message
<20091210151703.GB22600 at SamZwo.urz.uni-heidelberg.de>, Nathan Huesken
<boost-python at lonely-star.org> wrote: 
> Hi,
> 
> Thanks for the reply.
> 
> But still something, I would like to understand:
> 
> If I want to convert numpy.float64 -> double, it works without problems.
> If I want to convert numpy.int32 -> int, I seem to need to register a
> new converte ...
> 
> Why is this?
> 
> Thanks!
> Nathan
> 
> On Wed, Dec 09, 2009 at 04:29:19PM +0100, Ger van Diepen wrote:
>> Hi Nathan,
>> 
>> We have converters written at the end of 2006. We never needed to convert 
> them to the more powerful newer versions of Boost-Python as they still build 
> fine. So they might look outdated.
>> 
>> They can convert scalars, sequences, dicts, and numpy arrays of many types, 
> also all numeric numpy types. So it can convert, say, an element of a numpy 
> int32 array to a C++ float scalar. Vice-versa a C++ numeric value is always 
> converted to a regular Python numeric type.
>> They work well on both 32bit and 64bit platforms.
>> 
>> If you want to see more you can find the code at pyrap.googlecode.com.
>> 
>> Cheers,
>> Ger
>> 
>> 
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig



More information about the Cplusplus-sig mailing list