Skip to content

Commit c6f2aa4

Browse files
author
Ralf W. Grosse-Kunstleve
committed
new boost/python/ssize_t.hpp; avoids potential clash of Py_ssize_t typedef and PY_SSIZE_T_MIN/MAX macros with definitions from other libraries
[SVN r35325]
1 parent c7fb2f7 commit c6f2aa4

12 files changed

Lines changed: 166 additions & 30 deletions

File tree

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>

doc/v2/ssize_t.html

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
<!-- Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost -->
3+
<!-- Software License, Version 1.0. (See accompanying -->
4+
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
5+
6+
<html>
7+
<head>
8+
<meta name="generator" content=
9+
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
10+
<meta http-equiv="Content-Type" content=
11+
"text/html; charset=us-ascii">
12+
<link rel="stylesheet" type="text/css" href="../boost.css">
13+
14+
<title>Boost.Python - &lt;boost/python/ssize_t.hpp&gt;</title>
15+
</head>
16+
17+
<body>
18+
<table border="0" cellpadding="7" cellspacing="0" width="100%"
19+
summary="header">
20+
<tr>
21+
<td valign="top" width="300">
22+
<h3><a href="../../../../index.htm"><img height="86" width=
23+
"277" alt="C++ Boost" src="../../../../boost.png" border=
24+
"0"></a></h3>
25+
</td>
26+
27+
<td valign="top">
28+
<h1 align="center"><a href=
29+
"../index.html">Boost.Python</a></h1>
30+
31+
<h2 align="center">Header
32+
&lt;boost/python/ssize_t.hpp&gt;</h2>
33+
</td>
34+
</tr>
35+
</table>
36+
<hr>
37+
38+
<h2>Contents</h2>
39+
40+
<dl class="page-index">
41+
<dt><a href="#introduction">Introduction</a></dt>
42+
43+
<dt><a href="#typdefs">Typedef</a></dt>
44+
45+
<dt><a href="#macros">Constants</a></dt>
46+
</dl>
47+
<hr>
48+
49+
<h2><a name="introduction" id=
50+
"introduction"></a>Introduction</h2>
51+
52+
<p>Python 2.5 introduces a new <tt>Py_ssize_t</tt> typedef and
53+
two related macros (<a href=
54+
"http://www.python.org/dev/peps/pep-0353/">PEP 353</a>). The
55+
<tt>&lt;boost/python/ssize_t.hpp&gt;</tt> header imports these
56+
definitions into the <tt>boost::python</tt> namespace as
57+
<tt>ssize_t</tt>, <tt>ssize_t_max</tt>, and <tt>ssize_t_min</tt>.
58+
Appropriate definitions are provided for backward compatibility
59+
with previous Python versions.</p>
60+
61+
<h2><a name="typedefs" id="typedefs"></a>Typedefs</h2>Imports
62+
<tt>Py_ssize_t</tt> into the <tt>boost::python</tt> namespace if
63+
available, or provides an appropriate typedef for backward
64+
compatibility:
65+
<pre>
66+
#if PY_VERSION_HEX &gt;= 0x02050000
67+
typedef Py_ssize_t ssize_t;
68+
#else
69+
typedef int ssize_t;
70+
#endif
71+
</pre>
72+
73+
<h2><a name="constants" id="constants"></a>Constants</h2>Imports
74+
<tt>PY_SSIZE_T_MAX</tt> and <tt>PY_SSIZE_T_MIN</tt> as constants
75+
into the <tt>boost::python</tt> namespace if available, or
76+
provides appropriate constants for backward compatibility:
77+
<pre>
78+
#if PY_VERSION_HEX &gt;= 0x02050000
79+
ssize_t const ssize_t_max = PY_SSIZE_T_MAX;
80+
ssize_t const ssize_t_min = PY_SSIZE_T_MIN;
81+
#else
82+
ssize_t const ssize_t_max = INT_MAX;
83+
ssize_t const ssize_t_min = INT_MIN;
84+
#endif
85+
</pre>
86+
87+
<p>Revised
88+
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
89+
25 September, 2006
90+
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
91+
92+
<p><i>&copy; Copyright <a href=
93+
"../../../../people/ralf_w_grosse_kunstleve.htm">Ralf W.
94+
Grosse-Kunstleve</a> 2006.</i></p>
95+
</body>
96+
</html>

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<Py_ssize_t>(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<Py_ssize_t>(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/detail/wrap_python.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ 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-
150144
#ifdef BOOST_PYTHON_ULONG_MAX_UNDEFINED
151145
# undef ULONG_MAX
152146
# undef BOOST_PYTHON_ULONG_MAX_UNDEFINED

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(Py_ssize_t 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(Py_ssize_t 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(Py_ssize_t 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(Py_ssize_t 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 Py_ssize_t len(object const& obj)
18+
inline ssize_t len(object const& obj)
1919
{
20-
Py_ssize_t 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
}

include/boost/python/ssize_t.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Ralf W. Grosse-Kunstleve & David Abrahams 2006.
2+
// Distributed under the Boost Software License, Version 1.0. (See
3+
// accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef BOOST_PYTHON_SSIZE_T_RWGK20060924_HPP
7+
# define BOOST_PYTHON_SSIZE_T_RWGK20060924_HPP
8+
9+
# include <boost/python/detail/prefix.hpp>
10+
11+
namespace boost { namespace python {
12+
13+
#if PY_VERSION_HEX >= 0x02050000
14+
15+
typedef Py_ssize_t ssize_t;
16+
ssize_t const ssize_t_max = PY_SSIZE_T_MAX;
17+
ssize_t const ssize_t_min = PY_SSIZE_T_MIN;
18+
19+
#else
20+
21+
typedef int ssize_t;
22+
ssize_t const ssize_t_max = INT_MAX;
23+
ssize_t const ssize_t_min = INT_MIN;
24+
25+
#endif
26+
27+
}} // namespace boost::python
28+
29+
#endif // BOOST_PYTHON_SSIZE_T_RWGK20060924_HPP

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(Py_ssize_t 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(Py_ssize_t 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 & 3 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,14 +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-
assert(num_bases <= PY_SSIZE_T_MAX);
510-
handle<> bases(PyTuple_New(static_cast<Py_ssize_t>(num_bases)));
510+
assert(num_bases <= ssize_t_max);
511+
handle<> bases(PyTuple_New(static_cast<ssize_t>(num_bases)));
511512

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

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

src/object/function.cpp

Lines changed: 5 additions & 4 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-
Py_ssize_t 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,9 +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-
assert(max_arity <= PY_SSIZE_T_MAX);
162+
assert(max_arity <= ssize_t_max);
162163
inner_args = handle<>(
163-
PyTuple_New(static_cast<Py_ssize_t>(max_arity)));
164+
PyTuple_New(static_cast<ssize_t>(max_arity)));
164165

165166
// Fill in the positional arguments
166167
for (std::size_t i = 0; i < n_unnamed_actual; ++i)
@@ -295,7 +296,7 @@ void function::argument_error(PyObject* args, PyObject* /*keywords*/) const
295296
% make_tuple(this->m_namespace, this->m_name);
296297

297298
list actual_args;
298-
for (Py_ssize_t i = 0; i < PyTuple_Size(args); ++i)
299+
for (ssize_t i = 0; i < PyTuple_Size(args); ++i)
299300
{
300301
char const* name = PyTuple_GetItem(args, i)->ob_type->tp_name;
301302
actual_args.append(str(name));

0 commit comments

Comments
 (0)