Skip to content
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

gh-100239: Handle NaN and zero division in guards #128963

Merged
merged 8 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
41 changes: 41 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,47 @@ def binary_op_add_extend():
self.assert_specialized(binary_op_add_extend, "BINARY_OP_EXTEND")
self.assert_no_opcode(binary_op_add_extend, "BINARY_OP")

def binary_op_zero_division():
def compactlong_lhs(arg):
42 / arg
def float_lhs(arg):
42.0 / arg

with self.assertRaises(ZeroDivisionError):
compactlong_lhs(0)
with self.assertRaises(ZeroDivisionError):
compactlong_lhs(0.0)
with self.assertRaises(ZeroDivisionError):
float_lhs(0.0)
with self.assertRaises(ZeroDivisionError):
float_lhs(0)

self.assert_no_opcode(compactlong_lhs, "BINARY_OP_EXTEND")
self.assert_no_opcode(float_lhs, "BINARY_OP_EXTEND")

binary_op_zero_division()

def binary_op_nan():
def compactlong_lhs(arg):
42 + arg
42 - arg
42 * arg
42 / arg
def compactlong_rhs(arg):
arg + 42
arg - 42
arg * 42
arg / 42
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved

compactlong_lhs(1.0)
compactlong_lhs(float('nan'))
compactlong_rhs(1.0)
compactlong_rhs(float('nan'))

self.assert_no_opcode(compactlong_lhs, "BINARY_OP_EXTEND")
self.assert_no_opcode(compactlong_rhs, "BINARY_OP_EXTEND")

binary_op_nan()

@cpython_only
@requires_specialization_ft
Expand Down
36 changes: 32 additions & 4 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2416,13 +2416,28 @@ binary_op_fail_kind(int oparg, PyObject *lhs, PyObject *rhs)

/* float-long */

// Guards should check that the float part is not NaN.
// Guards for / (aka true_div) should check for 0 or 0.0 as the rhs.
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
static int
float_compactlong_guard(PyObject *lhs, PyObject *rhs)
{
return (
PyFloat_CheckExact(lhs) &&
PyLong_CheckExact(rhs) &&
_PyLong_IsCompact((PyLongObject *)rhs)
_PyLong_IsCompact((PyLongObject *)rhs) &&
!isnan(PyFloat_AsDouble(lhs))
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
);
}

static int
float_compactlong_guard_true_div(PyObject *lhs, PyObject *rhs)
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
{
return (
PyFloat_CheckExact(lhs) &&
PyLong_CheckExact(rhs) &&
_PyLong_IsCompact((PyLongObject *)rhs) &&
!PyLong_IsZero(rhs) &&
!isnan(PyFloat_AsDouble(lhs))
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand All @@ -2448,7 +2463,20 @@ compactlong_float_guard(PyObject *lhs, PyObject *rhs)
return (
PyFloat_CheckExact(rhs) &&
PyLong_CheckExact(lhs) &&
_PyLong_IsCompact((PyLongObject *)lhs)
_PyLong_IsCompact((PyLongObject *)lhs) &&
!isnan(PyFloat_AsDouble(rhs))
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
);
}

static int
compactlong_float_guard_true_div(PyObject *lhs, PyObject *rhs)
{
return (
PyFloat_CheckExact(rhs) &&
PyLong_CheckExact(lhs) &&
_PyLong_IsCompact((PyLongObject *)lhs) &&
PyFloat_AsDouble(rhs) != 0.0 &&
!isnan(PyFloat_AsDouble(rhs))
);
}

Expand All @@ -2469,14 +2497,14 @@ LONG_FLOAT_ACTION(compactlong_float_true_div, /)
static _PyBinaryOpSpecializationDescr float_compactlong_specs[NB_OPARG_LAST+1] = {
[NB_ADD] = {float_compactlong_guard, float_compactlong_add},
[NB_SUBTRACT] = {float_compactlong_guard, float_compactlong_subtract},
[NB_TRUE_DIVIDE] = {float_compactlong_guard, float_compactlong_true_div},
[NB_TRUE_DIVIDE] = {float_compactlong_guard_true_div, float_compactlong_true_div},
[NB_MULTIPLY] = {float_compactlong_guard, float_compactlong_multiply},
};

static _PyBinaryOpSpecializationDescr compactlong_float_specs[NB_OPARG_LAST+1] = {
[NB_ADD] = {compactlong_float_guard, compactlong_float_add},
[NB_SUBTRACT] = {compactlong_float_guard, compactlong_float_subtract},
[NB_TRUE_DIVIDE] = {compactlong_float_guard, compactlong_float_true_div},
[NB_TRUE_DIVIDE] = {compactlong_float_guard_true_div, compactlong_float_true_div},
[NB_MULTIPLY] = {compactlong_float_guard, compactlong_float_multiply},
};

Expand Down
Loading