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/extract.cpp | 143 +++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 src/boost/libs/python/test/extract.cpp (limited to 'src/boost/libs/python/test/extract.cpp') diff --git a/src/boost/libs/python/test/extract.cpp b/src/boost/libs/python/test/extract.cpp new file mode 100644 index 000000000..40584a077 --- /dev/null +++ b/src/boost/libs/python/test/extract.cpp @@ -0,0 +1,143 @@ +// Copyright David Abrahams 2002. +// 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 +#include +#include +#include +#include +#include +#define BOOST_ENABLE_ASSERT_HANDLER +#include +#include "test_class.hpp" + +using namespace boost::python; + +typedef test_class<> X; + +bool extract_bool(object x) { return extract(x); } + +boost::python::list extract_list(object x) +{ + extract get_list((x)); + + // Make sure we always have the right idea about whether it's a list + bool is_list_1 = get_list.check(); + bool is_list_2 = PyObject_IsInstance(x.ptr(), (PyObject*)&PyList_Type); + if (is_list_1 != is_list_2) { + throw std::runtime_error("is_list_1 == is_list_2 failure."); + } + return get_list(); +} + +char const* extract_cstring(object x) +{ + return extract(x); +} + +std::string extract_string(object x) +{ + std::string s = extract(x); + return s; +} + +std::string const& extract_string_cref(object x) +{ +#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 +# pragma warning(push) +# pragma warning(disable:4172) // msvc lies about returning a reference to temporary +#elif defined(_MSC_VER) && defined(__ICL) && __ICL <= 900 +# pragma warning(push) +# pragma warning(disable:473) // intel/win32 does too +#endif + + return extract(x); + +#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300 || defined(_MSC_VER) && defined(__ICL) && __ICL <= 800 +# pragma warning(pop) +#endif +} + +X extract_X(object x) +{ + return extract(x); +} + +X* extract_X_ptr(object x) { return extract(x); } + +X& extract_X_ref(object x) +{ + extract get_x(x); + return get_x; +} + +int double_X(object n) +{ + extract x(n); + return x().value() + x().value(); +} + +bool check_bool(object x) { return extract(x).check(); } +bool check_list(object x) { return extract(x).check(); } +bool check_cstring(object x) { return extract(x).check(); } +bool check_string(object x) { return extract(x).check(); } +bool check_string_cref(object x) { return extract(x).check(); } +bool check_X(object x) { return extract(x).check(); } +bool check_X_ptr(object x) { return extract(x).check(); } +bool check_X_ref(object x) { return extract(x).check(); } + +std::string x_rep(X const& x) +{ + return "X(" + boost::lexical_cast(x.value()) + ")"; +} + +BOOST_PYTHON_MODULE(extract_ext) +{ + implicitly_convertible(); + + def("extract_bool", extract_bool); + def("extract_list", extract_list); + def("extract_cstring", extract_cstring); + def("extract_string", extract_string); + def("extract_string_cref", extract_string_cref, return_value_policy()); + def("extract_X", extract_X); + def("extract_X_ptr", extract_X_ptr, return_value_policy()); + def("extract_X_ref", extract_X_ref, return_value_policy()); + + def("check_bool", check_bool); + def("check_list", check_list); + def("check_cstring", check_cstring); + def("check_string", check_string); + def("check_string_cref", check_string_cref); + def("check_X", check_X); + def("check_X_ptr", check_X_ptr); + def("check_X_ref", check_X_ref); + + def("double_X", double_X); + + def("count_Xs", &X::count); + ; + + object x_class( + class_("X", init()) + .def( "__repr__", x_rep)); + + // Instantiate an X object through the Python interface + object x_obj = x_class(3); + + // Get the C++ object out of the Python object + X const& x = extract(x_obj); + if (x.value() != 3) { + throw std::runtime_error("x.value() == 3 failure."); + } +} + + +#include "module_tail.cpp" + -- cgit v1.2.3