diff --git a/test/callbacks.cpp b/test/callbacks.cpp index 2bf285b5..6d1b8fba 100644 --- a/test/callbacks.cpp +++ b/test/callbacks.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include using namespace boost::python; @@ -48,6 +50,16 @@ void apply_void_X_ref(PyObject* f, X& x) returning::call(f, boost::ref(x)); } +X& apply_X_ref_pyobject(PyObject* f, PyObject* obj) +{ + return returning::call(f, obj); +} + +X* apply_X_ptr_pyobject(PyObject* f, PyObject* obj) +{ + return returning::call(f, obj); +} + void apply_void_X_cref(PyObject* f, X const& x) { returning::call(f, boost::cref(x)); @@ -63,6 +75,21 @@ void apply_void_X_deep_ptr(PyObject* f, X* x) returning::call(f, x); } +char const* apply_cstring_cstring(PyObject* f, char const* s) +{ + return returning ::call(f, s); +} + +char const* apply_cstring_pyobject(PyObject* f, PyObject* s) +{ + return returning ::call(f, s); +} + +char apply_char_char(PyObject* f, char c) +{ + return returning ::call(f, c); +} + int X::counter; BOOST_PYTHON_MODULE_INIT(callbacks_ext) @@ -75,6 +102,17 @@ BOOST_PYTHON_MODULE_INIT(callbacks_ext) .def("apply_void_X_cref", apply_void_X_cref) .def("apply_void_X_ptr", apply_void_X_ptr) .def("apply_void_X_deep_ptr", apply_void_X_deep_ptr) + + .def("apply_X_ptr_pyobject", apply_X_ptr_pyobject + , return_value_policy()) + + .def("apply_X_ref_pyobject", apply_X_ref_pyobject + , return_value_policy()) + + .def("apply_cstring_cstring", apply_cstring_cstring) + .def("apply_cstring_pyobject", apply_cstring_pyobject) + .def("apply_char_char", apply_char_char) + .add( class_("X") .def_init(args()) diff --git a/test/callbacks.py b/test/callbacks.py index 810aad53..71ad3fb7 100644 --- a/test/callbacks.py +++ b/test/callbacks.py @@ -66,6 +66,46 @@ 44 >>> last_x.value() 43 + +>>> y = apply_X_ref_pyobject(identity, x) +>>> assert y.value() == x.value() +>>> increment(x) +>>> assert y.value() == x.value() + +>>> y = apply_X_ptr_pyobject(identity, x) +>>> assert y.value() == x.value() +>>> increment(x) +>>> assert y.value() == x.value() + +>>> y = apply_X_ptr_pyobject(identity, None) +>>> y + +>>> def new_x(ignored): +... return X(666) +... +>>> try: apply_X_ref_pyobject(new_x, 1) +... except ReferenceError: pass +... else: print 'no error' + +>>> try: apply_X_ptr_pyobject(new_x, 1) +... except ReferenceError: pass +... else: print 'no error' + +>>> try: apply_cstring_cstring(identity, 'hello') +... except ReferenceError: pass +... else: print 'no error' + +>>> apply_char_char(identity, 'x') +'x' + +>>> apply_cstring_pyobject(identity, 'hello') +'hello' + +>>> apply_cstring_pyobject(identity, None) + + +>>> apply_char_char(identity, 'x') +'x' ''' def run(args = None):