2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-26 06:42:27 +00:00

Python long support

[SVN r14271]
This commit is contained in:
Dave Abrahams
2002-07-01 21:25:01 +00:00
parent f02a3c5b47
commit bed7a7d29c
7 changed files with 241 additions and 10 deletions

View File

@@ -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

View File

@@ -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 <boost/python/object.hpp>
# include <boost/python/converter/pytype_arg_from_python.hpp>
namespace boost { namespace python {
class long_ : public object
{
public:
BOOST_PYTHON_DECL long_(); // new long_
explicit BOOST_PYTHON_DECL long_(object_cref rhs);
template <class T>
explicit long_(T const& rhs)
: object(long_::call(object(rhs)))
{
}
explicit BOOST_PYTHON_DECL long_(object_cref rhs, object_cref base);
template <class T, class U>
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 <class T> struct arg_from_python;
template <>
struct arg_from_python<long_>
: converter::pytype_wrapper_value_arg_from_python<long_, &PyLong_Type>
{
typedef converter::pytype_wrapper_value_arg_from_python<long_, &PyLong_Type> base;
typedef long_ result_type;
arg_from_python(PyObject* p) : base(p) {}
};
template <>
struct arg_from_python<long_ const&>
: arg_from_python<long_>
{
arg_from_python(PyObject* p)
: arg_from_python<long_>(p) {}
};
template <>
struct arg_from_python<long_&>
: converter::pytype_wrapper_ref_arg_from_python<long_, &PyLong_Type>
{
typedef converter::pytype_wrapper_ref_arg_from_python<long_, &PyLong_Type> base;
typedef long_ result_type;
arg_from_python(PyObject* p)
: base(p) {}
};
namespace converter
{
template <class T> struct is_object_manager;
template <>
struct is_object_manager<long_>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
template <class T> struct return_from_python;
template <>
struct return_from_python<long_>
{
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

View File

@@ -88,21 +88,12 @@ namespace api
template <class U>
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<T>::type(start)
, slice_bound<V>::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
};

40
src/long.cpp Normal file
View File

@@ -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 <boost/python/long.hpp>
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

View File

@@ -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 ;

51
test/long.cpp Normal file
View File

@@ -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 <boost/python/module.hpp>
#include <boost/python/long.hpp>
#include <cassert>
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)
;
}

26
test/long.py Normal file
View File

@@ -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])