Skip to content

Commit ca60ccc

Browse files
committed
handle case of negative shift
1 parent dd53470 commit ca60ccc

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

Lib/test/test_opcache.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,16 @@ def binary_op_bitwise_extend():
14251425
self.assert_specialized(binary_op_bitwise_extend, "BINARY_OP_EXTEND")
14261426
self.assert_no_opcode(binary_op_bitwise_extend, "BINARY_OP")
14271427

1428+
for idx in range(100):
1429+
a, b = 2, 1
1430+
if idx == 99:
1431+
b = -1
1432+
try:
1433+
a >> b
1434+
except ValueError:
1435+
assert b == -1
1436+
self.assertEqual(a, 1)
1437+
14281438
@cpython_only
14291439
@requires_specialization_ft
14301440
def test_load_super_attr(self):

Python/specialize.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,12 +2447,25 @@ is_compactlong(PyObject *v)
24472447
_PyLong_IsCompact((PyLongObject *)v);
24482448
}
24492449

2450+
static inline int
2451+
is_compactnonnegativelong(PyObject *v)
2452+
{
2453+
return PyLong_CheckExact(v) &&
2454+
_PyLong_IsNonNegativeCompact((PyLongObject *)v);
2455+
}
2456+
24502457
static int
24512458
compactlongs_guard(PyObject *lhs, PyObject *rhs)
24522459
{
24532460
return (is_compactlong(lhs) && is_compactlong(rhs));
24542461
}
24552462

2463+
static int
2464+
compactlong_compactnonnegativelong_guard(PyObject *lhs, PyObject *rhs)
2465+
{
2466+
return (is_compactlong(lhs) && is_compactnonnegativelong(rhs);
2467+
}
2468+
24562469
#define BITWISE_LONGS_ACTION(NAME, OP) \
24572470
static PyObject * \
24582471
(NAME)(PyObject *lhs, PyObject *rhs) \
@@ -2541,11 +2554,11 @@ static _PyBinaryOpSpecializationDescr compactlongs_specs[NB_OPARG_LAST+1] = {
25412554
[NB_OR] = {compactlongs_guard, compactlongs_or},
25422555
[NB_AND] = {compactlongs_guard, compactlongs_and},
25432556
[NB_XOR] = {compactlongs_guard, compactlongs_xor},
2544-
[NB_RSHIFT] = {compactlongs_guard, compactlongs_rshift},
2557+
[NB_RSHIFT] = {compactlong_compactnonnegativelong_guard, compactlongs_rshift},
25452558
[NB_INPLACE_OR] = {compactlongs_guard, compactlongs_or},
25462559
[NB_INPLACE_AND] = {compactlongs_guard, compactlongs_and},
25472560
[NB_INPLACE_XOR] = {compactlongs_guard, compactlongs_xor},
2548-
[NB_INPLACE_RSHIFT] = {compactlongs_guard, compactlongs_rshift},
2561+
[NB_INPLACE_RSHIFT] = {compactlong_compactnonnegativelong_guard, compactlongs_rshift},
25492562
};
25502563

25512564
static _PyBinaryOpSpecializationDescr float_compactlong_specs[NB_OPARG_LAST+1] = {

0 commit comments

Comments
 (0)