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

Python 2.5 compatibility

[SVN r34017]
This commit is contained in:
Ralf W. Grosse-Kunstleve
2006-05-18 22:41:14 +00:00
parent 66ac61450e
commit caa9cb8268
9 changed files with 45 additions and 19 deletions

View File

@@ -115,9 +115,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<int>(x.size())))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::string, ::PyString_FromStringAndSize(x.data(),implicit_cast<Py_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<int>(x.size())))
BOOST_PYTHON_TO_PYTHON_BY_VALUE(std::wstring, ::PyUnicode_FromWideChar(x.data(),implicit_cast<Py_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,6 +141,12 @@ 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

@@ -24,11 +24,11 @@ namespace detail
long index(object_cref value) const; // return index of first occurrence of value
void insert(int index, object_cref); // insert object before index
void insert(Py_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(long index);
object pop(Py_ssize_t index);
object pop(object const& index);
void remove(object_cref value); // remove first occurrence of value
@@ -86,7 +86,7 @@ class list : public detail::list_base
}
template <class T>
void insert(int index, T const& x) // insert object before index
void insert(Py_ssize_t index, T const& x) // insert object before index
{
base::insert(index, object(x));
}
@@ -98,7 +98,7 @@ class list : public detail::list_base
}
object pop() { return base::pop(); }
object pop(long index) { return base::pop(index); }
object pop(Py_ssize_t index) { return base::pop(index); }
template <class T>
object pop(T const& index)

View File

@@ -15,9 +15,9 @@
namespace boost { namespace python {
inline long len(object const& obj)
inline Py_ssize_t len(object const& obj)
{
long result = PyObject_Length(obj.ptr());
Py_ssize_t result = PyObject_Length(obj.ptr());
if (PyErr_Occurred()) throw_error_already_set();
return result;
}

View File

@@ -53,7 +53,7 @@ long list_base::index(object_cref value) const
return result;
}
void list_base::insert(int index, object_cref item)
void list_base::insert(Py_ssize_t index, object_cref item)
{
if (PyList_CheckExact(this->ptr()))
{
@@ -79,7 +79,7 @@ object list_base::pop()
return this->attr("pop")();
}
object list_base::pop(long index)
object list_base::pop(Py_ssize_t index)
{
return this->pop(object(index));
}

View File

@@ -506,13 +506,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));
handle<> bases(PyTuple_New(num_bases));
assert(num_bases <= PY_SSIZE_T_MAX);
handle<> bases(PyTuple_New(static_cast<Py_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(), i - 1, upcast<PyObject>(c.release()));
PyTuple_SET_ITEM(bases.get(), static_cast<Py_ssize_t>(i - 1), upcast<PyObject>(c.release()));
}
// Call the class metatype to create a new class

View File

@@ -64,7 +64,7 @@ function::function(
= max_arity > num_keywords ? max_arity - num_keywords : 0;
unsigned tuple_size = num_keywords ? max_arity : 0;
Py_ssize_t tuple_size = num_keywords ? max_arity : 0;
m_arg_names = object(handle<>(PyTuple_New(tuple_size)));
if (num_keywords != 0)
@@ -158,7 +158,9 @@ PyObject* function::call(PyObject* args, PyObject* keywords) const
else
{
// build a new arg tuple, will adjust its size later
inner_args = handle<>(PyTuple_New(max_arity));
assert(max_arity <= PY_SSIZE_T_MAX);
inner_args = handle<>(
PyTuple_New(static_cast<Py_ssize_t>(max_arity)));
// Fill in the positional arguments
for (std::size_t i = 0; i < n_unnamed_actual; ++i)
@@ -293,7 +295,7 @@ void function::argument_error(PyObject* args, PyObject* /*keywords*/) const
% make_tuple(this->m_namespace, this->m_name);
list actual_args;
for (int i = 0; i < PyTuple_Size(args); ++i)
for (Py_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

@@ -106,7 +106,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)) {
int ilow = 0, ihigh = INT_MAX;
Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
if (!_PyEval_SliceIndex(v, &ilow))
return NULL;
if (!_PyEval_SliceIndex(w, &ihigh))
@@ -133,7 +133,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)) {
int ilow = 0, ihigh = INT_MAX;
Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
if (!_PyEval_SliceIndex(v, &ilow))
return -1;
if (!_PyEval_SliceIndex(w, &ihigh))

View File

@@ -21,10 +21,25 @@ str_base::str_base(const char* s)
: object(detail::new_reference(::PyString_FromString(s)))
{}
namespace {
Py_ssize_t str_size_as_py_ssize_t(std::size_t n)
{
if (n > PY_SSIZE_T_MAX)
{
throw std::range_error("str size > PY_SSIZE_T_MAX");
}
return static_cast<Py_ssize_t>(n);
}
} // namespace <anonymous>
str_base::str_base(char const* start, char const* finish)
: object(
detail::new_reference(
::PyString_FromStringAndSize(start, finish - start)
::PyString_FromStringAndSize(
start, str_size_as_py_ssize_t(finish - start)
)
)
)
{}
@@ -32,7 +47,9 @@ str_base::str_base(char const* start, char const* finish)
str_base::str_base(char const* start, std::size_t length) // new str
: object(
detail::new_reference(
::PyString_FromStringAndSize(start, length)
::PyString_FromStringAndSize(
start, str_size_as_py_ssize_t(length)
)
)
)
{}