Skip to content

Commit caa9cb8

Browse files
author
Ralf W. Grosse-Kunstleve
committed
Python 2.5 compatibility
[SVN r34017]
1 parent 66ac614 commit caa9cb8

File tree

9 files changed

+45
-19
lines changed

9 files changed

+45
-19
lines changed

include/boost/python/converter/builtin_converters.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned BOOST_PYTHON_LONG_LONG, ::PyLong_FromUn
115115

116116
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x))
117117
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x))
118-
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<int>(x.size())))
118+
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<Py_ssize_t>(x.size())))
119119
#if defined(Py_USING_UNICODE) && !defined(BOOST_NO_STD_WSTRING)
120-
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<int>(x.size())))
120+
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<Py_ssize_t>(x.size())))
121121
# endif
122122
BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, ::PyFloat_FromDouble(x))
123123
BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, ::PyFloat_FromDouble(x))

include/boost/python/detail/wrap_python.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ typedef int pid_t;
141141
# include <Python.h>
142142
#endif
143143

144+
#if PY_VERSION_HEX < 0x02050000
145+
typedef int Py_ssize_t;
146+
#define PY_SSIZE_T_MIN INT_MIN
147+
#define PY_SSIZE_T_MAX INT_MAX
148+
#endif
149+
144150
#ifdef BOOST_PYTHON_ULONG_MAX_UNDEFINED
145151
# undef ULONG_MAX
146152
# undef BOOST_PYTHON_ULONG_MAX_UNDEFINED

include/boost/python/list.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ namespace detail
2424

2525
long index(object_cref value) const; // return index of first occurrence of value
2626

27-
void insert(int index, object_cref); // insert object before index
27+
void insert(Py_ssize_t index, object_cref); // insert object before index
2828
void insert(object const& index, object_cref);
2929

3030
object pop(); // remove and return item at index (default last)
31-
object pop(long index);
31+
object pop(Py_ssize_t index);
3232
object pop(object const& index);
3333

3434
void remove(object_cref value); // remove first occurrence of value
@@ -86,7 +86,7 @@ class list : public detail::list_base
8686
}
8787

8888
template <class T>
89-
void insert(int index, T const& x) // insert object before index
89+
void insert(Py_ssize_t index, T const& x) // insert object before index
9090
{
9191
base::insert(index, object(x));
9292
}
@@ -98,7 +98,7 @@ class list : public detail::list_base
9898
}
9999

100100
object pop() { return base::pop(); }
101-
object pop(long index) { return base::pop(index); }
101+
object pop(Py_ssize_t index) { return base::pop(index); }
102102

103103
template <class T>
104104
object pop(T const& index)

include/boost/python/object.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
namespace boost { namespace python {
1717

18-
inline long len(object const& obj)
18+
inline Py_ssize_t len(object const& obj)
1919
{
20-
long result = PyObject_Length(obj.ptr());
20+
Py_ssize_t result = PyObject_Length(obj.ptr());
2121
if (PyErr_Occurred()) throw_error_already_set();
2222
return result;
2323
}

src/list.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ long list_base::index(object_cref value) const
5353
return result;
5454
}
5555

56-
void list_base::insert(int index, object_cref item)
56+
void list_base::insert(Py_ssize_t index, object_cref item)
5757
{
5858
if (PyList_CheckExact(this->ptr()))
5959
{
@@ -79,7 +79,7 @@ object list_base::pop()
7979
return this->attr("pop")();
8080
}
8181

82-
object list_base::pop(long index)
82+
object list_base::pop(Py_ssize_t index)
8383
{
8484
return this->pop(object(index));
8585
}

src/object/class.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,14 @@ namespace objects
506506
// were declared, we'll use our class_type() as the single base
507507
// class.
508508
std::size_t const num_bases = (std::max)(num_types - 1, static_cast<std::size_t>(1));
509-
handle<> bases(PyTuple_New(num_bases));
509+
assert(num_bases <= PY_SSIZE_T_MAX);
510+
handle<> bases(PyTuple_New(static_cast<Py_ssize_t>(num_bases)));
510511

511512
for (std::size_t i = 1; i <= num_bases; ++i)
512513
{
513514
type_handle c = (i >= num_types) ? class_type() : get_class(types[i]);
514515
// PyTuple_SET_ITEM steals this reference
515-
PyTuple_SET_ITEM(bases.get(), i - 1, upcast<PyObject>(c.release()));
516+
PyTuple_SET_ITEM(bases.get(), static_cast<Py_ssize_t>(i - 1), upcast<PyObject>(c.release()));
516517
}
517518

518519
// Call the class metatype to create a new class

src/object/function.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function::function(
6464
= max_arity > num_keywords ? max_arity - num_keywords : 0;
6565

6666

67-
unsigned tuple_size = num_keywords ? max_arity : 0;
67+
Py_ssize_t tuple_size = num_keywords ? max_arity : 0;
6868
m_arg_names = object(handle<>(PyTuple_New(tuple_size)));
6969

7070
if (num_keywords != 0)
@@ -158,7 +158,9 @@ PyObject* function::call(PyObject* args, PyObject* keywords) const
158158
else
159159
{
160160
// build a new arg tuple, will adjust its size later
161-
inner_args = handle<>(PyTuple_New(max_arity));
161+
assert(max_arity <= PY_SSIZE_T_MAX);
162+
inner_args = handle<>(
163+
PyTuple_New(static_cast<Py_ssize_t>(max_arity)));
162164

163165
// Fill in the positional arguments
164166
for (std::size_t i = 0; i < n_unnamed_actual; ++i)
@@ -293,7 +295,7 @@ void function::argument_error(PyObject* args, PyObject* /*keywords*/) const
293295
% make_tuple(this->m_namespace, this->m_name);
294296

295297
list actual_args;
296-
for (int i = 0; i < PyTuple_Size(args); ++i)
298+
for (Py_ssize_t i = 0; i < PyTuple_Size(args); ++i)
297299
{
298300
char const* name = PyTuple_GetItem(args, i)->ob_type->tp_name;
299301
actual_args.append(str(name));

src/object_protocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ namespace // slicing code copied directly out of the Python implementation
106106
PySequenceMethods *sq = tp->tp_as_sequence;
107107

108108
if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
109-
int ilow = 0, ihigh = INT_MAX;
109+
Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
110110
if (!_PyEval_SliceIndex(v, &ilow))
111111
return NULL;
112112
if (!_PyEval_SliceIndex(w, &ihigh))
@@ -133,7 +133,7 @@ namespace // slicing code copied directly out of the Python implementation
133133
PySequenceMethods *sq = tp->tp_as_sequence;
134134

135135
if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
136-
int ilow = 0, ihigh = INT_MAX;
136+
Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
137137
if (!_PyEval_SliceIndex(v, &ilow))
138138
return -1;
139139
if (!_PyEval_SliceIndex(w, &ihigh))

src/str.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,35 @@ str_base::str_base(const char* s)
2121
: object(detail::new_reference(::PyString_FromString(s)))
2222
{}
2323

24+
namespace {
25+
26+
Py_ssize_t str_size_as_py_ssize_t(std::size_t n)
27+
{
28+
if (n > PY_SSIZE_T_MAX)
29+
{
30+
throw std::range_error("str size > PY_SSIZE_T_MAX");
31+
}
32+
return static_cast<Py_ssize_t>(n);
33+
}
34+
35+
} // namespace <anonymous>
36+
2437
str_base::str_base(char const* start, char const* finish)
2538
: object(
2639
detail::new_reference(
27-
::PyString_FromStringAndSize(start, finish - start)
40+
::PyString_FromStringAndSize(
41+
start, str_size_as_py_ssize_t(finish - start)
42+
)
2843
)
2944
)
3045
{}
3146

3247
str_base::str_base(char const* start, std::size_t length) // new str
3348
: object(
3449
detail::new_reference(
35-
::PyString_FromStringAndSize(start, length)
50+
::PyString_FromStringAndSize(
51+
start, str_size_as_py_ssize_t(length)
52+
)
3653
)
3754
)
3855
{}

0 commit comments

Comments
 (0)