From bed7a7d29c63bc3b6a0842f5f0e771cb269a072e Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Mon, 1 Jul 2002 21:25:01 +0000 Subject: [PATCH] Python long support [SVN r14271] --- Jamfile | 1 + include/boost/python/long.hpp | 112 +++++++++++++++++++++++++++ include/boost/python/object_core.hpp | 20 ++--- src/long.cpp | 40 ++++++++++ test/Jamfile | 1 + test/long.cpp | 51 ++++++++++++ test/long.py | 26 +++++++ 7 files changed, 241 insertions(+), 10 deletions(-) create mode 100644 include/boost/python/long.hpp create mode 100644 src/long.cpp create mode 100644 test/long.cpp create mode 100644 test/long.py diff --git a/Jamfile b/Jamfile index 5ebd1f0a..b28cf262 100644 --- a/Jamfile +++ b/Jamfile @@ -14,6 +14,7 @@ if $(UNIX) && ( $(OS) = AIX ) dll bpl : src/list.cpp + src/long.cpp src/aix_init_module.cpp src/converter/from_python.cpp src/converter/registry.cpp diff --git a/include/boost/python/long.hpp b/include/boost/python/long.hpp new file mode 100644 index 00000000..5d39093e --- /dev/null +++ b/include/boost/python/long.hpp @@ -0,0 +1,112 @@ +// Copyright David Abrahams 2002. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. +#ifndef LONG_DWA2002627_HPP +# define LONG_DWA2002627_HPP + +# include +# include + +namespace boost { namespace python { + +class long_ : public object +{ + public: + BOOST_PYTHON_DECL long_(); // new long_ + explicit BOOST_PYTHON_DECL long_(object_cref rhs); + + template + explicit long_(T const& rhs) + : object(long_::call(object(rhs))) + { + } + + explicit BOOST_PYTHON_DECL long_(object_cref rhs, object_cref base); + + template + explicit long_(T const& rhs, U const& base) + : object(long_::call(object(rhs), object(base))) + { + } + public: // implementation detail -- for internal use only + explicit long_(detail::borrowed_reference); + explicit long_(detail::new_reference); + + private: + static BOOST_PYTHON_DECL detail::new_reference call(object const&); + static BOOST_PYTHON_DECL detail::new_reference call(object const&, object const&); +}; + +// +// Converter Specializations +// +template struct arg_from_python; + +template <> +struct arg_from_python + : converter::pytype_wrapper_value_arg_from_python +{ + typedef converter::pytype_wrapper_value_arg_from_python base; + typedef long_ result_type; + + arg_from_python(PyObject* p) : base(p) {} +}; + +template <> +struct arg_from_python + : arg_from_python +{ + arg_from_python(PyObject* p) + : arg_from_python(p) {} +}; + +template <> +struct arg_from_python + : converter::pytype_wrapper_ref_arg_from_python +{ + typedef converter::pytype_wrapper_ref_arg_from_python base; + typedef long_ result_type; + + arg_from_python(PyObject* p) + : base(p) {} +}; + +namespace converter +{ + template struct is_object_manager; + + template <> + struct is_object_manager + { + BOOST_STATIC_CONSTANT(bool, value = true); + }; + + template struct return_from_python; + template <> + struct return_from_python + { + typedef long_ result_type; + + result_type operator()(PyObject* x) const + { + return long_(python::detail::new_reference(x)); + } + }; +} + +// +// long_ implementation +// +inline long_::long_(detail::borrowed_reference p) + : object(p) +{} + +inline long_::long_(detail::new_reference p) + : object(p) +{} + +}} // namespace boost::python + +#endif // LONG_DWA2002627_HPP diff --git a/include/boost/python/object_core.hpp b/include/boost/python/object_core.hpp index d0228cc9..a465bd81 100755 --- a/include/boost/python/object_core.hpp +++ b/include/boost/python/object_core.hpp @@ -88,21 +88,12 @@ namespace api template class object_operators { + protected: # if !defined(BOOST_MSVC) || BOOST_MSVC > 1200 typedef object const& object_cref; # else typedef object object_cref; # endif - - // there is a confirmed CWPro8 codegen bug here. We prevent the - // early destruction of a temporary by binding a named object - // instead. -# if __MWERKS__ != 0x3000 - typedef object const& object_cref2; -# else - typedef object const object_cref2; -# endif - public: // function call // @@ -201,6 +192,15 @@ namespace api slice_bound::type(start) , slice_bound::type(end)); } +# endif + private: + // there is a confirmed CWPro8 codegen bug here. We prevent the + // early destruction of a temporary by binding a named object + // instead. +# if __MWERKS__ != 0x3000 + typedef object const& object_cref2; +# else + typedef object const object_cref2; # endif }; diff --git a/src/long.cpp b/src/long.cpp new file mode 100644 index 00000000..46d3d7c5 --- /dev/null +++ b/src/long.cpp @@ -0,0 +1,40 @@ +// Copyright David Abrahams 2002. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. +#include + +namespace boost { namespace python { + +BOOST_PYTHON_DECL detail::new_reference long_::call(object const& arg) +{ + return (detail::new_reference)PyObject_CallFunction( + (PyObject*)&PyLong_Type, "(O)", + arg.ptr()); +} + +BOOST_PYTHON_DECL detail::new_reference long_::call(object const& arg, object const& base) +{ + return (detail::new_reference)PyObject_CallFunction( + (PyObject*)&PyLong_Type, "(OO)", + arg.ptr(), base.ptr()); +} + +BOOST_PYTHON_DECL long_::long_() + : object( + detail::new_reference( + PyObject_CallFunction((PyObject*)&PyLong_Type, "()")) + ) +{} + +BOOST_PYTHON_DECL long_::long_(object_cref arg) + : object(long_::call(arg)) +{} + +BOOST_PYTHON_DECL long_::long_(object_cref arg, object_cref base) + : object(long_::call(arg, base)) +{} + + +}} // namespace boost::python diff --git a/test/Jamfile b/test/Jamfile index 6fe1fc61..5d0118d3 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -63,6 +63,7 @@ bpl-test operators ; bpl-test callbacks ; bpl-test object ; bpl-test list ; +bpl-test long ; bpl-test virtual_functions ; bpl-test back_reference ; bpl-test implicit ; diff --git a/test/long.cpp b/test/long.cpp new file mode 100644 index 00000000..2a139de8 --- /dev/null +++ b/test/long.cpp @@ -0,0 +1,51 @@ +// Copyright David Abrahams 2002. Permission to copy, use, +// modify, sell and distribute this software is granted provided this +// copyright notice appears in all copies. This software is provided +// "as is" without express or implied warranty, and with no claim as +// to its suitability for any purpose. + +#include +#include +#include + +using namespace boost::python; + +object new_long() +{ + return long_(); +} + +long_ longify(object x) +{ + return long_(x); +} + +object longify_string(char const* s) +{ + return long_(s); +} + +char const* is_long1(long_& x) +{ + long_ y = x; + x += 50; + assert(x == y + 50); + return "yes"; +} + +int is_long2(char const*) +{ + return 0; +} + +BOOST_PYTHON_MODULE_INIT(long_ext) +{ + module("long_ext") + .def("new_long", new_long) + .def("longify", longify) + .def("longify_string", longify_string) + .def("is_long", is_long1) + .def("is_long", is_long2) + ; +} + diff --git a/test/long.py b/test/long.py new file mode 100644 index 00000000..7d08e9b7 --- /dev/null +++ b/test/long.py @@ -0,0 +1,26 @@ +''' +>>> from long_ext import * +>>> new_long() +0L +>>> longify(42) +42L +>>> longify_string('300') +300L +>>> is_long(20L) +'yes' +>>> is_long('20') +0 +''' + +def run(args = None): + import sys + import doctest + + if args is not None: + sys.argv = args + return doctest.testmod(sys.modules.get(__name__)) + +if __name__ == '__main__': + print "running..." + import sys + sys.exit(run()[0])