2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 18:12:43 +00:00

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]
This commit is contained in:
Ralf W. Grosse-Kunstleve
2006-09-26 00:25:07 +00:00
parent c7fb2f7047
commit c6f2aa4ef2
12 changed files with 166 additions and 30 deletions

View File

@@ -1094,6 +1094,16 @@
</dd>
</dl>
</dd>
<dt><a href="ssize_t.html">ssize_t.hpp</a></dt>
<dd>
<dl class="index">
<dt><a href="ssize_t.html#typedefs">Typedefs</a></dt>
<dt><a href="ssize_t.html#constants">Constants</a></dt>
</dl>
</dd>
</dl>
<h2><a name="topics">Topics</a></h2>

96
doc/v2/ssize_t.html Normal file
View File

@@ -0,0 +1,96 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii">
<link rel="stylesheet" type="text/css" href="../boost.css">
<title>Boost.Python - &lt;boost/python/ssize_t.hpp&gt;</title>
</head>
<body>
<table border="0" cellpadding="7" cellspacing="0" width="100%"
summary="header">
<tr>
<td valign="top" width="300">
<h3><a href="../../../../index.htm"><img height="86" width=
"277" alt="C++ Boost" src="../../../../boost.png" border=
"0"></a></h3>
</td>
<td valign="top">
<h1 align="center"><a href=
"../index.html">Boost.Python</a></h1>
<h2 align="center">Header
&lt;boost/python/ssize_t.hpp&gt;</h2>
</td>
</tr>
</table>
<hr>
<h2>Contents</h2>
<dl class="page-index">
<dt><a href="#introduction">Introduction</a></dt>
<dt><a href="#typdefs">Typedef</a></dt>
<dt><a href="#macros">Constants</a></dt>
</dl>
<hr>
<h2><a name="introduction" id=
"introduction"></a>Introduction</h2>
<p>Python 2.5 introduces a new <tt>Py_ssize_t</tt> typedef and
two related macros (<a href=
"http://www.python.org/dev/peps/pep-0353/">PEP 353</a>). The
<tt>&lt;boost/python/ssize_t.hpp&gt;</tt> header imports these
definitions into the <tt>boost::python</tt> namespace as
<tt>ssize_t</tt>, <tt>ssize_t_max</tt>, and <tt>ssize_t_min</tt>.
Appropriate definitions are provided for backward compatibility
with previous Python versions.</p>
<h2><a name="typedefs" id="typedefs"></a>Typedefs</h2>Imports
<tt>Py_ssize_t</tt> into the <tt>boost::python</tt> namespace if
available, or provides an appropriate typedef for backward
compatibility:
<pre>
#if PY_VERSION_HEX &gt;= 0x02050000
typedef Py_ssize_t ssize_t;
#else
typedef int ssize_t;
#endif
</pre>
<h2><a name="constants" id="constants"></a>Constants</h2>Imports
<tt>PY_SSIZE_T_MAX</tt> and <tt>PY_SSIZE_T_MIN</tt> as constants
into the <tt>boost::python</tt> namespace if available, or
provides appropriate constants for backward compatibility:
<pre>
#if PY_VERSION_HEX &gt;= 0x02050000
ssize_t const ssize_t_max = PY_SSIZE_T_MAX;
ssize_t const ssize_t_min = PY_SSIZE_T_MIN;
#else
ssize_t const ssize_t_max = INT_MAX;
ssize_t const ssize_t_min = INT_MIN;
#endif
</pre>
<p>Revised
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->
25 September, 2006
<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
<p><i>&copy; Copyright <a href=
"../../../../people/ralf_w_grosse_kunstleve.htm">Ralf W.
Grosse-Kunstleve</a> 2006.</i></p>
</body>
</html>

View File

@@ -7,6 +7,7 @@
# include <boost/python/detail/prefix.hpp>
# include <boost/python/detail/none.hpp>
# include <boost/python/handle.hpp>
# include <boost/python/ssize_t.hpp>
# include <boost/implicit_cast.hpp>
# include <string>
# include <complex>
@@ -115,9 +116,9 @@ BOOST_PYTHON_TO_PYTHON_BY_VALUE(unsigned BOOST_PYTHON_LONG_LONG, ::PyLong_FromUn
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char, converter::do_return_to_python(x))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(char const*, converter::do_return_to_python(x))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<Py_ssize_t>(x.size())))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<ssize_t>(x.size())))
#if defined(Py_USING_UNICODE) && !defined(BOOST_NO_STD_WSTRING)
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<Py_ssize_t>(x.size())))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<ssize_t>(x.size())))
# endif
BOOST_PYTHON_TO_PYTHON_BY_VALUE(float, ::PyFloat_FromDouble(x))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(double, ::PyFloat_FromDouble(x))

View File

@@ -141,12 +141,6 @@ typedef int pid_t;
# include <Python.h>
#endif
#if PY_VERSION_HEX < 0x02050000
typedef int Py_ssize_t;
#define PY_SSIZE_T_MIN INT_MIN
#define PY_SSIZE_T_MAX INT_MAX
#endif
#ifdef BOOST_PYTHON_ULONG_MAX_UNDEFINED
# undef ULONG_MAX
# undef BOOST_PYTHON_ULONG_MAX_UNDEFINED

View File

@@ -9,6 +9,7 @@
# include <boost/python/object.hpp>
# include <boost/python/converter/pytype_object_mgr_traits.hpp>
# include <boost/python/ssize_t.hpp>
namespace boost { namespace python {
@@ -24,11 +25,11 @@ namespace detail
long index(object_cref value) const; // return index of first occurrence of value
void insert(Py_ssize_t index, object_cref); // insert object before index
void insert(ssize_t index, object_cref); // insert object before index
void insert(object const& index, object_cref);
object pop(); // remove and return item at index (default last)
object pop(Py_ssize_t index);
object pop(ssize_t index);
object pop(object const& index);
void remove(object_cref value); // remove first occurrence of value
@@ -86,7 +87,7 @@ class list : public detail::list_base
}
template <class T>
void insert(Py_ssize_t index, T const& x) // insert object before index
void insert(ssize_t index, T const& x) // insert object before index
{
base::insert(index, object(x));
}
@@ -98,7 +99,7 @@ class list : public detail::list_base
}
object pop() { return base::pop(); }
object pop(Py_ssize_t index) { return base::pop(index); }
object pop(ssize_t index) { return base::pop(index); }
template <class T>
object pop(T const& index)

View File

@@ -5,7 +5,7 @@
#ifndef OBJECT_DWA2002612_HPP
# define OBJECT_DWA2002612_HPP
# include <boost/python/detail/prefix.hpp>
# include <boost/python/ssize_t.hpp>
# include <boost/python/object_core.hpp>
# include <boost/python/object_attributes.hpp>
# include <boost/python/object_items.hpp>
@@ -15,9 +15,9 @@
namespace boost { namespace python {
inline Py_ssize_t len(object const& obj)
inline ssize_t len(object const& obj)
{
Py_ssize_t result = PyObject_Length(obj.ptr());
ssize_t result = PyObject_Length(obj.ptr());
if (PyErr_Occurred()) throw_error_already_set();
return result;
}

View File

@@ -0,0 +1,29 @@
// Copyright Ralf W. Grosse-Kunstleve & David Abrahams 2006.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_PYTHON_SSIZE_T_RWGK20060924_HPP
# define BOOST_PYTHON_SSIZE_T_RWGK20060924_HPP
# include <boost/python/detail/prefix.hpp>
namespace boost { namespace python {
#if PY_VERSION_HEX >= 0x02050000
typedef Py_ssize_t ssize_t;
ssize_t const ssize_t_max = PY_SSIZE_T_MAX;
ssize_t const ssize_t_min = PY_SSIZE_T_MIN;
#else
typedef int ssize_t;
ssize_t const ssize_t_max = INT_MAX;
ssize_t const ssize_t_min = INT_MIN;
#endif
}} // namespace boost::python
#endif // BOOST_PYTHON_SSIZE_T_RWGK20060924_HPP

View File

@@ -3,6 +3,7 @@
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/list.hpp>
#include <boost/python/ssize_t.hpp>
namespace boost { namespace python { namespace detail {
@@ -53,7 +54,7 @@ long list_base::index(object_cref value) const
return result;
}
void list_base::insert(Py_ssize_t index, object_cref item)
void list_base::insert(ssize_t index, object_cref item)
{
if (PyList_CheckExact(this->ptr()))
{
@@ -79,7 +80,7 @@ object list_base::pop()
return this->attr("pop")();
}
object list_base::pop(Py_ssize_t index)
object list_base::pop(ssize_t index)
{
return this->pop(object(index));
}

View File

@@ -20,6 +20,7 @@
#include <boost/python/self.hpp>
#include <boost/python/dict.hpp>
#include <boost/python/str.hpp>
#include <boost/python/ssize_t.hpp>
#include <functional>
#include <vector>
#include <cstddef>
@@ -506,14 +507,14 @@ namespace objects
// were declared, we'll use our class_type() as the single base
// class.
std::size_t const num_bases = (std::max)(num_types - 1, static_cast<std::size_t>(1));
assert(num_bases <= PY_SSIZE_T_MAX);
handle<> bases(PyTuple_New(static_cast<Py_ssize_t>(num_bases)));
assert(num_bases <= ssize_t_max);
handle<> bases(PyTuple_New(static_cast<ssize_t>(num_bases)));
for (std::size_t i = 1; i <= num_bases; ++i)
{
type_handle c = (i >= num_types) ? class_type() : get_class(types[i]);
// PyTuple_SET_ITEM steals this reference
PyTuple_SET_ITEM(bases.get(), static_cast<Py_ssize_t>(i - 1), upcast<PyObject>(c.release()));
PyTuple_SET_ITEM(bases.get(), static_cast<ssize_t>(i - 1), upcast<PyObject>(c.release()));
}
// Call the class metatype to create a new class

View File

@@ -14,6 +14,7 @@
#include <boost/python/extract.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/list.hpp>
#include <boost/python/ssize_t.hpp>
#include <boost/python/detail/signature.hpp>
#include <boost/mpl/vector/vector10.hpp>
@@ -64,7 +65,7 @@ function::function(
= max_arity > num_keywords ? max_arity - num_keywords : 0;
Py_ssize_t tuple_size = num_keywords ? max_arity : 0;
ssize_t tuple_size = num_keywords ? max_arity : 0;
m_arg_names = object(handle<>(PyTuple_New(tuple_size)));
if (num_keywords != 0)
@@ -158,9 +159,9 @@ PyObject* function::call(PyObject* args, PyObject* keywords) const
else
{
// build a new arg tuple, will adjust its size later
assert(max_arity <= PY_SSIZE_T_MAX);
assert(max_arity <= ssize_t_max);
inner_args = handle<>(
PyTuple_New(static_cast<Py_ssize_t>(max_arity)));
PyTuple_New(static_cast<ssize_t>(max_arity)));
// Fill in the positional arguments
for (std::size_t i = 0; i < n_unnamed_actual; ++i)
@@ -295,7 +296,7 @@ void function::argument_error(PyObject* args, PyObject* /*keywords*/) const
% make_tuple(this->m_namespace, this->m_name);
list actual_args;
for (Py_ssize_t i = 0; i < PyTuple_Size(args); ++i)
for (ssize_t i = 0; i < PyTuple_Size(args); ++i)
{
char const* name = PyTuple_GetItem(args, i)->ob_type->tp_name;
actual_args.append(str(name));

View File

@@ -6,6 +6,7 @@
#include <boost/python/object_protocol.hpp>
#include <boost/python/errors.hpp>
#include <boost/python/object.hpp>
#include <boost/python/ssize_t.hpp>
namespace boost { namespace python { namespace api {
@@ -106,7 +107,7 @@ namespace // slicing code copied directly out of the Python implementation
PySequenceMethods *sq = tp->tp_as_sequence;
if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
ssize_t ilow = 0, ihigh = ssize_t_max;
if (!_PyEval_SliceIndex(v, &ilow))
return NULL;
if (!_PyEval_SliceIndex(w, &ihigh))
@@ -133,7 +134,7 @@ namespace // slicing code copied directly out of the Python implementation
PySequenceMethods *sq = tp->tp_as_sequence;
if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
ssize_t ilow = 0, ihigh = ssize_t_max;
if (!_PyEval_SliceIndex(v, &ilow))
return -1;
if (!_PyEval_SliceIndex(w, &ihigh))

View File

@@ -3,6 +3,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/str.hpp>
#include <boost/python/extract.hpp>
#include <boost/python/ssize_t.hpp>
namespace boost { namespace python { namespace detail {
@@ -23,13 +24,13 @@ str_base::str_base(const char* s)
namespace {
Py_ssize_t str_size_as_py_ssize_t(std::size_t n)
ssize_t str_size_as_py_ssize_t(std::size_t n)
{
if (n > PY_SSIZE_T_MAX)
if (n > ssize_t_max)
{
throw std::range_error("str size > PY_SSIZE_T_MAX");
throw std::range_error("str size > ssize_t_max");
}
return static_cast<Py_ssize_t>(n);
return static_cast<ssize_t>(n);
}
} // namespace <anonymous>