[python] converting C++ object to PyObject

I have three C++ classes X, Y and Z that I have exported from C++ to Python using python::class_. 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

On 2011-07-27 11:06, Johan Råde wrote:
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

On 2011-07-29 13:29, Johan Råde wrote:
On 2011-07-27 11:06, Johan Råde wrote:
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()
No, that did not work.
When I run the following code
C++:
#include

On Friday, July 29, 2011 05:48:32 AM Johan Råde wrote:
When I run the following code
C++:
#include
#include using boost::noncopyable; using namespace boost::python;
class X : noncopyable {};
PyObject* f() { return api::object(new X).ptr(); }
BOOST_PYTHON_MODULE(Foo) { class_
("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

On 8/8/2011 6:21 AM, Ravi wrote:
On Friday, July 29, 2011 05:48:32 AM Johan Råde wrote: ...
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

On 12/3/2011 1:13 PM, Johan Råde wrote:
On 8/8/2011 6:21 AM, Ravi wrote:
On Friday, July 29, 2011 05:48:32 AM Johan Råde wrote: ...
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

All,
I noticed the below crashes[run time error because of space in string]
Is there a possibility to handle this situation? [without explicitly
calling split]
string s = " 12252001";
int offsets[] = {5,2,4};
offset_separator f(offsets, offsets+3);
tokenizer

I noticed the below crashes[run time error because of space in string] Is there a possibility to handle this situation? [without explicitly calling split]
string s = " 12252001"; int offsets[] = {5,2,4}; offset_separator f(offsets, offsets+3); tokenizer
tok(s,f); for(tokenizer ::iterator beg=tok.begin(); beg!=tok.end();++beg) int val = boost::lexical_cast<int>( *beg);
You should explicitly trim every token (use boost/algorithm/string/trim.hpp). Alternatively, you could use more flexible facility, like Spirit.
participants (4)
-
Igor R
-
Johan Råde
-
Ravi
-
Uthpal Urubail