Skip to content

Commit 4eb286a

Browse files
author
Ralf W. Grosse-Kunstleve
committed
ssize_t patches merged from HEAD
[SVN r35327]
1 parent a824230 commit 4eb286a

File tree

9 files changed

+57
-20
lines changed

9 files changed

+57
-20
lines changed

doc/v2/reference.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,16 @@ <h2><a name="utility">Utility and Infrastructure</a></h2>
10941094
</dd>
10951095
</dl>
10961096
</dd>
1097+
1098+
<dt><a href="ssize_t.html">ssize_t.hpp</a></dt>
1099+
1100+
<dd>
1101+
<dl class="index">
1102+
<dt><a href="ssize_t.html#typedefs">Typedefs</a></dt>
1103+
1104+
<dt><a href="ssize_t.html#constants">Constants</a></dt>
1105+
</dl>
1106+
</dd>
10971107
</dl>
10981108

10991109
<h2><a name="topics">Topics</a></h2>

include/boost/python/converter/builtin_converters.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# include <boost/python/detail/prefix.hpp>
88
# include <boost/python/detail/none.hpp>
99
# include <boost/python/handle.hpp>
10+
# include <boost/python/ssize_t.hpp>
1011
# include <boost/implicit_cast.hpp>
1112
# include <string>
1213
# include <complex>
@@ -115,9 +116,9 @@ BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned BOOST_PYTHON_LONG_LONG, ::PyLong_FromUn
115116

116117
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x))
117118
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())))
119+
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<ssize_t>(x.size())))
119120
#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())))
121+
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<ssize_t>(x.size())))
121122
# endif
122123
BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, ::PyFloat_FromDouble(x))
123124
BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, ::PyFloat_FromDouble(x))

include/boost/python/list.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# include <boost/python/object.hpp>
1111
# include <boost/python/converter/pytype_object_mgr_traits.hpp>
12+
# include <boost/python/ssize_t.hpp>
1213

1314
namespace boost { namespace python {
1415

@@ -24,11 +25,11 @@ namespace detail
2425

2526
long index(object_cref value) const; // return index of first occurrence of value
2627

27-
void insert(int index, object_cref); // insert object before index
28+
void insert(ssize_t index, object_cref); // insert object before index
2829
void insert(object const& index, object_cref);
2930

3031
object pop(); // remove and return item at index (default last)
31-
object pop(long index);
32+
object pop(ssize_t index);
3233
object pop(object const& index);
3334

3435
void remove(object_cref value); // remove first occurrence of value
@@ -86,7 +87,7 @@ class list : public detail::list_base
8687
}
8788

8889
template <class T>
89-
void insert(int index, T const& x) // insert object before index
90+
void insert(ssize_t index, T const& x) // insert object before index
9091
{
9192
base::insert(index, object(x));
9293
}
@@ -98,7 +99,7 @@ class list : public detail::list_base
9899
}
99100

100101
object pop() { return base::pop(); }
101-
object pop(long index) { return base::pop(index); }
102+
object pop(ssize_t index) { return base::pop(index); }
102103

103104
template <class T>
104105
object pop(T const& index)

include/boost/python/object.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef OBJECT_DWA2002612_HPP
66
# define OBJECT_DWA2002612_HPP
77

8-
# include <boost/python/detail/prefix.hpp>
8+
# include <boost/python/ssize_t.hpp>
99
# include <boost/python/object_core.hpp>
1010
# include <boost/python/object_attributes.hpp>
1111
# include <boost/python/object_items.hpp>
@@ -15,9 +15,9 @@
1515

1616
namespace boost { namespace python {
1717

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

src/list.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55
#include <boost/python/list.hpp>
6+
#include <boost/python/ssize_t.hpp>
67

78
namespace boost { namespace python { namespace detail {
89

@@ -53,7 +54,7 @@ long list_base::index(object_cref value) const
5354
return result;
5455
}
5556

56-
void list_base::insert(int index, object_cref item)
57+
void list_base::insert(ssize_t index, object_cref item)
5758
{
5859
if (PyList_CheckExact(this->ptr()))
5960
{
@@ -79,7 +80,7 @@ object list_base::pop()
7980
return this->attr("pop")();
8081
}
8182

82-
object list_base::pop(long index)
83+
object list_base::pop(ssize_t index)
8384
{
8485
return this->pop(object(index));
8586
}

src/object/class.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <boost/python/self.hpp>
2121
#include <boost/python/dict.hpp>
2222
#include <boost/python/str.hpp>
23+
#include <boost/python/ssize_t.hpp>
2324
#include <functional>
2425
#include <vector>
2526
#include <cstddef>
@@ -506,13 +507,14 @@ namespace objects
506507
// were declared, we'll use our class_type() as the single base
507508
// class.
508509
std::size_t const num_bases = (std::max)(num_types - 1, static_cast<std::size_t>(1));
509-
handle<> bases(PyTuple_New(num_bases));
510+
assert(num_bases <= ssize_t_max);
511+
handle<> bases(PyTuple_New(static_cast<ssize_t>(num_bases)));
510512

511513
for (std::size_t i = 1; i <= num_bases; ++i)
512514
{
513515
type_handle c = (i >= num_types) ? class_type() : get_class(types[i]);
514516
// PyTuple_SET_ITEM steals this reference
515-
PyTuple_SET_ITEM(bases.get(), i - 1, upcast<PyObject>(c.release()));
517+
PyTuple_SET_ITEM(bases.get(), static_cast<ssize_t>(i - 1), upcast<PyObject>(c.release()));
516518
}
517519

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

src/object/function.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <boost/python/extract.hpp>
1515
#include <boost/python/tuple.hpp>
1616
#include <boost/python/list.hpp>
17+
#include <boost/python/ssize_t.hpp>
1718

1819
#include <boost/python/detail/signature.hpp>
1920
#include <boost/mpl/vector/vector10.hpp>
@@ -64,7 +65,7 @@ function::function(
6465
= max_arity > num_keywords ? max_arity - num_keywords : 0;
6566

6667

67-
unsigned tuple_size = num_keywords ? max_arity : 0;
68+
ssize_t tuple_size = num_keywords ? max_arity : 0;
6869
m_arg_names = object(handle<>(PyTuple_New(tuple_size)));
6970

7071
if (num_keywords != 0)
@@ -158,7 +159,9 @@ PyObject* function::call(PyObject* args, PyObject* keywords) const
158159
else
159160
{
160161
// build a new arg tuple, will adjust its size later
161-
inner_args = handle<>(PyTuple_New(max_arity));
162+
assert(max_arity <= ssize_t_max);
163+
inner_args = handle<>(
164+
PyTuple_New(static_cast<ssize_t>(max_arity)));
162165

163166
// Fill in the positional arguments
164167
for (std::size_t i = 0; i < n_unnamed_actual; ++i)
@@ -293,7 +296,7 @@ void function::argument_error(PyObject* args, PyObject* /*keywords*/) const
293296
% make_tuple(this->m_namespace, this->m_name);
294297

295298
list actual_args;
296-
for (int i = 0; i < PyTuple_Size(args); ++i)
299+
for (ssize_t i = 0; i < PyTuple_Size(args); ++i)
297300
{
298301
char const* name = PyTuple_GetItem(args, i)->ob_type->tp_name;
299302
actual_args.append(str(name));

src/object_protocol.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <boost/python/object_protocol.hpp>
77
#include <boost/python/errors.hpp>
88
#include <boost/python/object.hpp>
9+
#include <boost/python/ssize_t.hpp>
910

1011
namespace boost { namespace python { namespace api {
1112

@@ -106,7 +107,7 @@ namespace // slicing code copied directly out of the Python implementation
106107
PySequenceMethods *sq = tp->tp_as_sequence;
107108

108109
if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
109-
int ilow = 0, ihigh = INT_MAX;
110+
ssize_t ilow = 0, ihigh = ssize_t_max;
110111
if (!_PyEval_SliceIndex(v, &ilow))
111112
return NULL;
112113
if (!_PyEval_SliceIndex(w, &ihigh))
@@ -133,7 +134,7 @@ namespace // slicing code copied directly out of the Python implementation
133134
PySequenceMethods *sq = tp->tp_as_sequence;
134135

135136
if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
136-
int ilow = 0, ihigh = INT_MAX;
137+
ssize_t ilow = 0, ihigh = ssize_t_max;
137138
if (!_PyEval_SliceIndex(v, &ilow))
138139
return -1;
139140
if (!_PyEval_SliceIndex(w, &ihigh))

src/str.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
44
#include <boost/python/str.hpp>
55
#include <boost/python/extract.hpp>
6+
#include <boost/python/ssize_t.hpp>
67

78
namespace boost { namespace python { namespace detail {
89

@@ -21,18 +22,35 @@ str_base::str_base(const char* s)
2122
: object(detail::new_reference(::PyString_FromString(s)))
2223
{}
2324

25+
namespace {
26+
27+
ssize_t str_size_as_py_ssize_t(std::size_t n)
28+
{
29+
if (n > ssize_t_max)
30+
{
31+
throw std::range_error("str size > ssize_t_max");
32+
}
33+
return static_cast<ssize_t>(n);
34+
}
35+
36+
} // namespace <anonymous>
37+
2438
str_base::str_base(char const* start, char const* finish)
2539
: object(
2640
detail::new_reference(
27-
::PyString_FromStringAndSize(start, finish - start)
41+
::PyString_FromStringAndSize(
42+
start, str_size_as_py_ssize_t(finish - start)
43+
)
2844
)
2945
)
3046
{}
3147

3248
str_base::str_base(char const* start, std::size_t length) // new str
3349
: object(
3450
detail::new_reference(
35-
::PyString_FromStringAndSize(start, length)
51+
::PyString_FromStringAndSize(
52+
start, str_size_as_py_ssize_t(length)
53+
)
3654
)
3755
)
3856
{}

0 commit comments

Comments
 (0)