Skip to content

Commit 22b75d9

Browse files
authored
gh-82616: Add Py_IS_TYPE_SIGNED() macro (#93178)
_posixsubprocess: add a static assertion to ensure that the pid_t type is signed. Replace _Py_IntegralTypeSigned() with _Py_IS_TYPE_SIGNED().
1 parent cb04a09 commit 22b75d9

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

Include/internal/pycore_pymath.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,13 @@ static inline void _Py_ADJUST_ERANGE2(double x, double y)
5656
}
5757
}
5858

59-
// Return whether integral type *type* is signed or not.
60-
#define _Py_IntegralTypeSigned(type) \
61-
((type)(-1) < 0)
62-
6359
// Return the maximum value of integral type *type*.
6460
#define _Py_IntegralTypeMax(type) \
65-
((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
61+
(_Py_IS_TYPE_SIGNED(type) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
6662

6763
// Return the minimum value of integral type *type*.
6864
#define _Py_IntegralTypeMin(type) \
69-
((_Py_IntegralTypeSigned(type)) ? -_Py_IntegralTypeMax(type) - 1 : 0)
65+
(_Py_IS_TYPE_SIGNED(type) ? -_Py_IntegralTypeMax(type) - 1 : 0)
7066

7167
// Check whether *v* is in the range of integral type *type*. This is most
7268
// useful if *v* is floating-point, since demoting a floating-point *v* to an

Include/pymacro.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,9 @@
150150
// For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error.
151151
#define _Py_RVALUE(EXPR) ((void)0, (EXPR))
152152

153+
// Return non-zero if the type is signed, return zero if it's unsigned.
154+
// Use "<= 0" rather than "< 0" to prevent the compiler warning:
155+
// "comparison of unsigned expression in '< 0' is always false".
156+
#define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0)
157+
153158
#endif /* Py_PYMACRO_H */

Modules/_posixsubprocess.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,10 @@ child_exec(char *const exec_array[],
612612
#endif
613613

614614
#ifdef HAVE_SETPGID
615-
if (pgid_to_set >= 0)
615+
static_assert(_Py_IS_TYPE_SIGNED(pid_t), "pid_t is unsigned");
616+
if (pgid_to_set >= 0) {
616617
POSIX_CALL(setpgid(0, pgid_to_set));
618+
}
617619
#endif
618620

619621
#ifdef HAVE_SETGROUPS

Modules/_testcapimodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5858,6 +5858,20 @@ settrace_to_record(PyObject *self, PyObject *list)
58585858
Py_RETURN_NONE;
58595859
}
58605860

5861+
static PyObject *
5862+
test_macros(PyObject *self, PyObject *Py_UNUSED(args))
5863+
{
5864+
// Py_MIN(), Py_MAX()
5865+
assert(Py_MIN(5, 11) == 5);
5866+
assert(Py_MAX(5, 11) == 11);
5867+
5868+
// _Py_IS_TYPE_SIGNED()
5869+
assert(_Py_IS_TYPE_SIGNED(int));
5870+
assert(!_Py_IS_TYPE_SIGNED(unsigned int));
5871+
5872+
Py_RETURN_NONE;
5873+
}
5874+
58615875
static PyObject *negative_dictoffset(PyObject *, PyObject *);
58625876
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
58635877
static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
@@ -6149,6 +6163,7 @@ static PyMethodDef TestMethods[] = {
61496163
{"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
61506164
{"test_code_api", test_code_api, METH_NOARGS, NULL},
61516165
{"settrace_to_record", settrace_to_record, METH_O, NULL},
6166+
{"test_macros", test_macros, METH_NOARGS, NULL},
61526167
{NULL, NULL} /* sentinel */
61536168
};
61546169

0 commit comments

Comments
 (0)