Skip to content

bpo-37086: fixed time.sleep error message #13768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member

@iritkatriel iritkatriel Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. In the else case below it calls PyLong_AsLongLong, which can handle values which are not PyLongs as long as they can be converted to a PyLong via _PyNumber_Index.

If this change didn't break any unit tests, then a test is probably missing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see you've commented on that. Sorry for the noise.

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)) {
Expand Down