From 6c633dc3db5721128fc759f3074f6af383082915 Mon Sep 17 00:00:00 2001 From: Windsooon Date: Mon, 3 Jun 2019 11:42:29 +0800 Subject: [PATCH 1/4] bpo-37086: fixed time.sleep error message --- Lib/test/test_time.py | 6 ++++++ Objects/longobject.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 42799b2a21ca34..a0477d33c3d1fc 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -163,6 +163,12 @@ def test_sleep(self): self.assertRaises(ValueError, time.sleep, -1) time.sleep(1.2) + def test_sleep_invalid(self): + with self.assertRaisesRegex( + TypeError, 'an integer or a float is required \(got type str\)') as te: + time.sleep('foo') + + def test_strftime(self): tt = time.gmtime(self.t) for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', diff --git a/Objects/longobject.c b/Objects/longobject.c index 9fb1fb02c276bd..a3d3cf7159b5fc 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -207,7 +207,7 @@ _PyLong_FromNbIndexOrNbInt(PyObject *integral) nb = Py_TYPE(integral)->tp_as_number; if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) { PyErr_Format(PyExc_TypeError, - "an integer is required (got type %.200s)", + "an integer or a float is required (got type %.200s)", Py_TYPE(integral)->tp_name); return NULL; } From c8f1958f543e542f763f4bc9124c3ac5bfbec4de Mon Sep 17 00:00:00 2001 From: Windsooon Date: Tue, 4 Jun 2019 08:59:27 +0800 Subject: [PATCH 2/4] make the string as raw string, clean code --- Lib/test/test_time.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index a0477d33c3d1fc..f5134c62482465 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -165,10 +165,9 @@ def test_sleep(self): def test_sleep_invalid(self): with self.assertRaisesRegex( - TypeError, 'an integer or a float is required \(got type str\)') as te: + TypeError, r'an integer or a float is required \(got type str\)'): time.sleep('foo') - def test_strftime(self): tt = time.gmtime(self.t) for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', From 54cd9c8b0abf2da1dc48b64aa0555f948b4e57cf Mon Sep 17 00:00:00 2001 From: Windsooon Date: Tue, 4 Jun 2019 09:16:07 +0800 Subject: [PATCH 3/4] remote 'a' before 'float is required' --- Lib/test/test_time.py | 2 +- Objects/longobject.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index f5134c62482465..d74b92dde3fb8d 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -165,7 +165,7 @@ def test_sleep(self): def test_sleep_invalid(self): with self.assertRaisesRegex( - TypeError, r'an integer or a float is required \(got type str\)'): + TypeError, r'an integer or float is required \(got type str\)'): time.sleep('foo') def test_strftime(self): diff --git a/Objects/longobject.c b/Objects/longobject.c index a3d3cf7159b5fc..187eb9995d015a 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -207,7 +207,7 @@ _PyLong_FromNbIndexOrNbInt(PyObject *integral) nb = Py_TYPE(integral)->tp_as_number; if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) { PyErr_Format(PyExc_TypeError, - "an integer or a float is required (got type %.200s)", + "an integer or float is required (got type %.200s)", Py_TYPE(integral)->tp_name); return NULL; } From 429723423d5708e8d54afb54820a1f9de498e62f Mon Sep 17 00:00:00 2001 From: Windsooon Date: Tue, 4 Jun 2019 11:21:45 +0800 Subject: [PATCH 4/4] fixed time.sleep error message --- Lib/test/test_time.py | 2 +- Objects/longobject.c | 2 +- Python/pytime.c | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index d74b92dde3fb8d..08b847b1219bd3 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -165,7 +165,7 @@ def test_sleep(self): def test_sleep_invalid(self): with self.assertRaisesRegex( - TypeError, r'an integer or float is required \(got type str\)'): + TypeError, 'an integer or float is required'): time.sleep('foo') def test_strftime(self): diff --git a/Objects/longobject.c b/Objects/longobject.c index 187eb9995d015a..9fb1fb02c276bd 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -207,7 +207,7 @@ _PyLong_FromNbIndexOrNbInt(PyObject *integral) nb = Py_TYPE(integral)->tp_as_number; if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) { PyErr_Format(PyExc_TypeError, - "an integer or float is required (got type %.200s)", + "an integer is required (got type %.200s)", Py_TYPE(integral)->tp_name); return NULL; } diff --git a/Python/pytime.c b/Python/pytime.c index 9ff300699f04af..b66f4166c79a73 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -401,7 +401,11 @@ static int _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round, long unit_to_ns) { - if (PyFloat_Check(obj)) { + if (!PyFloat_Check(obj) && !PyLong_Check(obj)) { + PyErr_SetString(PyExc_TypeError, "an integer or float is required."); + return -1; + } + else if (PyFloat_Check(obj)) { double d; d = PyFloat_AsDouble(obj); if (Py_IS_NAN(d)) {