Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 155c44b

Browse filesBrowse files
gh-81267: Correct time.sleep() error message (#131055)
1 parent 15a8412 commit 155c44b
Copy full SHA for 155c44b

File tree

3 files changed

+26
-15
lines changed
Filter options

3 files changed

+26
-15
lines changed

‎Lib/test/test_time.py

Copy file name to clipboardExpand all lines: Lib/test/test_time.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ def test_sleep_exceptions(self):
167167
self.assertRaises(ValueError, time.sleep, -1)
168168
self.assertRaises(ValueError, time.sleep, -0.1)
169169

170+
# Improved exception #81267
171+
with self.assertRaises(TypeError) as errmsg:
172+
time.sleep([])
173+
self.assertIn("integer or float", str(errmsg.exception))
174+
170175
def test_sleep(self):
171176
for value in [-0.0, 0, 0.0, 1e-100, 1e-9, 1e-6, 1, 1.2]:
172177
with self.subTest(value=value):
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correct :func:`time.sleep` error message when an object that cannot be interpreted
2+
as an integer or float is provided.

‎Python/pytime.c

Copy file name to clipboardExpand all lines: Python/pytime.c
+19-15Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -594,26 +594,30 @@ pytime_from_object(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
594594
}
595595
return pytime_from_double(tp, d, round, unit_to_ns);
596596
}
597-
else {
598-
long long sec = PyLong_AsLongLong(obj);
599-
if (sec == -1 && PyErr_Occurred()) {
600-
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
601-
pytime_overflow();
602-
}
603-
return -1;
604-
}
605597

606-
static_assert(sizeof(long long) <= sizeof(PyTime_t),
607-
"PyTime_t is smaller than long long");
608-
PyTime_t ns = (PyTime_t)sec;
609-
if (pytime_mul(&ns, unit_to_ns) < 0) {
598+
long long sec = PyLong_AsLongLong(obj);
599+
if (sec == -1 && PyErr_Occurred()) {
600+
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
610601
pytime_overflow();
611-
return -1;
612602
}
603+
else if (PyErr_ExceptionMatches(PyExc_TypeError)) {
604+
PyErr_Format(PyExc_TypeError,
605+
"'%T' object cannot be interpreted as an integer or float",
606+
obj);
607+
}
608+
return -1;
609+
}
613610

614-
*tp = ns;
615-
return 0;
611+
static_assert(sizeof(long long) <= sizeof(PyTime_t),
612+
"PyTime_t is smaller than long long");
613+
PyTime_t ns = (PyTime_t)sec;
614+
if (pytime_mul(&ns, unit_to_ns) < 0) {
615+
pytime_overflow();
616+
return -1;
616617
}
618+
619+
*tp = ns;
620+
return 0;
617621
}
618622

619623

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.