Skip to content

Commit

Permalink
PyCXX: [skip ci] fix SeqBase<>::max_size() by returning the max. valu…
Browse files Browse the repository at this point in the history
…e of a Py_ssize_t instead of -1
  • Loading branch information
wwmayer committed Nov 26, 2020
1 parent 298c677 commit a16eec8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/CXX/Python3/Objects.hxx
Expand Up @@ -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;
Expand Down Expand Up @@ -1298,8 +1299,16 @@ namespace Py

virtual size_type max_size() const
{
return std::string::npos; // ?
//return std::numeric_limits<size_type>::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<size_type>::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
Expand Down
6 changes: 6 additions & 0 deletions src/CXX/Python3/cxxsupport.cxx
Expand Up @@ -36,9 +36,15 @@
//-----------------------------------------------------------------------------

#include "CXX/Objects.hxx"
#include <limits>
namespace Py
{

Py_ssize_t numeric_limits_max()
{
return std::numeric_limits<Py_ssize_t>::max();
}

Py_UNICODE unicode_null_string[1] = { 0 };

Type Object::type() const
Expand Down

0 comments on commit a16eec8

Please sign in to comment.