Skip to content

Commit d414f7e

Browse files
authored
gh-90822: Make PY_SSIZE_T_MAX and PY_SSIZE_T_MIN constant expression (GH-92071)
1 parent 4d10f70 commit d414f7e

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Include/pyport.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,23 @@ typedef intptr_t Py_intptr_t;
114114
/* Py_ssize_t is a signed integral type such that sizeof(Py_ssize_t) ==
115115
* sizeof(size_t). C99 doesn't define such a thing directly (size_t is an
116116
* unsigned integral type). See PEP 353 for details.
117+
* PY_SSIZE_T_MAX is the largest positive value of type Py_ssize_t.
117118
*/
118119
#ifdef HAVE_PY_SSIZE_T
119120

120121
#elif HAVE_SSIZE_T
121122
typedef ssize_t Py_ssize_t;
123+
# define PY_SSIZE_T_MAX SSIZE_MAX
122124
#elif SIZEOF_VOID_P == SIZEOF_SIZE_T
123125
typedef Py_intptr_t Py_ssize_t;
126+
# define PY_SSIZE_T_MAX INTPTR_MAX
124127
#else
125128
# error "Python needs a typedef for Py_ssize_t in pyport.h."
126129
#endif
127130

131+
/* Smallest negative value of type Py_ssize_t. */
132+
#define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX-1)
133+
128134
/* Py_hash_t is the same size as a pointer. */
129135
#define SIZEOF_PY_HASH_T SIZEOF_SIZE_T
130136
typedef Py_ssize_t Py_hash_t;
@@ -138,11 +144,6 @@ typedef Py_ssize_t Py_ssize_clean_t;
138144
/* Largest possible value of size_t. */
139145
#define PY_SIZE_MAX SIZE_MAX
140146

141-
/* Largest positive value of type Py_ssize_t. */
142-
#define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
143-
/* Smallest negative value of type Py_ssize_t. */
144-
#define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX-1)
145-
146147
/* Macro kept for backward compatibility: use "z" in new code.
147148
*
148149
* PY_FORMAT_SIZE_T is a platform-specific modifier for use in a printf

PC/pyconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ WIN32 is still required for the locale module.
162162
/* Define like size_t, omitting the "unsigned" */
163163
#ifdef MS_WIN64
164164
typedef __int64 Py_ssize_t;
165+
# define PY_SSIZE_T_MAX LLONG_MAX
165166
#else
166167
typedef _W64 int Py_ssize_t;
168+
# define PY_SSIZE_T_MAX INT_MAX
167169
#endif
168170
#define HAVE_PY_SSIZE_T 1
169171

0 commit comments

Comments
 (0)