-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
base: main
Are you sure you want to change the base?
Conversation
I'm not sure should I add news or not. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits, but otherwise looks good.
I don't think you need a news entry.
Lib/test/test_time.py
Outdated
TypeError, 'an integer or a float is required \(got type str\)') as te: | ||
time.sleep('foo') | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like all of the other tests have just one blank line between them. Please delete one of the lines.
Lib/test/test_time.py
Outdated
@@ -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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the as te
part of this expression.
Lib/test/test_time.py
Outdated
@@ -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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want this to be a raw (r''
) string, in order to escape the parens for the regex engine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I think you are right. It came to me earlier but I forgot to delete it. I updated the PR, thanks.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Thank you for review my code. I updated the PR based on your review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the one last nit. Once you make this one and the tests pass, I'll commit it.
Objects/longobject.c
Outdated
@@ -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)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is trivial, but it seems more consistent with existing code to say "an integer or float is required"
(that is, drop the "a"
). Would you mind making that change, too? I apologize for not catching that earlier.
It looks like test_datetime and test_socket are failing because they check the exception text. They'll need to be fixed, too. |
@ericvsmith I have some questions
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this remove support for objects with __int__
? See also bpo-35707.
Indeed, this breaks >>> import time, decimal; time.sleep(decimal.Decimal(1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer or float is required. |
Thank you :D, I will update the pr as well as the tests in a few days.
|
I think the earlier version of the patch (which didn't move the tests around) was a better design. |
I think it's better to merge #11636 first since that will change the structure of this code anyway and it has all the tests that you need. |
@@ -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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
https://bugs.python.org/issue37086