]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-82616: Add Py_IS_TYPE_SIGNED() macro (#93178)
authorVictor Stinner <vstinner@python.org>
Fri, 27 May 2022 13:05:35 +0000 (15:05 +0200)
committerGitHub <noreply@github.com>
Fri, 27 May 2022 13:05:35 +0000 (15:05 +0200)
_posixsubprocess: add a static assertion to ensure that the pid_t
type is signed.

Replace _Py_IntegralTypeSigned() with _Py_IS_TYPE_SIGNED().

Include/internal/pycore_pymath.h
Include/pymacro.h
Modules/_posixsubprocess.c
Modules/_testcapimodule.c

index 5c6aee2a23890b5bbc73afb281f06f2eb5a68191..5f3afe4df6865c05e807dde3ed460104b0547282 100644 (file)
@@ -56,17 +56,13 @@ static inline void _Py_ADJUST_ERANGE2(double x, double y)
     }
 }
 
-// Return whether integral type *type* is signed or not.
-#define _Py_IntegralTypeSigned(type) \
-    ((type)(-1) < 0)
-
 // Return the maximum value of integral type *type*.
 #define _Py_IntegralTypeMax(type) \
-    ((_Py_IntegralTypeSigned(type)) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
+    (_Py_IS_TYPE_SIGNED(type) ? (((((type)1 << (sizeof(type)*CHAR_BIT - 2)) - 1) << 1) + 1) : ~(type)0)
 
 // Return the minimum value of integral type *type*.
 #define _Py_IntegralTypeMin(type) \
-    ((_Py_IntegralTypeSigned(type)) ? -_Py_IntegralTypeMax(type) - 1 : 0)
+    (_Py_IS_TYPE_SIGNED(type) ? -_Py_IntegralTypeMax(type) - 1 : 0)
 
 // Check whether *v* is in the range of integral type *type*. This is most
 // useful if *v* is floating-point, since demoting a floating-point *v* to an
index b959eeb3f58b35d2d3a7a9029e0594b3aba10134..0a2c342e2c8aeefcdc201665989136572e722735 100644 (file)
 // For example, "int x; _Py_RVALUE(x) = 1;" fails with a compiler error.
 #define _Py_RVALUE(EXPR) ((void)0, (EXPR))
 
+// Return non-zero if the type is signed, return zero if it's unsigned.
+// Use "<= 0" rather than "< 0" to prevent the compiler warning:
+// "comparison of unsigned expression in '< 0' is always false".
+#define _Py_IS_TYPE_SIGNED(type) ((type)(-1) <= 0)
+
 #endif /* Py_PYMACRO_H */
index 9132f13e8166a72fc725743db866d6f0e57ff6b7..44e60d7c14954dc1ea4910d0e0bc70d951eeb629 100644 (file)
@@ -612,8 +612,10 @@ child_exec(char *const exec_array[],
 #endif
 
 #ifdef HAVE_SETPGID
-    if (pgid_to_set >= 0)
+    static_assert(_Py_IS_TYPE_SIGNED(pid_t), "pid_t is unsigned");
+    if (pgid_to_set >= 0) {
         POSIX_CALL(setpgid(0, pgid_to_set));
+    }
 #endif
 
 #ifdef HAVE_SETGROUPS
index 37f4ded8001c6f62fc4d68849b0d4ed9271b7450..ac0c96a11d3f4a29ee44d77e9bda4671cdca6cbc 100644 (file)
@@ -5858,6 +5858,20 @@ settrace_to_record(PyObject *self, PyObject *list)
     Py_RETURN_NONE;
 }
 
+static PyObject *
+test_macros(PyObject *self, PyObject *Py_UNUSED(args))
+{
+    // Py_MIN(), Py_MAX()
+    assert(Py_MIN(5, 11) == 5);
+    assert(Py_MAX(5, 11) == 11);
+
+    // _Py_IS_TYPE_SIGNED()
+    assert(_Py_IS_TYPE_SIGNED(int));
+    assert(!_Py_IS_TYPE_SIGNED(unsigned int));
+
+    Py_RETURN_NONE;
+}
+
 static PyObject *negative_dictoffset(PyObject *, PyObject *);
 static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
 static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
@@ -6149,6 +6163,7 @@ static PyMethodDef TestMethods[] = {
     {"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
     {"test_code_api", test_code_api, METH_NOARGS, NULL},
     {"settrace_to_record", settrace_to_record, METH_O, NULL},
+    {"test_macros", test_macros, METH_NOARGS, NULL},
     {NULL, NULL} /* sentinel */
 };