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

Mark Russell mrussell8081 at pacbell.net
Wed Dec 11 19:08:21 CET 2002


David,

Thanks for your reply--the approach I have taken to work around this case is
to create a python extension object called flag.  This object simply stores
the long value I want to set.  On the boost side I use an lvalue_from_ptype
converter to retrieve the value.  This works just fine so I will continue to
use this approach.  --Mark

-----Original Message-----
From: c++-sig-admin at python.org [mailto:c++-sig-admin at python.org]On
Behalf Of David Abrahams
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