Skip to content

gh-125442: Use _PyLong_GetOne() and _PyLong_GetZero() in Objects #125440

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Objects/enumobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ reversed_len(reversedobject *ro, PyObject *Py_UNUSED(ignored))
Py_ssize_t position, seqsize;

if (ro->seq == NULL)
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
seqsize = PySequence_Size(ro->seq);
if (seqsize == -1)
return NULL;
Expand Down
4 changes: 1 addition & 3 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1526,9 +1526,7 @@ float_as_integer_ratio_impl(PyObject *self)
numerator = PyLong_FromDouble(float_part);
if (numerator == NULL)
goto error;
denominator = PyLong_FromLong(1);
if (denominator == NULL)
goto error;
denominator = Py_NewRef(_PyLong_GetOne());
py_exponent = PyLong_FromLong(Py_ABS(exponent));
if (py_exponent == NULL)
goto error;
Expand Down
3 changes: 2 additions & 1 deletion Objects/iterobject.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Iterator objects */

#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_abstract.h" // _PyObject_HasLen()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
Expand Down Expand Up @@ -96,7 +97,7 @@ iter_len(seqiterobject *it, PyObject *Py_UNUSED(ignored))
if (len >= 0)
return PyLong_FromSsize_t(len);
}
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
}

PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Expand Down
4 changes: 2 additions & 2 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "pycore_pyatomic_ft_wrappers.h"
#include "pycore_interp.h" // PyInterpreterState.list
#include "pycore_list.h" // struct _Py_list_freelist, _PyListIterObject
#include "pycore_long.h" // _PyLong_DigitCount
#include "pycore_long.h" // _PyLong_DigitCount, _PyLong_GetZero()
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_object.h" // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
#include "pycore_tuple.h" // _PyTuple_FromArray()
Expand Down Expand Up @@ -3900,7 +3900,7 @@ listiter_len(PyObject *self, PyObject *Py_UNUSED(ignored))
if (len >= 0)
return PyLong_FromSsize_t(len);
}
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
}

static PyObject *
Expand Down
30 changes: 14 additions & 16 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
Py_ssize_t idigit = 0; /* next free index in v->long_value.ob_digit */

if (n == 0)
return PyLong_FromLong(0L);
return Py_NewRef(_PyLong_GetZero());

if (little_endian) {
pstartbyte = bytes;
Expand Down Expand Up @@ -3715,7 +3715,7 @@ x_sub(PyLongObject *a, PyLongObject *b)
while (--i >= 0 && a->long_value.ob_digit[i] == b->long_value.ob_digit[i])
;
if (i < 0)
return (PyLongObject *)PyLong_FromLong(0);
return (PyLongObject *)Py_NewRef(_PyLong_GetZero());
if (a->long_value.ob_digit[i] < b->long_value.ob_digit[i]) {
sign = -1;
{ PyLongObject *temp = a; a = b; b = temp; }
Expand Down Expand Up @@ -4017,7 +4017,7 @@ k_mul(PyLongObject *a, PyLongObject *b)
i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
if (asize <= i) {
if (asize == 0)
return (PyLongObject *)PyLong_FromLong(0);
return (PyLongObject *)Py_NewRef(_PyLong_GetZero());
else
return x_mul(a, b);
}
Expand Down Expand Up @@ -4960,7 +4960,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
/* if modulus == 1:
return 0 */
if (_PyLong_IsNonNegativeCompact(c) && (c->long_value.ob_digit[0] == 1)) {
z = (PyLongObject *)PyLong_FromLong(0L);
z = (PyLongObject *)Py_NewRef(_PyLong_GetZero());
goto Done;
}

Expand Down Expand Up @@ -5003,9 +5003,7 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
/* At this point a, b, and c are guaranteed non-negative UNLESS
c is NULL, in which case a may be negative. */

z = (PyLongObject *)PyLong_FromLong(1L);
if (z == NULL)
goto Error;
z = (PyLongObject *)Py_NewRef(_PyLong_GetOne());

/* Perform a modular reduction, X = X % c, but leave X alone if c
* is NULL.
Expand Down Expand Up @@ -5328,7 +5326,7 @@ long_rshift(PyObject *a, PyObject *b)
return NULL;
}
if (_PyLong_IsZero((PyLongObject *)a)) {
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
}
if (PyLong_AsInt64(b, &shiftby) < 0) {
if (!PyErr_ExceptionMatches(PyExc_OverflowError)) {
Expand All @@ -5339,7 +5337,7 @@ long_rshift(PyObject *a, PyObject *b)
return PyLong_FromLong(-1);
}
else {
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
}
}
return _PyLong_Rshift(a, shiftby);
Expand All @@ -5355,15 +5353,15 @@ _PyLong_Rshift(PyObject *a, int64_t shiftby)
assert(PyLong_Check(a));
assert(shiftby >= 0);
if (_PyLong_IsZero((PyLongObject *)a)) {
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
}
#if PY_SSIZE_T_MAX <= INT64_MAX / PyLong_SHIFT
if (shiftby > (int64_t)PY_SSIZE_T_MAX * PyLong_SHIFT) {
if (_PyLong_IsNegative((PyLongObject *)a)) {
return PyLong_FromLong(-1);
}
else {
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
}
}
#endif
Expand Down Expand Up @@ -5426,7 +5424,7 @@ long_lshift_method(PyObject *aa, PyObject *bb)
return NULL;
}
if (_PyLong_IsZero(a)) {
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
}

int64_t shiftby;
Expand All @@ -5447,7 +5445,7 @@ long_lshift_int64(PyLongObject *a, int64_t shiftby)
assert(shiftby >= 0);

if (_PyLong_IsZero(a)) {
return PyLong_FromLong(0);
return Py_NewRef(_PyLong_GetZero());
}
#if PY_SSIZE_T_MAX <= INT64_MAX / PyLong_SHIFT
if (shiftby > (int64_t)PY_SSIZE_T_MAX * PyLong_SHIFT) {
Expand Down Expand Up @@ -5897,7 +5895,7 @@ long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase)
"int() missing string argument");
return NULL;
}
return PyLong_FromLong(0L);
return Py_NewRef(_PyLong_GetZero());
}
/* default base and limit, forward to standard implementation */
if (obase == NULL)
Expand Down Expand Up @@ -5979,13 +5977,13 @@ int___getnewargs___impl(PyObject *self)
static PyObject *
long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context))
{
return PyLong_FromLong(0L);
return Py_NewRef(_PyLong_GetZero());
}

static PyObject *
long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored))
{
return PyLong_FromLong(1L);
return Py_NewRef(_PyLong_GetOne());
}

/*[clinic input]
Expand Down
4 changes: 2 additions & 2 deletions Objects/rangeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h" // _PyEval_GetBuiltin()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_long.h" // _PyLong_GetZero(), _PyLong_GetOne()
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_range.h"
#include "pycore_tuple.h" // _PyTuple_ITEMS()
Expand Down Expand Up @@ -32,7 +32,7 @@ validate_step(PyObject *step)
{
/* No step specified, use a step of 1. */
if (!step)
return PyLong_FromLong(1);
return Py_NewRef(_PyLong_GetOne());

step = PyNumber_Index(step);
if (step && _PyLong_IsZero((PyLongObject *)step)) {
Expand Down
Loading