Re: [C++-sig] reuse C++ classes that have been wrapped with SWIG

2010-07-20 Thread Marco Selinger
Dear Hans,
your explanations are very helpful.

Now it is obvious that I should not redeclare the opencv classes in 
boost.python 
after they have been wrapped with swig. But still I did not think about it in 
the right way.

You write:
>Instead, you *may* define a manual converter to make your life easier, i.e. 
>let 

>boost::python wrap your functions and automatically call SWIGs wrap/unwrap 
>functions for the corresponding OpenCV classes.
Do you have some example, how this can be achieved? Do I need to do some text 
processing or parsing of the swig output to find out which (un)wrap functions 
to 
call? I have never worked on the internals of swig, so that I wouldn't know 
where to find those (un)wrappers.

Thanks
Marco



Von: Hans Meine 
An: Development of Python/C++ integration 
Gesendet: Montag, den 19. Juli 2010, 10:55:49 Uhr
Betreff: Re: [C++-sig] reuse C++ classes that have been wrapped with SWIG

On Thursday 15 July 2010 13:35:55 Marco Selinger wrote:
> There is a complete SWIG wrapper for the OpenCV
> (http://opencv.willowgarage.com) project.
> 
> I am developing a small image processing application that uses some of the
> OpenCV classes. This application will be wrapped using Boost.Python.
> How do I declare those classes in Boost.Python that have already been
> wrapped with SWIG?

For all these questions (involving SIP/SWIG/BPL/...), remember that they're 
all based on the common Python/C API.  Thus, each of these wrapping libraries 
have means to convert from/to python, i.e. wrapping a C++ object pointer into 
a (PyObject *) and vice versa.  One just needs to know how this is done with 
each of the above, and depending on the lifetime management.

Additionally, you should *not* "declare those classes in Boost.Python" again, 
i.e. using class_<...>.  (That would lead to incompatible wrappers.)  Instead, 
you *may* define a manual converter to make your life easier, i.e. let 
boost::python wrap your functions and automatically call SWIGs wrap/unwrap 
functions for the corresponding OpenCV classes.  (The alternative is to 
declare your functions with (PyObject *) pointers and manually call these SWIG 
functions, which is probably easier if you have just one or two functions to 
wrap.)

HTH from an overview point,
  Hans
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig




___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Pickle python subclass of C++ interface

2010-07-20 Thread Hans Meine
On Sunday 18 July 2010 11:16:29 John Reid wrote:
> I'm not sure what you mean by mix-in, but my first attempt involved
> defining pickle suite getstate() and setstate() methods. I did not
> define a getinitargs() method. Unfortunately when the derived object was
> unpickled, __init__ was called with no arguments. As far as I can see
> there's no way to use the boost.python pickle suite that does not
> involve a call to __init__() on the unpickled object.

How should it work?  A proper object of a BPL extension type needs a wrapped 
C++ object "behind it".  Thus, if you unpickle such an object, the 
corresponding C++ object needs to be created.

There's the difference between pure python and BPL classes: with pure Python, 
you (or the pickle module) may just assign __class__, restore all attributes 
and be done.  For C++ objects OTOH, pickle cannot list or restore the 
attributes, or make sure that the object is in any defined state.  So you 
*need* to call a constructor, which may need arguments, hence the 
getinitargs().

In the __reduce__ hack within your last mail, you explicitly call __init__ 
with a single self arg, relying on a default constructible class, and relying 
on the fact that the C++ object will be in the same state as before.  (That's 
probably fine, since you originally wrote that you're dealing with interfaces 
w/o state, but I wanted to point it out again for the unwary readers.)

Have a nice day,
  Hans
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] reuse C++ classes that have been wrapped with SWIG

2010-07-20 Thread Hans Meine
Hi Marco!

On Tuesday 20 July 2010 09:31:48 Marco Selinger wrote:
> Do you have some example, how this can be achieved? Do I need to do some
> text processing or parsing of the swig output to find out which (un)wrap
> functions to call? I have never worked on the internals of swig, so that I
> wouldn't know where to find those (un)wrappers.

Sorry, I don't have the time to search myself, but this has been discussed on 
the list before.  You may use keywords like "swig boost::python register 
converter" or "lvalue converter".

For example, this was my first hit and looks perfect for one direction (from 
python) at least:

http://wiki.python.org/moin/boost.python/HowTo#SWIGexposedC.2B-.2B-
objectfromPython

I have done the same with SIP only, but I have no experience with SWIG.

HTH,
  Hans
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] Wanted: Examples of boost.python embedded usage

2010-07-20 Thread David Aldrich
Hi

I am new to boost.python ( and to Python ) and need to demonstrate embedding 
Python code in a C++ application.

I would like to pass data to a Python function from C++ and retrieve the result.

Please can anyone suggest a tutorial that would show me how to do this please?

Best regards

David

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Pickle python subclass of C++ interface

2010-07-20 Thread John Reid

Hans Meine wrote:

On Sunday 18 July 2010 11:16:29 John Reid wrote:

I'm not sure what you mean by mix-in, but my first attempt involved
defining pickle suite getstate() and setstate() methods. I did not
define a getinitargs() method. Unfortunately when the derived object was
unpickled, __init__ was called with no arguments. As far as I can see
there's no way to use the boost.python pickle suite that does not
involve a call to __init__() on the unpickled object.


How should it work?  A proper object of a BPL extension type needs a wrapped 
C++ object "behind it".  Thus, if you unpickle such an object, the 
corresponding C++ object needs to be created.


There's the difference between pure python and BPL classes: with pure Python, 
you (or the pickle module) may just assign __class__, restore all attributes 
and be done.  For C++ objects OTOH, pickle cannot list or restore the 
attributes, or make sure that the object is in any defined state.  So you 
*need* to call a constructor, which may need arguments, hence the 
getinitargs().


In the __reduce__ hack within your last mail, you explicitly call __init__ 
with a single self arg, relying on a default constructible class, and relying 
on the fact that the C++ object will be in the same state as before.  (That's 
probably fine, since you originally wrote that you're dealing with interfaces 
w/o state, but I wanted to point it out again for the unwary readers.)




Thanks for the clarification, that all makes sense.

I was hoping that I could make all the sub-classes picklable without 
recoding a getinitargs() for each one. I think I've managed that by 
using __reduce__(). I don't think I can do that using the boost.python 
pickle suite.


I can see why you call my method a 'hack' but I think it is fine for my 
purposes.


Thanks,
John.

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


Re: [C++-sig] Wanted: Examples of boost.python embedded usage

2010-07-20 Thread Philip Jonientz - NEXPLORE AG
Hi there

http://ironalbatross.net/wiki/index.php5/CPP_BOOST_STACKLESS_PYTHON
This one is actually with stackless python, but he's using the same 
boost.python you would use with standart cpython.
If you skip the whole setting up part for linux there are some samples on how 
to create function wrappers and so on...
This tutorial really helped me(Even if this is stackless python I think it 
might help you too).

Greetings

Phil

Von: [email protected] 
[mailto:[email protected]] Im Auftrag von David 
Aldrich
Gesendet: Dienstag, 20. Juli 2010 13:26
An: [email protected]
Betreff: [C++-sig] Wanted: Examples of boost.python embedded usage

Hi

I am new to boost.python ( and to Python ) and need to demonstrate embedding 
Python code in a C++ application.

I would like to pass data to a Python function from C++ and retrieve the result.

Please can anyone suggest a tutorial that would show me how to do this please?

Best regards

David

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Wanted: Examples of boost.python embedded usage

2010-07-20 Thread David Aldrich
Hi Phil,

Thanks, I will take a look.

Best regards

David


From: [email protected] 
[mailto:[email protected]] On Behalf Of 
Philip Jonientz - NEXPLORE AG
Sent: 20 July 2010 12:41
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Wanted: Examples of boost.python embedded usage

Hi there

http://ironalbatross.net/wiki/index.php5/CPP_BOOST_STACKLESS_PYTHON
This one is actually with stackless python, but he's using the same 
boost.python you would use with standart cpython.
If you skip the whole setting up part for linux there are some samples on how 
to create function wrappers and so on...
This tutorial really helped me(Even if this is stackless python I think it 
might help you too).

Greetings

Phil

Von: [email protected] 
[mailto:[email protected]] Im Auftrag von David 
Aldrich
Gesendet: Dienstag, 20. Juli 2010 13:26
An: [email protected]
Betreff: [C++-sig] Wanted: Examples of boost.python embedded usage

Hi

I am new to boost.python ( and to Python ) and need to demonstrate embedding 
Python code in a C++ application.

I would like to pass data to a Python function from C++ and retrieve the result.

Please can anyone suggest a tutorial that would show me how to do this please?

Best regards

David



Click 
here
 to report this email as spam.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig