Skip to content

Commit c170b1b

Browse files
committed
char conversions
Handle dangling references [SVN r13164]
1 parent be6016a commit c170b1b

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

test/callbacks.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <boost/python/class.hpp>
99
#include <boost/ref.hpp>
1010
#include <boost/python/ptr.hpp>
11+
#include <boost/python/return_value_policy.hpp>
12+
#include <boost/python/reference_existing_object.hpp>
1113

1214
using namespace boost::python;
1315

@@ -48,6 +50,16 @@ void apply_void_X_ref(PyObject* f, X& x)
4850
returning<void>::call(f, boost::ref(x));
4951
}
5052

53+
X& apply_X_ref_pyobject(PyObject* f, PyObject* obj)
54+
{
55+
return returning<X&>::call(f, obj);
56+
}
57+
58+
X* apply_X_ptr_pyobject(PyObject* f, PyObject* obj)
59+
{
60+
return returning<X*>::call(f, obj);
61+
}
62+
5163
void apply_void_X_cref(PyObject* f, X const& x)
5264
{
5365
returning<void>::call(f, boost::cref(x));
@@ -63,6 +75,21 @@ void apply_void_X_deep_ptr(PyObject* f, X* x)
6375
returning<void>::call(f, x);
6476
}
6577

78+
char const* apply_cstring_cstring(PyObject* f, char const* s)
79+
{
80+
return returning <char const*>::call(f, s);
81+
}
82+
83+
char const* apply_cstring_pyobject(PyObject* f, PyObject* s)
84+
{
85+
return returning <char const*>::call(f, s);
86+
}
87+
88+
char apply_char_char(PyObject* f, char c)
89+
{
90+
return returning <char>::call(f, c);
91+
}
92+
6693
int X::counter;
6794

6895
BOOST_PYTHON_MODULE_INIT(callbacks_ext)
@@ -75,6 +102,17 @@ BOOST_PYTHON_MODULE_INIT(callbacks_ext)
75102
.def("apply_void_X_cref", apply_void_X_cref)
76103
.def("apply_void_X_ptr", apply_void_X_ptr)
77104
.def("apply_void_X_deep_ptr", apply_void_X_deep_ptr)
105+
106+
.def("apply_X_ptr_pyobject", apply_X_ptr_pyobject
107+
, return_value_policy<reference_existing_object>())
108+
109+
.def("apply_X_ref_pyobject", apply_X_ref_pyobject
110+
, return_value_policy<reference_existing_object>())
111+
112+
.def("apply_cstring_cstring", apply_cstring_cstring)
113+
.def("apply_cstring_pyobject", apply_cstring_pyobject)
114+
.def("apply_char_char", apply_char_char)
115+
78116
.add(
79117
class_<X>("X")
80118
.def_init(args<int>())

test/callbacks.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,46 @@
6666
44
6767
>>> last_x.value()
6868
43
69+
70+
>>> y = apply_X_ref_pyobject(identity, x)
71+
>>> assert y.value() == x.value()
72+
>>> increment(x)
73+
>>> assert y.value() == x.value()
74+
75+
>>> y = apply_X_ptr_pyobject(identity, x)
76+
>>> assert y.value() == x.value()
77+
>>> increment(x)
78+
>>> assert y.value() == x.value()
79+
80+
>>> y = apply_X_ptr_pyobject(identity, None)
81+
>>> y
82+
83+
>>> def new_x(ignored):
84+
... return X(666)
85+
...
86+
>>> try: apply_X_ref_pyobject(new_x, 1)
87+
... except ReferenceError: pass
88+
... else: print 'no error'
89+
90+
>>> try: apply_X_ptr_pyobject(new_x, 1)
91+
... except ReferenceError: pass
92+
... else: print 'no error'
93+
94+
>>> try: apply_cstring_cstring(identity, 'hello')
95+
... except ReferenceError: pass
96+
... else: print 'no error'
97+
98+
>>> apply_char_char(identity, 'x')
99+
'x'
100+
101+
>>> apply_cstring_pyobject(identity, 'hello')
102+
'hello'
103+
104+
>>> apply_cstring_pyobject(identity, None)
105+
106+
107+
>>> apply_char_char(identity, 'x')
108+
'x'
69109
'''
70110

71111
def run(args = None):

0 commit comments

Comments
 (0)