[C++-sig] Returning values to Python in C++ reference arguments

2015-05-24 Thread Jason Addison
How can results be returned in function arguments?

I've include my example C++ extension and Python code below.

I've tried, what I think, are some obvious approaches (Python objects,
ctypes), though none have worked.

It seems like this should be doable.

If the python code owns, say, a c_int, I would thing it is mostly safe
to pass it to c++ to be modified.

This seems like it should be a common question, though I can't find it
being explicitly addressed in the documentation

All of the call policy stuff seems to only apply to the function result.

Can the policy stuff be applied to the function arguments?

If so, how would that work with my example?

Thanks,
Jason


my_module.cpp

/*
clang++ -c -I/usr/include/python2.7 my_module.cpp
clang++ -dynamiclib -o my_module.so -lboost_python -lpython2.7
my_module.o
 */

#include 

struct Foo {
void bar(int &a, double &b) {
a = 12;
b = 6.2832;
}
};

using namespace boost::python;

BOOST_PYTHON_MODULE(my_module)
{
class_("Foo", init<>())
.def("bar", &Foo::bar)
;
}



byref.py

from ctypes import *
import my_module

f = my_module.Foo()

a = 0
b = 0.

# py objects
'''
f.bar(a, b)

Traceback (most recent call last):
  File "byref.py", line 8, in 
f.bar(a, b)
Boost.Python.ArgumentError: Python argument types in
Foo.bar(Foo, int, float)
did not match C++ signature:
bar(Foo {lvalue}, int {lvalue}, double {lvalue})
'''

x = c_int(0)
y = c_double(0)

# as is
'''
f.bar(x, y)

Traceback (most recent call last):
  File "byref.py", line 25, in 
f.bar(x, y)
Boost.Python.ArgumentError: Python argument types in
Foo.bar(Foo, c_int, c_double)
did not match C++ signature:
bar(Foo {lvalue}, int {lvalue}, double {lvalue})
'''

# ctype.byref
'''
f.bar(byref(x), byref(y))

Traceback (most recent call last):
  File "byref.py", line 36, in 
f.bar(byref(x), byref(y))
Boost.Python.ArgumentError: Python argument types in
Foo.bar(Foo, CArgObject, CArgObject)
did not match C++ signature:
bar(Foo {lvalue}, int {lvalue}, double {lvalue})
'''

# ctype.pointer
'''
f.bar(pointer(x), pointer(y))

Traceback (most recent call last):
  File "byref.py", line 48, in 
f.bar(pointer(x), pointer(y))
Boost.Python.ArgumentError: Python argument types in
Foo.bar(Foo, LP_c_int, LP_c_double)
did not match C++ signature:
bar(Foo {lvalue}, int {lvalue}, double {lvalue})
'''

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


Re: [C++-sig] Returning values to Python in C++ reference arguments

2015-05-26 Thread Jason Addison
On Mon, May 25, 2015 at 3:39 AM, Trigve Siver via Cplusplus-sig
 wrote:
>
>
>>
>> How can results be returned in function arguments?
>>
>
>
> I don't think that you can return arguments by reference/pointers in python. 
> You could theoretically pass a mutable sequence (list) and push the objects 
> there.
>

You can return results via function arguments from C to Python (see code below).

Why is something similar impossible with Boost Python?

my_module2.c

/*
clang -c -I/usr/include/python2.7 my_module2.c
clang -dynamiclib -o my_module2.so -lpython2.7 my_module2.o
 */

void foo_bar(int *a, double *b) {
*a = 12;
*b = 6.2832;
}


byref2.py

from ctypes import *
my_module2 = CDLL('my_module2.so')

x = c_int(0)
y = c_double(0)

my_module2.foo_bar(byref(x), byref(y))

assert(x.value == 12)
assert(y.value == 6.2832)


> But the common approach is to return tuple.
___
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig