diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 42799b2a21ca34..08b847b1219bd3 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -163,6 +163,11 @@ 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 float is required'): + 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/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)) {