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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 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
Prev Previous commit
Next Next commit
Address Irit's review && Add guards for NaN
  • Loading branch information
Eclips4 committed Jan 17, 2025
commit 8363e9d270d95b7d23a0cda5dff6a9448bc28ea5
54 changes: 41 additions & 13 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,19 +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 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)
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
Comment thread
Eclips4 marked this conversation as resolved.
Outdated

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
16 changes: 10 additions & 6 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2416,25 +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.
Comment thread
Eclips4 marked this conversation as resolved.
Outdated
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))
Comment thread
Eclips4 marked this conversation as resolved.
Outdated
);
}

static int
float_compactlong_guard_true_div(PyObject *lhs, PyObject *rhs)
Comment thread
Eclips4 marked this conversation as resolved.
Outdated
{
// guards should check if rhs has a non-zero value
return (
PyFloat_CheckExact(lhs) &&
PyLong_CheckExact(rhs) &&
_PyLong_IsCompact((PyLongObject *)rhs) &&
!PyLong_IsZero(rhs)
!PyLong_IsZero(rhs) &&
!isnan(PyFloat_AsDouble(lhs))
);
}

Expand All @@ -2460,19 +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))
Comment thread
Eclips4 marked this conversation as resolved.
);
}

static int
compactlong_float_guard_true_div(PyObject *lhs, PyObject *rhs)
{
// guards should check if rhs has a non-zero value
return (
PyFloat_CheckExact(rhs) &&
PyLong_CheckExact(lhs) &&
_PyLong_IsCompact((PyLongObject *)lhs) &&
PyFloat_AsDouble(rhs) != 0.0
PyFloat_AsDouble(rhs) != 0.0 &&
!isnan(PyFloat_AsDouble(rhs))
);
}

Expand Down