[C++-sig] function that modifies an existing object

2011-05-09 Thread Engelbert Tijskens
Hi, i need some help on this.

//c++
class Simulation
{
Simulation() {/*...*/}
run() {/**/}
};

void modifySimulation( Simulation & sim ) {/*...*/}

BOOST_PYTHON_MODULE(pySimulation)
{
using namespace boost::python;
class_("Simulation")
.def("run"  ,&Simulation_t::run )
 ;
def("modifySimulation",modifySimulation);
}

#python
import pySimulation
sim = pySimulation.Simulation() #fine
modifySimulation(sim) #fine as well, I can debug it, and it does the right thing
sim.run() 

on this last statement the system crashes. python is basically telling me that 
sim is unbound. it seems to mee that the Simulation object is destroyed, but i 
cannot see why nor how i am supposed to prevent this using boost.python
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS default args core dump

2011-05-09 Thread Holger Joukl

Hi,

this is basically a re-post of a problem I posted 5 weeks ago, on which
there's
been no echo whatsoever. Now, I'm unsure if this is because I posted on
April 1st,
nobody has ever seen this problem on his platform, nobody ever uses the
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro this way or I'm doing s.th.
blatantly
stupid.

What nags me is that I don't think I'm doing something exotic and,
moreover, that
I see different behaviour depending on execution context, i.e. run in a
script with
or without previous method call(s) vs interactive interpreter session.

I'll try to summarize the problem a bit more(see below for a link to the
original post
for reference):

I'm having trouble wrapping a very simple member function with Boost.Python
using the
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro, getting a segmentation fault
(sometimes
a bus error).

I run into the problem both with Boost 1.44.0 and 1.46.1, running on
Solaris 10/Sparc
using gcc 4.5.1 and Python 2.7.1.

I can reproducibly avoid the segfault and see an (expected) exception iff
the code is
 * not run in an interactive interpreter session and
 * if there is a boost-induced exception succesfully raised before the
critical call
(which I don't understand at all).

# wrapped class
// file default_arguments_class.hpp
class DefaultArgs {
 public: // member functions
int foo(int arg1=100, int arg2=10) { return arg1 - arg2; };
};

# wrapper code

// file default_arguments_wrap.cpp

#include 
#include "default_arguments_class.hpp"

namespace bp = boost::python;

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(DefaultArgs_foo_overloads,
DefaultArgs::foo, 0, 2)
BOOST_PYTHON_MODULE(defaultargs)
{
bp::class_("DefaultArgs", "DefaultArgs class docstring")
.def("foo_macro_a2", &DefaultArgs::foo,
DefaultArgs_foo_overloads((bp::arg("arg2"
;
};

# In use in interactive interpreter session:

>>> import defaultargs
>>> d = defaultargs.DefaultArgs()
>>>
>>> try:
... print "d.foo_macro_a2(1, 2, 3):", d.foo_macro_a2(1, 2, 3)
... except Exception, e:
... print e
...
d.foo_macro_a2(1, 2, 3): Python argument types in
DefaultArgs.foo_macro_a2(DefaultArgs, int, int, int)
did not match C++ signature:
foo_macro_a2(DefaultArgs {lvalue})
foo_macro_a2(DefaultArgs {lvalue}, int)
foo_macro_a2(DefaultArgs {lvalue}, int, int arg2)
>>> print "d.foo_macro_a2(arg2=60):", d.foo_macro_a2(arg2=60)
Bus Error (core dumped)

# In use within a script:
$ cat foo.py
import defaultargs
d = defaultargs.DefaultArgs()

try:
print "d.foo_macro_a2(1, 2, 3):", d.foo_macro_a2(1, 2, 3)
except Exception, e:
print e

print "d.foo_macro_a2(arg2=60):", d.foo_macro_a2(arg2=60)

$
PYTHONPATH=/var/tmp/boost_apps/boost/build/boost_1_46_1/py2.7/minimal/gcc-4.5.1
/debug/ /apps/local/gcc/4.5.1/bin/python2.7 foo.py
d.foo_macro_a2(1, 2, 3): Python argument types in
DefaultArgs.foo_macro_a2(DefaultArgs, int, int, int)
did not match C++ signature:
foo_macro_a2(DefaultArgs {lvalue})
foo_macro_a2(DefaultArgs {lvalue}, int)
foo_macro_a2(DefaultArgs {lvalue}, int, int arg2)
d.foo_macro_a2(arg2=60):
Traceback (most recent call last):
  File "/ae/data/tmp/hjoukl/foo.py", line 9, in 
print "d.foo_macro_a2(arg2=60):", d.foo_macro_a2(arg2=60)
Boost.Python.ArgumentError: Python argument types in
DefaultArgs.foo_macro_a2(DefaultArgs)
did not match C++ signature:
foo_macro_a2(DefaultArgs {lvalue})
foo_macro_a2(DefaultArgs {lvalue}, int)
foo_macro_a2(DefaultArgs {lvalue}, int, int arg2)

My original post complete with bjam etc. can be found here:
http://article.gmane.org/gmane.comp.python.c%2B%2B/15163

Holger


Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart

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


Re: [C++-sig] BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS default args core dump

2011-05-09 Thread Ralf W. Grosse-Kunstleve
Hi Holger,

You don't need the BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro if you support 
keyword arguments (which is usually best). It should be as simple as:

.def("foo", &DefaultArgs::foo, (bp::arg("arg1")=100, bp::arg("arg2")=10))


The FUNCTION_OVERLOADS macros are more or less a relict. It is almost always 
best to follow the recipe above.

General remarks: use valgrind to find sources of segmentation faults and bus 
errors; look in boost/libs/python/test for role models to follow (everything 
you see in there is certain to work).


Ralf



>
>From: Holger Joukl 
>To: [email protected]
>Sent: Monday, May 9, 2011 8:14 AM
>Subject: Re: [C++-sig] BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADSdefault args core 
>dump
>
>
>Hi,
>
>this is basically a re-post of a problem I posted 5 weeks ago, on which
>there's
>been no echo whatsoever. Now, I'm unsure if this is because I posted on
>April 1st,
>nobody has ever seen this problem on his platform, nobody ever uses the
>BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro this way or I'm doing s.th.
>blatantly
>stupid.
>
>What nags me is that I don't think I'm doing something exotic and,
>moreover, that
>I see different behaviour depending on execution context, i.e. run in a
>script with
>or without previous method call(s) vs interactive interpreter session.
>
>I'll try to summarize the problem a bit more(see below for a link to the
>original post
>for reference):
>
>I'm having trouble wrapping a very simple member function with Boost.Python
>using the
>BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS macro, getting a segmentation fault
>(sometimes
>a bus error).
>
>I run into the problem both with Boost 1.44.0 and 1.46.1, running on
>Solaris 10/Sparc
>using gcc 4.5.1 and Python 2.7.1.
>
>I can reproducibly avoid the segfault and see an (expected) exception iff
>the code is
>* not run in an interactive interpreter session and
>* if there is a boost-induced exception succesfully raised before the
>critical call
>(which I don't understand at all).
>
># wrapped class
>// file default_arguments_class.hpp
>class DefaultArgs {
>public: // member functions
>    int foo(int arg1=100, int arg2=10) { return arg1 - arg2; };
>};
>
># wrapper code
>
>// file default_arguments_wrap.cpp
>
>#include 
>#include "default_arguments_class.hpp"
>
>namespace bp = boost::python;
>
>BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(DefaultArgs_foo_overloads,
>DefaultArgs::foo, 0, 2)
>BOOST_PYTHON_MODULE(defaultargs)
>{
>    bp::class_("DefaultArgs", "DefaultArgs class docstring")
>        .def("foo_macro_a2", &DefaultArgs::foo,
>DefaultArgs_foo_overloads((bp::arg("arg2"
>    ;
>};
>
># In use in interactive interpreter session:
>
 import defaultargs
 d = defaultargs.DefaultArgs()

 try:
>...     print "d.foo_macro_a2(1, 2, 3):", d.foo_macro_a2(1, 2, 3)
>... except Exception, e:
>...     print e
>...
>d.foo_macro_a2(1, 2, 3): Python argument types in
>    DefaultArgs.foo_macro_a2(DefaultArgs, int, int, int)
>did not match C++ signature:
>    foo_macro_a2(DefaultArgs {lvalue})
>    foo_macro_a2(DefaultArgs {lvalue}, int)
>    foo_macro_a2(DefaultArgs {lvalue}, int, int arg2)
 print "d.foo_macro_a2(arg2=60):", d.foo_macro_a2(arg2=60)
>Bus Error (core dumped)
>
># In use within a script:
>$ cat foo.py
>import defaultargs
>d = defaultargs.DefaultArgs()
>
>try:
>    print "d.foo_macro_a2(1, 2, 3):", d.foo_macro_a2(1, 2, 3)
>except Exception, e:
>    print e
>
>print "d.foo_macro_a2(arg2=60):", d.foo_macro_a2(arg2=60)
>
>$
>PYTHONPATH=/var/tmp/boost_apps/boost/build/boost_1_46_1/py2.7/minimal/gcc-4.5.1
>/debug/ /apps/local/gcc/4.5.1/bin/python2.7 foo.py
>d.foo_macro_a2(1, 2, 3): Python argument types in
>    DefaultArgs.foo_macro_a2(DefaultArgs, int, int, int)
>did not match C++ signature:
>    foo_macro_a2(DefaultArgs {lvalue})
>    foo_macro_a2(DefaultArgs {lvalue}, int)
>    foo_macro_a2(DefaultArgs {lvalue}, int, int arg2)
>d.foo_macro_a2(arg2=60):
>Traceback (most recent call last):
>  File "/ae/data/tmp/hjoukl/foo.py", line 9, in 
>    print "d.foo_macro_a2(arg2=60):", d.foo_macro_a2(arg2=60)
>Boost.Python.ArgumentError: Python argument types in
>    DefaultArgs.foo_macro_a2(DefaultArgs)
>did not match C++ signature:
>    foo_macro_a2(DefaultArgs {lvalue})
>    foo_macro_a2(DefaultArgs {lvalue}, int)
>    foo_macro_a2(DefaultArgs {lvalue}, int, int arg2)
>
>My original post complete with bjam etc. can be found here:
>http://article.gmane.org/gmane.comp.python.c%2B%2B/15163
>
>Holger
>
>
>Landesbank Baden-Wuerttemberg
>Anstalt des oeffentlichen Rechts
>Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
>HRA 12704
>Amtsgericht Stuttgart
>
>___
>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/cpluspl