Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-82616: Add Py_IS_TYPE_SIGNED() macro #93178

Merged
merged 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Include/internal/pycore_pymath.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Include/pymacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,9 @@
// 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 */
4 changes: 3 additions & 1 deletion Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5832,6 +5832,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*);
Expand Down Expand Up @@ -6122,6 +6136,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 */
};

Expand Down