Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77d8a4f

Browse files
committed
float_floor_div: An expression like 3.//1j crashed the interpreter, or
delivered bizarre results. Check float_divmod for a Py_NotImplemented return and pass it along (instead of treating Py_NotImplemented as a 2-tuple). CONVERT_TO_DOUBLE: Added comments; this macro is obscure.
1 parent 63a3571 commit 77d8a4f

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

Objects/floatobject.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,11 @@ PyFloat_AsStringEx(char *buf, PyFloatObject *v, int precision)
265265

266266
/* Macro and helper that convert PyObject obj to a C double and store
267267
the value in dbl; this replaces the functionality of the coercion
268-
slot function */
269-
268+
slot function. If conversion to double raises an exception, obj is
269+
set to NULL, and the function invoking this macro returns NULL. If
270+
obj is not of float, int or long type, Py_NotImplemented is incref'ed,
271+
stored in obj, and returned from the function invoking this macro.
272+
*/
270273
#define CONVERT_TO_DOUBLE(obj, dbl) \
271274
if (PyFloat_Check(obj)) \
272275
dbl = PyFloat_AS_DOUBLE(obj); \
@@ -519,13 +522,13 @@ float_floor_div(PyObject *v, PyObject *w)
519522
PyObject *t, *r;
520523

521524
t = float_divmod(v, w);
522-
if (t != NULL) {
523-
r = PyTuple_GET_ITEM(t, 0);
524-
Py_INCREF(r);
525-
Py_DECREF(t);
526-
return r;
527-
}
528-
return NULL;
525+
if (t == NULL || t == Py_NotImplemented)
526+
return t;
527+
assert(PyTuple_CheckExact(t));
528+
r = PyTuple_GET_ITEM(t, 0);
529+
Py_INCREF(r);
530+
Py_DECREF(t);
531+
return r;
529532
}
530533

531534
static PyObject *

0 commit comments

Comments
 (0)