Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

bpo-31293: Fix crashes in truediv and mul of a timedelta by a float with a bad as_integer_ratio() method#3227

Merged
serhiy-storchaka merged 5 commits intopython:masterfrom
orenmn:bpo31293-fix-crashes
Sep 19, 2017
Merged

bpo-31293: Fix crashes in truediv and mul of a timedelta by a float with a bad as_integer_ratio() method#3227
serhiy-storchaka merged 5 commits intopython:masterfrom
orenmn:bpo31293-fix-crashes

Conversation

@orenmn
Copy link
Contributor

@orenmnorenmn commentedAug 28, 2017
edited by bedevere-bot
Loading

  • in_datetimemodule.c - add checks whether as_integer_ratio() returned a tuple.
  • indatetimetester.py - add tests to verify that the crashes are no more.

https://bugs.python.org/issue31293

class BadFloat(float):
def as_integer_ratio(self):
return 1 << 1000
self.assertRaises(TypeError, truediv, timedelta(), BadFloat())
Copy link
Member

Choose a reason for hiding this comment

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

The modern style to test for exceptions is to use awith assertRaises(..) block:

withself.assertRaises(TypeError):timedelta()/BadFloat()

goto error;
if (!PyTuple_Check(ratio)) {
PyErr_SetString(PyExc_TypeError,
"Can't multiply timedelta object by float with "
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 other error messages in this file start with a lowercase letter. Let's keep the style consistent.

I also wonder if a better message would be "unexpected return type from as_integer_ratio(): expected tuple, got %s".

@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 phraseI didn't expect the Spanish Inquisition!. 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.

@orenmn
Copy link
ContributorAuthor

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

Nobody expects the Spanish Inquisition!

@abalkin: please review the changes made to this pull request.

ratio = _PyObject_CallMethodId(floatobj, &PyId_as_integer_ratio, NULL);
if (ratio == NULL)
goto error;
if (!PyTuple_Check(ratio)) {

Choose a reason for hiding this comment

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

This is not enough. The size of the tuple should be 2.

Perhaps the code can be shared inmultiply_float_timedelta() anddivide_timedelta_int().

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

agh, of course.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

how can code be shared between these two functions? (I wrote a helper-function to share my patch's code between multiply_float_timedelta() and truedivide_timedelta_float().)

Choose a reason for hiding this comment

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

The code ofmultiply_float_timedelta() anddivide_timedelta_int() is almost the same. It is enough a one boolean parameter to distinguish multiplication from division. All code can be moved in a separate function, andmultiply_float_timedelta() anddivide_timedelta_int() will call it with additional argument 0 or 1.

Usually a refactoring is made only in develop version, but I think this one can be done in a bugfix change. It is enough simple and can help to fix other bugs if they will be found. What are your thoughts@abalkin?

@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 phraseI didn't expect the Spanish Inquisition!. 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.

And if you don't make the requested changes,you will be put in the comfy chair!

@orenmn
Copy link
ContributorAuthor

orenmn commentedAug 28, 2017
edited
Loading

I am working to fix the problems that Serhiy pointed out. please don't merge.

@orenmn
Copy link
ContributorAuthor

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

Nobody expects the Spanish Inquisition!

@serhiy-storchaka,@abalkin: please review the changes made to this pull request.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. I have added just a nitpick.

def test_issue31293(self):
# The interpreter shouldn't crash in case a timedelta is divided or
# multiplied by a float with a bad as_integer_ratio() method.
def _get_bad_float(bad_ratio):

Choose a reason for hiding this comment

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

No starting underscore is needed.

@serhiy-storchakaserhiy-storchaka added needs backport to 3.6 type-bugAn unexpected behavior, bug, or error labelsSep 19, 2017
@serhiy-storchakaserhiy-storchaka merged commit865e4b4 intopython:masterSep 19, 2017
@miss-islington
Copy link
Contributor

Thanks@orenmn for the PR, and@serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestSep 19, 2017
…loat with a bad as_integer_ratio() method. (pythonGH-3227)(cherry picked from commit865e4b4)
@bedevere-bot
Copy link

GH-3654 is a backport of this pull request to the3.6 branch.

serhiy-storchaka pushed a commit that referenced this pull requestSep 19, 2017
…loat with a bad as_integer_ratio() method. (GH-3227) (#3654)(cherry picked from commit865e4b4)
with self.assertRaises(TypeError):
timedelta() * get_bad_float(1 << 1000)

for bad_ratio in [(), (42, ), (1, 2, 3)]:
Copy link
Member

Choose a reason for hiding this comment

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

What would happen if as_integer_ratio() returns a pair of non-integers? A pair of strings? A pair of floats? Could you add tests for these scenarios?

Choose a reason for hiding this comment

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

TypeError will be raised on the attempt to divide on a string.

It doesn't matter what errors are raised with a bad as_integer_ratio(), but the code shouldn't crash.

@miss-islington
Copy link
Contributor

Thanks@orenmn for the PR, and@serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry,@orenmn and@serhiy-storchaka, I could not cleanly backport this to3.6 due to a conflict.
Please backport usingcherry_picker on command line.
cherry_picker 865e4b4f630e2ae91e61239258abb58b488f1d65 3.6

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@abalkinabalkinabalkin approved these changes

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

Assignees

No one assigned

Labels

type-bugAn unexpected behavior, bug, or error

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

7 participants

@orenmn@bedevere-bot@miss-islington@abalkin@serhiy-storchaka@Mariatta@the-knights-who-say-ni

Comments


[8]ページ先頭

©2009-2026 Movatter.jp