diff --git a/Include/pymath.h b/Include/pymath.h index 6cf69f98acf933..87a49d18ac22ed 100644 --- a/Include/pymath.h +++ b/Include/pymath.h @@ -221,10 +221,14 @@ PyAPI_FUNC(void) _Py_set_387controlword(unsigned short); #define _Py_IntegralTypeMax(type) ((_Py_IntegralTypeSigned(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) + /* 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 - * integral type that cannot represent *v*'s integral part is undefined - * behavior. */ -#define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v <= _Py_IntegralTypeMax(type)) + * useful if *v* is floating-point, since demoting a floating-point *v* to + * an integral type that cannot represent *v*'s integral part is undefined + * behavior. If however sizeof(*v*) == sizeof(*type*) and *v* is a + * floating-point, maximal value of *type* cannot be represented exactly, + * thus the check, to be true needs to use strict less than (<). + */ +#define _Py_InIntegralTypeRange(type, v) (_Py_IntegralTypeMin(type) <= v && v < _Py_IntegralTypeMax(type)) #endif /* Py_PYMATH_H */ diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 7354b969907b16..03208597fd482c 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -160,6 +160,7 @@ def test_conversions(self): def test_sleep(self): self.assertRaises(ValueError, time.sleep, -2) self.assertRaises(ValueError, time.sleep, -1) + self.assertRaises(OverflowError, time.sleep, 2**63 / SEC_TO_NS) time.sleep(1.2) def test_strftime(self): diff --git a/Misc/ACKS b/Misc/ACKS index 8c8d954a312cb4..86eef013ff9e0b 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1830,3 +1830,4 @@ Jelle Zijlstra Gennadiy Zlobin Doug Zongker Peter Åstrand +Michał Radwański diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-08-18-01-39-06.bpo-34423.8rsQIk.rst b/Misc/NEWS.d/next/Core and Builtins/2018-08-18-01-39-06.bpo-34423.8rsQIk.rst new file mode 100644 index 00000000000000..b33096dca81456 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-08-18-01-39-06.bpo-34423.8rsQIk.rst @@ -0,0 +1,2 @@ +``time.sleep(2**63 / 10**9)`` no longer eludes range checks and doesn't +overflow.