Hi,

I noticed that boost.python silently accepts negative (or too long)
integers for constructor/methods that have "unsigned int" parameters.
(signed) "int" arguments seem to be handled just fine. Is it possible
to tell boost.python to check for overflow and raise an exception just
like it happens to int parameters ?

BTW, I'm using boost1.38.

See test case below (save it as "mymodule.cpp"), "MyClass1" uses int
parameters, "MyClass2" uses unsigned int:

### snip ###

/*

$ g++ -shared -I/usr/include/python2.6 -o mymodule.so mymodule.cpp
-lboost_python-mt
$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mymodule import MyClass1, MyClass2
>>>
>>> MyClass1(840835495615213080).some_method(840835495615213080)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int
>>>
>>> MyClass1(-1).some_method(-1)
MyClass1: -1
MyClass1::some_method: -1
>>>
>>> MyClass2(840835495615213080).some_method(840835495615213080)
MyClass2: 4294967295
MyClass2::some_method: 4294967295
>>>
>>> MyClass2(-1).some_method(-1)
MyClass2: 4294967295
MyClass2::some_method: 4294967295
>>>

*/

#include <boost/python.hpp>
#include <iostream>

class MyClass1
{
public:
        MyClass1(int x) { std::cout << "MyClass1: " << x << std::endl; }
        void some_method(int x) { std::cout << "MyClass1::some_method: " << x
<< std::endl; }
};

class MyClass2
{
public:
        MyClass2(unsigned int x) { std::cout << "MyClass2: " << x << std::endl; 
}
        void some_method(unsigned int x) { std::cout <<
"MyClass2::some_method: " << x << std::endl; }
};

using namespace boost::python;

BOOST_PYTHON_MODULE(mymodule)
{
        class_<MyClass1>("MyClass1", init<int>())
                .def("some_method", &MyClass1::some_method)
        ;
        class_<MyClass2>("MyClass2", init<unsigned int>())
                .def("some_method", &MyClass2::some_method)
        ;
}

### snip ###

Regards,
-- 
Anderson Lizardo
OpenBossa Labs - INdT
Manaus - Brazil
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to