Skip to content

bpo-27961: Replace PY_LLONG_MAX, PY_LLONG_MIN and PY_ULLONG_MAX with standard macros #15385

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

Merged
merged 2 commits into from
Dec 5, 2019
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
4 changes: 2 additions & 2 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
:meth:`__int__` method (if present) to convert it to a
:c:type:`PyLongObject`.

If the value of *obj* is greater than :const:`PY_LLONG_MAX` or less than
:const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively,
If the value of *obj* is greater than :const:`LLONG_MAX` or less than
:const:`LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``, respectively,
and return ``-1``; otherwise, set *\*overflow* to ``0``. If any other
exception occurs set *\*overflow* to ``0`` and return ``-1`` as usual.

Expand Down
8 changes: 4 additions & 4 deletions Include/pythread.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ PyAPI_FUNC(int) PyThread_acquire_lock(PyThread_type_lock, int);
#if defined(_POSIX_THREADS)
/* PyThread_acquire_lock_timed() uses _PyTime_FromNanoseconds(us * 1000),
convert microseconds to nanoseconds. */
# define PY_TIMEOUT_MAX (PY_LLONG_MAX / 1000)
# define PY_TIMEOUT_MAX (LLONG_MAX / 1000)
#elif defined (NT_THREADS)
/* In the NT API, the timeout is a DWORD and is expressed in milliseconds */
# if 0xFFFFFFFFLL * 1000 < PY_LLONG_MAX
# if 0xFFFFFFFFLL * 1000 < LLONG_MAX
# define PY_TIMEOUT_MAX (0xFFFFFFFFLL * 1000)
# else
# define PY_TIMEOUT_MAX PY_LLONG_MAX
# define PY_TIMEOUT_MAX LLONG_MAX
# endif
#else
# define PY_TIMEOUT_MAX PY_LLONG_MAX
# define PY_TIMEOUT_MAX LLONG_MAX
#endif


Expand Down
30 changes: 15 additions & 15 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
int overflow;

/* Test that overflow is set properly for a large value. */
/* num is a number larger than PY_LLONG_MAX on a typical machine. */
/* num is a number larger than LLONG_MAX on a typical machine. */
num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
if (num == NULL)
return NULL;
Expand All @@ -655,8 +655,8 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
return raiseTestError("test_long_long_and_overflow",
"overflow was not set to 1");

/* Same again, with num = PY_LLONG_MAX + 1 */
num = PyLong_FromLongLong(PY_LLONG_MAX);
/* Same again, with num = LLONG_MAX + 1 */
num = PyLong_FromLongLong(LLONG_MAX);
if (num == NULL)
return NULL;
one = PyLong_FromLong(1L);
Expand All @@ -683,7 +683,7 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
"overflow was not set to 1");

/* Test that overflow is set properly for a large negative value. */
/* num is a number smaller than PY_LLONG_MIN on a typical platform */
/* num is a number smaller than LLONG_MIN on a typical platform */
num = PyLong_FromString("-FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
if (num == NULL)
return NULL;
Expand All @@ -699,8 +699,8 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
return raiseTestError("test_long_long_and_overflow",
"overflow was not set to -1");

/* Same again, with num = PY_LLONG_MIN - 1 */
num = PyLong_FromLongLong(PY_LLONG_MIN);
/* Same again, with num = LLONG_MIN - 1 */
num = PyLong_FromLongLong(LLONG_MIN);
if (num == NULL)
return NULL;
one = PyLong_FromLong(1L);
Expand Down Expand Up @@ -757,32 +757,32 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored))
return raiseTestError("test_long_long_and_overflow",
"overflow was set incorrectly");

num = PyLong_FromLongLong(PY_LLONG_MAX);
num = PyLong_FromLongLong(LLONG_MAX);
if (num == NULL)
return NULL;
overflow = 1234;
value = PyLong_AsLongLongAndOverflow(num, &overflow);
Py_DECREF(num);
if (value == -1 && PyErr_Occurred())
return NULL;
if (value != PY_LLONG_MAX)
if (value != LLONG_MAX)
return raiseTestError("test_long_long_and_overflow",
"expected return value PY_LLONG_MAX");
"expected return value LLONG_MAX");
if (overflow != 0)
return raiseTestError("test_long_long_and_overflow",
"overflow was not cleared");

num = PyLong_FromLongLong(PY_LLONG_MIN);
num = PyLong_FromLongLong(LLONG_MIN);
if (num == NULL)
return NULL;
overflow = 0;
value = PyLong_AsLongLongAndOverflow(num, &overflow);
Py_DECREF(num);
if (value == -1 && PyErr_Occurred())
return NULL;
if (value != PY_LLONG_MIN)
if (value != LLONG_MIN)
return raiseTestError("test_long_long_and_overflow",
"expected return value PY_LLONG_MIN");
"expected return value LLONG_MIN");
if (overflow != 0)
return raiseTestError("test_long_long_and_overflow",
"overflow was not cleared");
Expand Down Expand Up @@ -6710,9 +6710,9 @@ PyInit__testcapi(void)
PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(LLONG_MAX));
PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(LLONG_MIN));
PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(ULLONG_MAX));
PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
Expand Down
10 changes: 5 additions & 5 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ PyLong_AsVoidPtr(PyObject *vv)
* rewritten to use the newer PyLong_{As,From}ByteArray API.
*/

#define PY_ABS_LLONG_MIN (0-(unsigned long long)PY_LLONG_MIN)
#define PY_ABS_LLONG_MIN (0-(unsigned long long)LLONG_MIN)

/* Create a new int object from a C long long int. */

Expand Down Expand Up @@ -1462,11 +1462,11 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
/* Haven't lost any bits, but casting to long requires extra
* care (see comment above).
*/
if (x <= (unsigned long long)PY_LLONG_MAX) {
if (x <= (unsigned long long)LLONG_MAX) {
res = (long long)x * sign;
}
else if (sign < 0 && x == PY_ABS_LLONG_MIN) {
res = PY_LLONG_MIN;
res = LLONG_MIN;
}
else {
*overflow = sign;
Expand Down Expand Up @@ -5020,7 +5020,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
/* a fits into a long, so b must too */
x = PyLong_AsLong((PyObject *)a);
y = PyLong_AsLong((PyObject *)b);
#elif PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
#elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
x = PyLong_AsLongLong((PyObject *)a);
y = PyLong_AsLongLong((PyObject *)b);
#else
Expand All @@ -5039,7 +5039,7 @@ _PyLong_GCD(PyObject *aarg, PyObject *barg)
}
#if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
return PyLong_FromLong(x);
#elif PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
#elif LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
return PyLong_FromLongLong(x);
#else
# error "_PyLong_GCD"
Expand Down