[C++-sig] Can't convert to unsigned int

Langston, Matthew David langston at SLAC.Stanford.EDU
Wed Dec 11 19:26:07 CET 2002


I just ran into this same problem last night.

In High Energy Physics we are always using unsigned longs as event bit
masks. It would be nice if Python had a bit-vector type for this
purpose, but since it doesn't (does it?) a Python long seems like the
next best thing.

Is there a way to do this on a case-by-case basis (i.e. convert this
unsigned long to a Python long for this member function, but not that
one). Barring that, I couldn't figure out a way to even register a
global to_python_converter to convert all unsigned longs to Python
longs. Is this even possible?

-Matt

-----Original Message-----
From: David Abrahams [mailto:dave at boost-consulting.com] 
Sent: Wednesday, December 11, 2002 9:50 AM
To: c++-sig at python.org
Subject: Re: [C++-sig] Can't convert to unsigned int


Mark Russell <mrussell8081 at pacbell.net> writes:

> I am wrapping a library that makes extensive use of flags (in varying
> guises) that are unsigned longs.  I have put together a simplified 
> example below--when I try to use this module I get an overflow error.  
> Is there a way to make this type of conversion? Thanks in advance.  
> --Mark
>
>
> struct bigint {
> 	unsigned long flag;
> };
>
> #include <boost/python.hpp>
> using namespace boost::python;
>
> BOOST_PYTHON_MODULE(ul) {
> 	class_<bigint>("bigint")
> 		.def_readwrite("flag", &bigint::flag);
> }
>
> C:\developer\Python D3D8\dword>python
> Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on 
> win32 Type "help", "copyright", "credits" or "license" for more 
> information.
>>>> from ul import *
>>>> b = bigint()
>>>> b.flag = 0x80000000l
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> OverflowError: long int too large to convert to int

Well, that's an interesting question.  A C++ unsigned long actually has values that can't be represented as a Python INT.  Should Boost.Python use Python LONGs to represent these?

Even if we did something to make the assignment above work, what would you expect type(b.flag) to be?

This very issue has caused a lot of contention on Python-dev.  I'm honestly unsure of what the best approach is, and would value a discussion.

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution


_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list