473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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?
Jul 18 '05 #1
2 3037
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::object Variant::getValue() {

switch (type) {
case INT:
return Py_BuildValue("i", union.int);
return python::object(union.int);
break;
case SOMECLASS:
return python::object(*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


Jul 18 '05 #2
"Mike Rovner" <mi**@nospam.com> wrote in message news:<ma*********************************@python.o rg>...
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.
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.


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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gouda Man | last post by:
I'm planning to write a large program that will have advanced scripting in python in which the scripter will subclass c++ objects to add functionality. this leads me to boost.python (if you dont...
0
by: Li Daobing | last post by:
I can't use .def(str(self)) I write a simple example, without `str', I can build it well, but with this one, I can't build //Rational.cpp #include <boost/python.hpp> #include <iostream> ...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
11
by: Osiris | last post by:
I have these pieces of C-code (NOT C++ !!) I want to call from Python. I found Boost. I have MS Visual Studio 2005 with C++. is this the idea: I write the following C source file:...
5
by: Cromulent | last post by:
Okay I'm having a few issues with this and I can't seem to get it sorted out (most likely due to my inexperience with Python). Here is my Python code: def fileInput(): data = s =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.