Skip to content

Commit bed7a7d

Browse files
committed
Python long support
[SVN r14271]
1 parent f02a3c5 commit bed7a7d

7 files changed

Lines changed: 241 additions & 10 deletions

File tree

Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ if $(UNIX) && ( $(OS) = AIX )
1414
dll bpl
1515
:
1616
src/list.cpp
17+
src/long.cpp
1718
src/aix_init_module.cpp
1819
src/converter/from_python.cpp
1920
src/converter/registry.cpp

include/boost/python/long.hpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#ifndef LONG_DWA2002627_HPP
7+
# define LONG_DWA2002627_HPP
8+
9+
# include <boost/python/object.hpp>
10+
# include <boost/python/converter/pytype_arg_from_python.hpp>
11+
12+
namespace boost { namespace python {
13+
14+
class long_ : public object
15+
{
16+
public:
17+
BOOST_PYTHON_DECL long_(); // new long_
18+
explicit BOOST_PYTHON_DECL long_(object_cref rhs);
19+
20+
template <class T>
21+
explicit long_(T const& rhs)
22+
: object(long_::call(object(rhs)))
23+
{
24+
}
25+
26+
explicit BOOST_PYTHON_DECL long_(object_cref rhs, object_cref base);
27+
28+
template <class T, class U>
29+
explicit long_(T const& rhs, U const& base)
30+
: object(long_::call(object(rhs), object(base)))
31+
{
32+
}
33+
public: // implementation detail -- for internal use only
34+
explicit long_(detail::borrowed_reference);
35+
explicit long_(detail::new_reference);
36+
37+
private:
38+
static BOOST_PYTHON_DECL detail::new_reference call(object const&);
39+
static BOOST_PYTHON_DECL detail::new_reference call(object const&, object const&);
40+
};
41+
42+
//
43+
// Converter Specializations
44+
//
45+
template <class T> struct arg_from_python;
46+
47+
template <>
48+
struct arg_from_python<long_>
49+
: converter::pytype_wrapper_value_arg_from_python<long_, &PyLong_Type>
50+
{
51+
typedef converter::pytype_wrapper_value_arg_from_python<long_, &PyLong_Type> base;
52+
typedef long_ result_type;
53+
54+
arg_from_python(PyObject* p) : base(p) {}
55+
};
56+
57+
template <>
58+
struct arg_from_python<long_ const&>
59+
: arg_from_python<long_>
60+
{
61+
arg_from_python(PyObject* p)
62+
: arg_from_python<long_>(p) {}
63+
};
64+
65+
template <>
66+
struct arg_from_python<long_&>
67+
: converter::pytype_wrapper_ref_arg_from_python<long_, &PyLong_Type>
68+
{
69+
typedef converter::pytype_wrapper_ref_arg_from_python<long_, &PyLong_Type> base;
70+
typedef long_ result_type;
71+
72+
arg_from_python(PyObject* p)
73+
: base(p) {}
74+
};
75+
76+
namespace converter
77+
{
78+
template <class T> struct is_object_manager;
79+
80+
template <>
81+
struct is_object_manager<long_>
82+
{
83+
BOOST_STATIC_CONSTANT(bool, value = true);
84+
};
85+
86+
template <class T> struct return_from_python;
87+
template <>
88+
struct return_from_python<long_>
89+
{
90+
typedef long_ result_type;
91+
92+
result_type operator()(PyObject* x) const
93+
{
94+
return long_(python::detail::new_reference(x));
95+
}
96+
};
97+
}
98+
99+
//
100+
// long_ implementation
101+
//
102+
inline long_::long_(detail::borrowed_reference p)
103+
: object(p)
104+
{}
105+
106+
inline long_::long_(detail::new_reference p)
107+
: object(p)
108+
{}
109+
110+
}} // namespace boost::python
111+
112+
#endif // LONG_DWA2002627_HPP

include/boost/python/object_core.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,12 @@ namespace api
8888
template <class U>
8989
class object_operators
9090
{
91+
protected:
9192
# if !defined(BOOST_MSVC) || BOOST_MSVC > 1200
9293
typedef object const& object_cref;
9394
# else
9495
typedef object object_cref;
9596
# endif
96-
97-
// there is a confirmed CWPro8 codegen bug here. We prevent the
98-
// early destruction of a temporary by binding a named object
99-
// instead.
100-
# if __MWERKS__ != 0x3000
101-
typedef object const& object_cref2;
102-
# else
103-
typedef object const object_cref2;
104-
# endif
105-
10697
public:
10798
// function call
10899
//
@@ -201,6 +192,15 @@ namespace api
201192
slice_bound<T>::type(start)
202193
, slice_bound<V>::type(end));
203194
}
195+
# endif
196+
private:
197+
// there is a confirmed CWPro8 codegen bug here. We prevent the
198+
// early destruction of a temporary by binding a named object
199+
// instead.
200+
# if __MWERKS__ != 0x3000
201+
typedef object const& object_cref2;
202+
# else
203+
typedef object const object_cref2;
204204
# endif
205205
};
206206

src/long.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
#include <boost/python/long.hpp>
7+
8+
namespace boost { namespace python {
9+
10+
BOOST_PYTHON_DECL detail::new_reference long_::call(object const& arg)
11+
{
12+
return (detail::new_reference)PyObject_CallFunction(
13+
(PyObject*)&PyLong_Type, "(O)",
14+
arg.ptr());
15+
}
16+
17+
BOOST_PYTHON_DECL detail::new_reference long_::call(object const& arg, object const& base)
18+
{
19+
return (detail::new_reference)PyObject_CallFunction(
20+
(PyObject*)&PyLong_Type, "(OO)",
21+
arg.ptr(), base.ptr());
22+
}
23+
24+
BOOST_PYTHON_DECL long_::long_()
25+
: object(
26+
detail::new_reference(
27+
PyObject_CallFunction((PyObject*)&PyLong_Type, "()"))
28+
)
29+
{}
30+
31+
BOOST_PYTHON_DECL long_::long_(object_cref arg)
32+
: object(long_::call(arg))
33+
{}
34+
35+
BOOST_PYTHON_DECL long_::long_(object_cref arg, object_cref base)
36+
: object(long_::call(arg, base))
37+
{}
38+
39+
40+
}} // namespace boost::python

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ bpl-test operators ;
6363
bpl-test callbacks ;
6464
bpl-test object ;
6565
bpl-test list ;
66+
bpl-test long ;
6667
bpl-test virtual_functions ;
6768
bpl-test back_reference ;
6869
bpl-test implicit ;

test/long.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright David Abrahams 2002. Permission to copy, use,
2+
// modify, sell and distribute this software is granted provided this
3+
// copyright notice appears in all copies. This software is provided
4+
// "as is" without express or implied warranty, and with no claim as
5+
// to its suitability for any purpose.
6+
7+
#include <boost/python/module.hpp>
8+
#include <boost/python/long.hpp>
9+
#include <cassert>
10+
11+
using namespace boost::python;
12+
13+
object new_long()
14+
{
15+
return long_();
16+
}
17+
18+
long_ longify(object x)
19+
{
20+
return long_(x);
21+
}
22+
23+
object longify_string(char const* s)
24+
{
25+
return long_(s);
26+
}
27+
28+
char const* is_long1(long_& x)
29+
{
30+
long_ y = x;
31+
x += 50;
32+
assert(x == y + 50);
33+
return "yes";
34+
}
35+
36+
int is_long2(char const*)
37+
{
38+
return 0;
39+
}
40+
41+
BOOST_PYTHON_MODULE_INIT(long_ext)
42+
{
43+
module("long_ext")
44+
.def("new_long", new_long)
45+
.def("longify", longify)
46+
.def("longify_string", longify_string)
47+
.def("is_long", is_long1)
48+
.def("is_long", is_long2)
49+
;
50+
}
51+

test/long.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
>>> from long_ext import *
3+
>>> new_long()
4+
0L
5+
>>> longify(42)
6+
42L
7+
>>> longify_string('300')
8+
300L
9+
>>> is_long(20L)
10+
'yes'
11+
>>> is_long('20')
12+
0
13+
'''
14+
15+
def run(args = None):
16+
import sys
17+
import doctest
18+
19+
if args is not None:
20+
sys.argv = args
21+
return doctest.testmod(sys.modules.get(__name__))
22+
23+
if __name__ == '__main__':
24+
print "running..."
25+
import sys
26+
sys.exit(run()[0])

0 commit comments

Comments
 (0)