From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- src/boost/libs/python/test/pytype_function.cpp | 85 ++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/boost/libs/python/test/pytype_function.cpp (limited to 'src/boost/libs/python/test/pytype_function.cpp') diff --git a/src/boost/libs/python/test/pytype_function.cpp b/src/boost/libs/python/test/pytype_function.cpp new file mode 100644 index 000000000..46cce19e6 --- /dev/null +++ b/src/boost/libs/python/test/pytype_function.cpp @@ -0,0 +1,85 @@ +// Copyright Joel de Guzman 2004. Distributed under the Boost +// Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include +#include + +using namespace boost::python; + +struct A +{ +}; + +struct B +{ + A a; + B(const A& a_):a(a_){} +}; + +// Converter from A to python int +struct BToPython +#ifndef BOOST_PYTHON_NO_PY_SIGNATURES + : converter::to_python_target_type //inherits get_pytype +#endif +{ + static PyObject* convert(const B& b) + { + return boost::python::incref(boost::python::object(b.a).ptr()); + } +}; + +// Conversion from python int to A +struct BFromPython +{ + BFromPython() + { + boost::python::converter::registry::push_back( + &convertible, + &construct, + boost::python::type_id< B >() +#ifndef BOOST_PYTHON_NO_PY_SIGNATURES + , &converter::expected_from_python_type::get_pytype//convertible to A can be converted to B +#endif + ); + } + + static void* convertible(PyObject* obj_ptr) + { + extract ex(obj_ptr); + if (!ex.check()) return 0; + return obj_ptr; + } + + static void construct( + PyObject* obj_ptr, + boost::python::converter::rvalue_from_python_stage1_data* data) + { + void* storage = ( + (boost::python::converter::rvalue_from_python_storage< B >*)data)-> storage.bytes; + + extract ex(obj_ptr); + new (storage) B(ex()); + data->convertible = storage; + } +}; + + +B func(const B& b) { return b ; } + + +BOOST_PYTHON_MODULE(pytype_function_ext) +{ + to_python_converter< B , BToPython,true >(); //has get_pytype + BFromPython(); + + class_("A") ; + + def("func", &func); + +} + +#include "module_tail.cpp" -- cgit v1.2.3