mirror of
https://github.com/python/cpython.git
synced 2025-09-24 17:33:29 +00:00
gh-81267: Correct time.sleep() error message (#131055)
This commit is contained in:
parent
15a8412b5e
commit
155c44b901
3 changed files with 27 additions and 16 deletions
|
@ -167,6 +167,11 @@ class TimeTestCase(unittest.TestCase):
|
|||
self.assertRaises(ValueError, time.sleep, -1)
|
||||
self.assertRaises(ValueError, time.sleep, -0.1)
|
||||
|
||||
# Improved exception #81267
|
||||
with self.assertRaises(TypeError) as errmsg:
|
||||
time.sleep([])
|
||||
self.assertIn("integer or float", str(errmsg.exception))
|
||||
|
||||
def test_sleep(self):
|
||||
for value in [-0.0, 0, 0.0, 1e-100, 1e-9, 1e-6, 1, 1.2]:
|
||||
with self.subTest(value=value):
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Correct :func:`time.sleep` error message when an object that cannot be interpreted
|
||||
as an integer or float is provided.
|
|
@ -594,12 +594,17 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
|
|||
}
|
||||
return pytime_from_double(tp, d, round, unit_to_ns);
|
||||
}
|
||||
else {
|
||||
|
||||
long long sec = PyLong_AsLongLong(obj);
|
||||
if (sec == -1 && PyErr_Occurred()) {
|
||||
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
|
||||
pytime_overflow();
|
||||
}
|
||||
else if (PyErr_ExceptionMatches(PyExc_TypeError)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"'%T' object cannot be interpreted as an integer or float",
|
||||
obj);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -613,7 +618,6 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
|
|||
|
||||
*tp = ns;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue