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

David Abrahams dave at boost-consulting.com
Wed Dec 11 18:49:47 CET 2002


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





More information about the Cplusplus-sig mailing list