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

Conversation

Windsooon
Copy link
Contributor

@Windsooon Windsooon commented Jun 3, 2019

@Windsooon
Copy link
Contributor Author

I'm not sure should I add news or not.

Copy link
Member

@ericvsmith ericvsmith left a 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.

TypeError, 'an integer or a float is required \(got type str\)') as te:
time.sleep('foo')


Copy link
Member

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.

@@ -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:
Copy link
Member

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.

@@ -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:
Copy link
Member

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.

Copy link
Contributor Author

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.

@bedevere-bot
Copy link

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 I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Windsooon
Copy link
Contributor Author

Thank you for review my code. I updated the PR based on your review.

Copy link
Member

@ericvsmith ericvsmith left a 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.

@@ -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)",
Copy link
Member

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.

@ericvsmith
Copy link
Member

It looks like test_datetime and test_socket are failing because they check the exception text. They'll need to be fixed, too.

@Windsooon
Copy link
Contributor Author

Windsooon commented Jun 4, 2019

@ericvsmith I have some questions

  1. Should we use an integer or float is required. or an integer or float is required. (got type %s), ob->ob_name here, I found other tests didn't use a dynamic string in the PyErr_SetString function.
  2. Should I change Invalid value NaN (not a number) to Not a float number in invalid value in this commit?

@ericvsmith
Copy link
Member

@Windsooon:

  1. If you're talking about the tests, I'd just look for an integer or float is required. But for the error message, I'd include the found type. It's very helpful in debugging.
  2. I wouldn't change that as part of this PR. If it needs doing, I'd create a separate issue and PR.

Copy link
Contributor

@jdemeyer jdemeyer left a 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.

@jdemeyer
Copy link
Contributor

jdemeyer commented Jun 7, 2019

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.

@Windsooon
Copy link
Contributor Author

Windsooon commented Jun 7, 2019 via email

@ericvsmith
Copy link
Member

I think the earlier version of the patch (which didn't move the tests around) was a better design.

@jdemeyer
Copy link
Contributor

jdemeyer commented Jun 7, 2019

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)) {
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.

@JustinFay JustinFay mannequin mentioned this pull request Apr 10, 2022
@erlend-aasland erlend-aasland marked this pull request as draft February 9, 2024 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants