Skip to content

Commit 5197ecb

Browse files
authored
gh-143050: Remove redundant decref in _PyLong_Negate (gh-143051)
1 parent 665c1db commit 5197ecb

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Objects/longobject.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ _Py_DECREF_INT(PyLongObject *op)
4848
_Py_DECREF_SPECIALIZED((PyObject *)op, _PyLong_ExactDealloc);
4949
}
5050

51+
static inline int
52+
/// Return 1 if the object is one of the immortal small ints
53+
_long_is_small_int(PyObject *op)
54+
{
55+
assert(PyLong_Check(op));
56+
PyLongObject *long_object = (PyLongObject *)op;
57+
int is_small_int = (long_object->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0;
58+
assert((!is_small_int) || PyLong_CheckExact(op));
59+
return is_small_int;
60+
}
61+
5162
static inline int
5263
is_medium_int(stwodigits x)
5364
{
@@ -344,8 +355,6 @@ medium_from_stwodigits(stwodigits x)
344355
}
345356

346357

347-
/* If a freshly-allocated int is already shared, it must
348-
be a small integer, so negating it must go to PyLong_FromLong */
349358
Py_LOCAL_INLINE(void)
350359
_PyLong_Negate(PyLongObject **x_p)
351360
{
@@ -357,8 +366,10 @@ _PyLong_Negate(PyLongObject **x_p)
357366
return;
358367
}
359368

360-
*x_p = _PyLong_FromSTwoDigits(-medium_value(x));
361-
Py_DECREF(x);
369+
/* If a freshly-allocated int is already shared, it must
370+
be a small integer, so negating it will fit a single digit */
371+
assert(_long_is_small_int((PyObject *)x));
372+
*x_p = (PyLongObject *)_PyLong_FromSTwoDigits(-medium_value(x));
362373
}
363374

364375
#define PYLONG_FROM_INT(UINT_TYPE, INT_TYPE, ival) \
@@ -3622,16 +3633,6 @@ long_richcompare(PyObject *self, PyObject *other, int op)
36223633
Py_RETURN_RICHCOMPARE(result, 0, op);
36233634
}
36243635

3625-
static inline int
3626-
/// Return 1 if the object is one of the immortal small ints
3627-
_long_is_small_int(PyObject *op)
3628-
{
3629-
PyLongObject *long_object = (PyLongObject *)op;
3630-
int is_small_int = (long_object->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0;
3631-
assert((!is_small_int) || PyLong_CheckExact(op));
3632-
return is_small_int;
3633-
}
3634-
36353636
void
36363637
_PyLong_ExactDealloc(PyObject *self)
36373638
{

0 commit comments

Comments
 (0)