Now I want to write a factory function like
PyObject* f(char c)
{
if(c == 'x')
return new X;
else if(c == 'y')
return new Y;
else if(c == 'z')
return new Z;
else
PyErr_SetString(PyExc_ValueError, "Bad function argument");
}
and export that function to Python.
What is missing here is code that converts X*, Y* and Z* to PyObject*.
How do I write that?
Thank you,
Johan
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
> What is missing here is code that converts X*, Y* and Z* to PyObject*.
> How do I write that?
I figured out the answer:
boost::python::api::object(...).ptr()
It is not easy to find these things in boost.python documentation.
--Johan
No, that did not work.
When I run the following code
C++:
#include<boost/noncopyable.hpp>
#include<boost/python.hpp>
using boost::noncopyable;
using namespace boost::python;
class X : noncopyable {};
PyObject* f()
{
return api::object(new X).ptr();
}
BOOST_PYTHON_MODULE(Foo)
{
class_<X, noncopyable>("X");
def("f", &f);
}
Python:
import Foo
Foo.f()
then an exception, with the following error string, is thrown from the
api::object constructor:
TypeError: No to_python (by-value) converter found for C++ type:
class X
Am I using api::object incorrectly, or should I not use api::object at
all but do something completely different?
As far as I can tell, the above should work. Could you please file a ticket on
trac?
Ravi
Now I have finally done that:
https://svn.boost.org/trac/boost/ticket/6203
--Johan
It turned out not to be a bug.
The python::object constructor does not take a raw pointer, you must
give it a shared pointer. See https://svn.boost.org/trac/boost/ticket/6203
Thanks
UJ
You should explicitly trim every token (use boost/algorithm/string/trim.hpp).
Alternatively, you could use more flexible facility, like Spirit.