[C++-sig] long long unsigned issue...
Milind Patil
milind_patil at hotmail.com
Sat May 3 01:43:46 CEST 2003
Hi,
I have used boost libraries to wrap a large c++ library and it has worked
very well.
However, I stumbled on the issue of long long conversion to class problem.
The problem has been reduced to the minimum code below.
I would like to expose to python a class that has constructors from int and
long long unsigned int among others:
class Y {
public:
Y() : y(0L) { }
Y(int y) : y(y) { }
Y(long long unsigned int y) : y(y) { }
Y(Y const& rhs) : y(rhs.y) { }
virtual ~Y() { }
operator int() const { return y; }
void operator=(Y const& y) {
this->y = y.y;
}
long long unsigned int y;
};
I use boost thus:
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python.hpp>
#include <hello.h>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
class_< Y >("Y", init< >())
.def(init< int >())
.def(init< long long unsigned int >())
.def(init< const Y & >())
.def_readwrite("y", &Y::y)
.def("__int__", &Y::operator int)
;
implicitly_convertible<int, Y>();
implicitly_convertible<long long unsigned int, Y>();
}
and use it in python thus:
import hello
x = hello.Y(4294967295)
print x.y
y = hello.Y(0xFFFFFFFFFFFFFFFF)
print y.y
However the code craps out:
Traceback (most recent call last):
File "p.py", line 3, in ?
x = hello.Y(4294967295)
OverflowError: long int too large to convert to int
Why wouldn't it try yo convert to long long unsigned int before trying
out int?
I am seeking a behaviour where python int is converted to c++ int
before constructing to c++ object Y, and python long is converted
to c++ long long unsigned int before constructing to c++ object Y.
It is possible that there are ways to write explicit boost converters
from long to Y, but that is too hairy for me. But before I go into it, I
was wondering if I have made a silly presumption or missed a simple
obvious way to do this. Any pointers will be a great help.
Thanks,
Milind
More information about the Cplusplus-sig
mailing list