[C++-sig] Re: Converting a C++ object to python
David Abrahams
dave at boost-consulting.com
Tue Sep 16 23:40:38 CEST 2003
Raoul Gough <RaoulGough at yahoo.co.uk> writes:
G> "Ron Penton" <rpenton at adelphia.net> writes:
>
>>>If you catch a boost::python::error_already_set in C++, I guess you
>>>can get more information by calling PyErr_Print(), which should dump a
>>>standard Python traceback on stderr. At a guess, you're getting a
>>>Python TypeError from the object constructor, because it doesn't have
>>>a converter for booga *. BTW, why are you passing a pointer to b,
>>>instead of a reference?
>>
>> PyErr_Print says "TypeError: No Python class registered for C++ class class
>> booga",
>> which confirms what you thought.
>
> Well, not really - I would have thought that there is a problem with
> booga * (i.e. pointer to booga), but from your error message, that is
> not the case. I don't suppose it makes any difference to use b instead
> of &b then?
>
>>
>> The part that confuses me is the converter part; I'm not quite sure how to
>> go about creating one. All of the examples I can find go about the creation
>> of the python types manually, rather than using the class_ definition, so
>> I'm really unsure of where to go from here.
>
> Well, we've now snipped the original code, but looking back at it from
> your original post:
>
>> class booga{public:
>> booga() : a( 0 ) {} int a; void
>> looga() { a = a + 10; cout << a << endl; }};
>> BOOST_PYTHON_MODULE( wrap ){ class_<booga>( "booga"
>> ) .def( "looga", &booga::looga
>> );}
>> int main(){ Py_Initialize();
>>
>> PyImport_AppendInittab(
>> "wrap", initwrap );
>> booga b;
>> object obj( &b );
>
> the class_<booga> constructor should handle all of the converter
> registration. However, I know that at least *some* of the registration
> is done at run-time.
All of it, except for some built-in to-python conversions like those
for int, char const*, etc.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list