Boost + Python C/API: Mixing python return types with boost return types

S

Steve Knight

Hello,

I'm new to Boost & Python and I'm diving straight in by trying to
write an extension module to a third party library. Foolishness
probably, but I don't have much choice!

My question is how can I write a C++ method that will either return a
boost wrapped C++ class or a Python object type depending on some
internal value?

So I want to do something like:

PyObject* Variant::getValue() {

switch (type) {
case INT:
return Py_BuildValue("i", union.int);
break;
case SOMECLASS:
??????
}
}

But I can't work out how to get my switch to construct and return my
boost C++ wrapped object in this scheme. I'm getting round it by:

SomeClass Variant::getSomeClass() {
SomeClass s(*reinterpret_cast<SomeClass*>(union.p_void));
return s;
}

But it seems a bit nasty. Any ideas?
 
M

Mike Rovner

Steve Knight wrote:

It's better ask boost.python related questions in comp.lang.python.c++
group/list.
I'm new to Boost & Python and I'm diving straight in by trying to
write an extension module to a third party library. Foolishness
probably, but I don't have much choice!

Why, it's usually fun and rewarding.
My question is how can I write a C++ method that will either return a
boost wrapped C++ class or a Python object type depending on some
internal value?

In either case you return a python object, that is what matters.
So I want to do something like:

PyObject* Variant::getValue() {

python::eek:bject Variant::getValue() {
switch (type) {
case INT:
return Py_BuildValue("i", union.int);

return python::eek:bject(union.int);
break;
case SOMECLASS:

return python::eek:bject(*union.p_someclass);
??????
}
}

But I can't work out how to get my switch to construct and return my
boost C++ wrapped object in this scheme. I'm getting round it by:

SomeClass Variant::getSomeClass() {
SomeClass s(*reinterpret_cast<SomeClass*>(union.p_void));
return s;
}

But it seems a bit nasty. Any ideas?

In case you keep void pointer in union you'll need that nastiness anyway.
Why can't you keep a pointer to someclass in the union instead?

BTW, more complete example of the code (better working one) will definitely
clarify communications.

HTH,
Mike
 
S

Steve Knight

Mike Rovner said:
Steve Knight wrote:

It's better ask boost.python related questions in comp.lang.python.c++
group/list.

Ahh, yes. I'll remember to do that next time.
Why, it's usually fun and rewarding.

Oh, it's fun alright it's just a tricky place to start when you've
never really written a line of Python before!!

Your suggestions work a treat this is what I was looking for but I
couldn't really see how to do it from the tutorials and Wikis.
Must've missed something somewhere.

Thanks alot for your help,

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top