2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00

str(), pow(), complex() support

[SVN r14070]
This commit is contained in:
Dave Abrahams
2002-06-02 18:35:09 +00:00
parent b042644c85
commit 92aae63af2
5 changed files with 110 additions and 6 deletions

View File

@@ -46,7 +46,8 @@ enum operator_id
op_irshift,
op_iand,
op_ixor,
op_ior
op_ior,
op_complex,
};
}}} // namespace boost::python::detail

View File

@@ -13,6 +13,9 @@
# include <boost/mpl/select_type.hpp>
# include <boost/python/self.hpp>
# include <boost/python/other.hpp>
# include <boost/lexical_cast.hpp>
# include <string>
# include <complex>
namespace boost { namespace python {
@@ -132,7 +135,7 @@ namespace detail
};
}
# define BOOST_PYTHON_BINARY_OPERATOR(id, rid, op) \
# define BOOST_PYTHON_BINARY_OPERATION(id, rid, expr) \
namespace detail \
{ \
template <> \
@@ -143,7 +146,7 @@ namespace detail \
{ \
static inline PyObject* execute(L const& l, R const& r) \
{ \
return detail::convert_result(l op r); \
return detail::convert_result(expr); \
} \
}; \
static char const* name() { return "__" #id "__"; } \
@@ -157,12 +160,15 @@ namespace detail \
{ \
static inline PyObject* execute(R const& r, L const& l) \
{ \
return detail::convert_result(l op r); \
return detail::convert_result(expr); \
} \
}; \
static char const* name() { return "__" #rid "__"; } \
}; \
} \
}
# define BOOST_PYTHON_BINARY_OPERATOR(id, rid, op) \
BOOST_PYTHON_BINARY_OPERATION(id, rid, l op r) \
namespace self_ns \
{ \
template <class L, class R> \
@@ -190,6 +196,44 @@ BOOST_PYTHON_BINARY_OPERATOR(le, ge, <=)
BOOST_PYTHON_BINARY_OPERATOR(eq, eq, ==)
BOOST_PYTHON_BINARY_OPERATOR(ne, ne, !=)
// pow isn't an operator in C++; handle it specially.
BOOST_PYTHON_BINARY_OPERATION(pow, rpow, pow(l,r))
namespace self_ns
{
# ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
template <class L, class R>
inline detail::operator_<detail::op_pow,L,R>
pow(L const&, R const&)
{
return detail::operator_<detail::op_pow,L,R>();
}
# else
// When there's no argument-dependent lookup, we need these
// overloads to handle the case when everything is imported into the
// global namespace. Note that the plain overload below does /not/
// take const& arguments. This is needed by MSVC6 at least, or it
// complains of ambiguities, since there's no partial ordering.
inline detail::operator_<detail::op_pow,self_t,self_t>
pow(self_t, self_t)
{
return detail::operator_<detail::op_pow,self_t,self_t>();
}
template <class R>
inline detail::operator_<detail::op_pow,self_t,R>
pow(self_t const&, R const&)
{
return detail::operator_<detail::op_pow,self_t,R>();
}
template <class L>
inline detail::operator_<detail::op_pow,L,self_t>
pow(L const&, self_t const&)
{
return detail::operator_<detail::op_pow,L,self_t>();
}
# endif
}
# define BOOST_PYTHON_INPLACE_OPERATOR(id, op) \
namespace detail \
{ \
@@ -263,6 +307,8 @@ BOOST_PYTHON_UNARY_OPERATOR(invert, ~, operator~)
BOOST_PYTHON_UNARY_OPERATOR(int, long, int_)
BOOST_PYTHON_UNARY_OPERATOR(long, PyLong_FromLong, long_)
BOOST_PYTHON_UNARY_OPERATOR(float, double, float_)
BOOST_PYTHON_UNARY_OPERATOR(complex, std::complex<double>, complex_)
BOOST_PYTHON_UNARY_OPERATOR(str, lexical_cast<std::string>, str)
}} // namespace boost::python
@@ -271,6 +317,9 @@ using boost::python::self_ns::abs;
using boost::python::self_ns::int_;
using boost::python::self_ns::long_;
using boost::python::self_ns::float_;
using boost::python::self_ns::complex_;
using boost::python::self_ns::str;
using boost::python::self_ns::pow;
# endif
#endif // OPERATORS2_DWA2002530_HPP

View File

@@ -86,7 +86,9 @@ namespace
"add__",
"and__",
"div__",
"divmod__",
"eq__",
"floordiv__",
"ge__",
"gt__",
"le__",
@@ -96,19 +98,25 @@ namespace
"mul__",
"ne__",
"or__",
"pow__",
"radd__",
"rand__",
"rdiv__",
"rdivmod__",
"rfloordiv__",
"rlshift__",
"rmod__",
"rmul__",
"ror__",
"rpow__",
"rrshift__",
"rshift__",
"rsub__",
"rtruediv__",
"rxor__",
"sub__",
"xor__",
"truediv__",
"xor__"
};
struct less_cstring

View File

@@ -8,6 +8,15 @@
#include <boost/python/class.hpp>
#include <boost/python/module.hpp>
#include "test_class.hpp"
#if __GNUC__ != 2
# include <ostream>
#else
# include <ostream.h>
#endif
// Just use math.h here; trying to use std::pow() causes too much
// trouble for non-conforming compilers and libraries.
#include <math.h>
using namespace boost::python;
@@ -27,6 +36,26 @@ bool operator<(int x, X const& y) { return x < y.value(); }
X abs(X x) { return X(x.value() < 0 ? -x.value() : x.value()); }
X pow(X x, int y)
{
return X(int(pow(double(x.value()), y)));
}
X pow(X x, X y)
{
return X(int(pow(double(x.value()), y.value())));
}
int pow(int x, X y)
{
return int(pow(double(x), y.value()));
}
std::ostream& operator<<(std::ostream& s, X const& x)
{
return s << x.value();
}
BOOST_PYTHON_MODULE_INIT(operators_ext)
{
module("operators_ext")
@@ -43,12 +72,18 @@ BOOST_PYTHON_MODULE_INIT(operators_ext)
.def(1 < self)
.def(self -= self)
.def(abs(self))
.def(str(self))
.def(pow(self,self))
.def(pow(self,int()))
.def(pow(int(),self))
)
.add(
class_<test_class<1> >("Z")
.def_init(args<int>())
.def(int_(self))
.def(float_(self))
.def(complex_(self))
)
;
}

View File

@@ -53,12 +53,23 @@
>>> x -= y
>>> x.value()
5
>>> str(x)
'5'
>>> z = Z(10)
>>> int(z)
10
>>> float(z)
10.0
>>> complex(z)
(10+0j)
>>> pow(2,x)
32
>>> pow(x,2).value()
25
>>> pow(X(2),x).value()
32
'''
def run(args = None):