From 7732b390b4bebee6aeaabc3c618f21cb062a35a8 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 5 Mar 2001 20:01:01 +0000 Subject: [PATCH] Join ralf_grosse_kunstleve with HEAD [SVN r9444] --- include/boost/python/conversions.hpp | 83 ++++++++++++++++----- include/boost/python/detail/base_object.hpp | 6 +- 2 files changed, 67 insertions(+), 22 deletions(-) diff --git a/include/boost/python/conversions.hpp b/include/boost/python/conversions.hpp index c72260ac..47f80989 100644 --- a/include/boost/python/conversions.hpp +++ b/include/boost/python/conversions.hpp @@ -5,6 +5,11 @@ // // The author gratefully acknowleges the support of Dragon Systems, Inc., in // producing this work. +// +// Revision History: +// 04 Mar 01 Fixed std::complex<> stuff to work with MSVC (David Abrahams) +// 03 Mar 01 added: converters for [plain] char and std::complex +// (Ralf W. Grosse-Kunstleve) #ifndef METHOD_DWA122899_H_ # define METHOD_DWA122899_H_ @@ -16,8 +21,18 @@ # include # include # include + +# ifdef BOOST_MSVC6_OR_EARLIER +# pragma warning(push) +# pragma warning(disable:4275) // disable a bogus warning caused by +# endif + # include +# ifdef BOOST_MSVC6_OR_EARLIER +# pragma warning(pop) +# endif + BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug workaround // This can be instantiated on an enum to provide the to_python/from_python @@ -71,6 +86,30 @@ inline void xdecref(T* p) xdecref_impl(reinterpret_cast(p_base)); } +namespace detail { + + void expect_complex(PyObject*); + + template + std::complex complex_from_python(PyObject* p, boost::python::type) + { + expect_complex(p); + + return std::complex( + static_cast(PyComplex_RealAsDouble(p)), + static_cast(PyComplex_ImagAsDouble(p))); + } + + template + PyObject* complex_to_python(const std::complex& sc) { + Py_complex pcc; + pcc.real = sc.real(); + pcc.imag = sc.imag(); + return PyComplex_FromCComplex(pcc); + } + +} + }} // namespace boost::python BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE @@ -135,30 +174,34 @@ PyObject* to_python(const std::string& s); std::string from_python(PyObject*, boost::python::type); std::string from_python(PyObject*, boost::python::type); -template -PyObject* to_python(const std::complex& sc) { - Py_complex pcc; - pcc.real = sc.real(); - pcc.imag = sc.imag(); - return PyComplex_FromCComplex(pcc); +inline PyObject* to_python(const std::complex& x) +{ + return boost::python::detail::complex_to_python(x); } -template -std::complex from_python(PyObject* p, - boost::python::type&>) { - if (! PyComplex_Check(p)) { - PyErr_SetString(PyExc_TypeError, "expected a complex number"); - throw boost::python::argument_error(); - } - return std::complex( - static_cast(PyComplex_RealAsDouble(p)), - static_cast(PyComplex_ImagAsDouble(p))); +inline PyObject* to_python(const std::complex& x) +{ + return boost::python::detail::complex_to_python(x); } -template -inline std::complex from_python(PyObject* p, - boost::python::type >) { - return from_python(p, boost::python::type&>()); +inline std::complex from_python(PyObject* p, + boost::python::type >) { + return boost::python::detail::complex_from_python(p, boost::python::type()); +} + +inline std::complex from_python(PyObject* p, + boost::python::type&>) { + return boost::python::detail::complex_from_python(p, boost::python::type()); +} + +inline std::complex from_python(PyObject* p, + boost::python::type >) { + return boost::python::detail::complex_from_python(p, boost::python::type()); +} + +inline std::complex from_python(PyObject* p, + boost::python::type&>) { + return boost::python::detail::complex_from_python(p, boost::python::type()); } // For when your C++ function really wants to pass/return a PyObject* diff --git a/include/boost/python/detail/base_object.hpp b/include/boost/python/detail/base_object.hpp index f8ed665b..bf0faa7b 100644 --- a/include/boost/python/detail/base_object.hpp +++ b/include/boost/python/detail/base_object.hpp @@ -5,6 +5,9 @@ // // The author gratefully acknowleges the support of Dragon Systems, Inc., in // producing this work. +// +// Revision History: +// Mar 01 01 Use PyObject_INIT() instead of trying to hand-initialize (David Abrahams) #ifndef BASE_OBJECT_DWA051600_H_ # define BASE_OBJECT_DWA051600_H_ @@ -46,9 +49,8 @@ base_object::base_object(PyTypeObject* type_obj) std:: #endif memset(bp, 0, sizeof(base_python_type)); - ob_refcnt = 1; - ob_type = type_obj; Py_INCREF(type_obj); + PyObject_INIT(bp, type_obj); } template