[C++-sig] Can't get Boost.Python to use extensions generated and wrapped with Py++
Lawrence Spector
Lawrence.Spector at CanfieldSci.com
Tue Jul 17 00:10:31 CEST 2007
Here's another issue I'm having. I have the following class:
#pragma once
#include <iostream>
#include <tchar.h>
class TestClass
{
private:
std::ostream& m_outStream;
int m_x;
float m_y;
char m_z;
public:
TestClass() : m_outStream(std::cout), m_x(0), m_y(0), m_z(0) {}
TestClass(std::ostream& p_outStream, int p_x, float p_y, char p_z) :
m_outStream(p_outStream), m_x(p_x), m_y(p_y), m_z(p_z)
{
std::cout << __FUNCTION__ << " called. this = " << this << std::endl;
} // end TestClass
~TestClass()
{
std::cout << __FUNCTION__ << " called. this = " << this << std::endl;
} // end TestClass
virtual void output()
{
m_outStream << "x = " << m_x << ", y = " << m_y << ", z = " << m_z << "\n";
std::cout << __FUNCTION__ << " called. this = " << this << std::endl;
} // end callMethod
}; // end TestClass
Py++ successfully generated the following:
namespace bp = boost::python;
struct TestClass_wrapper : TestClass, bp::wrapper< TestClass > {
TestClass_wrapper( )
: TestClass( )
, bp::wrapper< TestClass >(){
// null constructor
}
TestClass_wrapper(::std::ostream & p_outStream, int p_x, float p_y, char p_z )
: TestClass( boost::ref(p_outStream), p_x, p_y, p_z )
, bp::wrapper< TestClass >(){
// constructor
}
virtual void output( ) {
if( bp::override func_output = this->get_override( "output" ) )
func_output( );
else
this->TestClass::output( );
}
void default_output( ) {
TestClass::output( );
}
};
BOOST_PYTHON_MODULE(PythonInterface){
bp::class_< TestClass_wrapper, boost::noncopyable >( "TestClass" )
.def( bp::init< >() )
.def( bp::init< std::ostream &, int, float, char >(( bp::arg("p_outStream"), bp::arg("p_x"), bp::arg("p_y"), bp::arg("p_z") )) )
.def(
"output"
, &::TestClass::output
, &TestClass_wrapper::default_output );
}
Great. I use it in the following way, as a simple test:
TestClass testClass(std::cout, 3, 9.6f, 'Q');
boost::python::object pyTestClass(testClass);
main_namespace[L"pyTestClass"] = pyTestClass;
This throws with the following python error:
Caught error_already_set
TypeError: No to_python (by-value) converter found for C++ type: class TestClass
Any ideas what steps I've missed? I've successfully built a few python extensions without using Boost.Python, but never successfully using a wrapper. Am I missing something?
Thanks,
Lawrence
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070716/b4c44279/attachment.htm>
More information about the Cplusplus-sig
mailing list