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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
serhiy-storchaka merged 5 commits intopython:masterfromorenmn:bpo31293-fix-crashes
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
init commit
  • Loading branch information
@orenmn
orenmn committedAug 28, 2017
commit6338b6aa28d142565c0afbe8280553b60a0524a9
11 changes: 10 additions & 1 deletionLib/test/datetimetester.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,7 @@

from array import array

from operator import lt, le, gt, ge, eq, ne, truediv, floordiv, mod
from operator import lt, le, gt, ge, eq, ne, truediv, floordiv, mod, mul

from test import support

Expand DownExpand Up@@ -866,6 +866,15 @@ def test_divmod(self):

self.assertRaises(TypeError, divmod, t, 10)

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.
class BadFloat(float):
def as_integer_ratio(self):
return (1 << 1000) - 1
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()

self.assertRaises(TypeError, mul, timedelta(), BadFloat())


#############################################################################
# date tests
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Fix crashes in true division and multiplication of a timedelta object by a
float with a bad as_integer_ratio() method. Patch by Oren Milman.
12 changes: 12 additions & 0 deletionsModules/_datetimemodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1664,6 +1664,12 @@ multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
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?

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".

"bad as_integer_ratio() method");
goto error;
}
temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0));
Py_DECREF(pyus_in);
pyus_in = NULL;
Expand DownExpand Up@@ -1762,6 +1768,12 @@ truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f)
ratio = _PyObject_CallMethodId(f, &PyId_as_integer_ratio, NULL);
if (ratio == NULL)
goto error;
if (!PyTuple_Check(ratio)) {
PyErr_SetString(PyExc_TypeError,
"Can't divide timedelta object by float with "
"bad as_integer_ratio() method");
goto error;
}
temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1));
Py_DECREF(pyus_in);
pyus_in = NULL;
Expand Down

[8]ページ先頭

©2009-2026 Movatter.jp