From 1f9f69dd4c5ee232c0b2f782933a89359932a67f Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Thu, 5 Dec 2019 19:55:28 +0500 Subject: [PATCH] bpo-27961: Replace PY_LLONG_MAX, PY_LLONG_MIN and PY_ULLONG_MAX with standard macros (GH-15385) Use standard constants LLONG_MIN, LLONG_MAX and ULLONG_MAX. --- Doc/c-api/long.rst | 4 ++-- Include/pythread.h | 8 ++++---- Modules/_testcapimodule.c | 30 +++++++++++++++--------------- Objects/longobject.c | 10 +++++----- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Doc/c-api/long.rst b/Doc/c-api/long.rst index 83d59de6a0d..5a6d09ad1bd 100644 --- a/Doc/c-api/long.rst +++ b/Doc/c-api/long.rst @@ -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. diff --git a/Include/pythread.h b/Include/pythread.h index f22e8c42c50..569d6964899 100644 --- a/Include/pythread.h +++ b/Include/pythread.h @@ -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 diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 0908f3457f5..943bee6e21e 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -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; @@ -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); @@ -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; @@ -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); @@ -757,7 +757,7 @@ 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; @@ -765,14 +765,14 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) 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; @@ -780,9 +780,9 @@ test_long_long_and_overflow(PyObject *self, PyObject *Py_UNUSED(ignored)) 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"); @@ -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))); diff --git a/Objects/longobject.c b/Objects/longobject.c index f0567970468..bc87fb2bd65 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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. */ @@ -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; @@ -5020,7 +5020,7 @@ simple: /* 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 @@ -5039,7 +5039,7 @@ simple: } #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"