From a16eec8a7b6ebdef073c7f188a6987698532fff5 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 26 Nov 2020 10:17:25 +0100 Subject: [PATCH] PyCXX: [skip ci] fix SeqBase<>::max_size() by returning the max. value of a Py_ssize_t instead of -1 --- src/CXX/Python3/Objects.hxx | 13 +++++++++++-- src/CXX/Python3/cxxsupport.cxx | 6 ++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/CXX/Python3/Objects.hxx b/src/CXX/Python3/Objects.hxx index 7dd09af70d21..dd4b46c779f3 100644 --- a/src/CXX/Python3/Objects.hxx +++ b/src/CXX/Python3/Objects.hxx @@ -55,6 +55,7 @@ namespace Py { typedef Py_ssize_t sequence_index_type; // type of an index into a sequence + Py_ssize_t numeric_limits_max(); // Forward declarations class Object; @@ -1298,8 +1299,16 @@ namespace Py virtual size_type max_size() const { - return std::string::npos; // ? - //return std::numeric_limits::max(); + // Hint: Upstream version returns std::string::npos that is the maximum + // value of a size_t. But when assigned to a ssize_t it will become -1. + // Now Python provides 'sys.maxsize' that is the maximum value of a ssize_t + // and thus this method should return the same value. + // This can be done with 'std::numeric_limits::max()' but due + // to a name collision with a macro on Windows we cannot directly call it + // here. + // So, a workaround is to implement the helper function 'numeric_limits_max'. + //return std::string::npos; // ? + return numeric_limits_max(); } virtual size_type capacity() const diff --git a/src/CXX/Python3/cxxsupport.cxx b/src/CXX/Python3/cxxsupport.cxx index 442d932b2b3e..aff1b04b69d0 100644 --- a/src/CXX/Python3/cxxsupport.cxx +++ b/src/CXX/Python3/cxxsupport.cxx @@ -36,9 +36,15 @@ //----------------------------------------------------------------------------- #include "CXX/Objects.hxx" +#include namespace Py { +Py_ssize_t numeric_limits_max() +{ + return std::numeric_limits::max(); +} + Py_UNICODE unicode_null_string[1] = { 0 }; Type Object::type() const