diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 556666779d8522..e945af7b07e5e7 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -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; diff --git a/Objects/floatobject.c b/Objects/floatobject.c index a48a210adee3b9..d14c5de9dbc207 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -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; diff --git a/Objects/iterobject.c b/Objects/iterobject.c index 135ced9ea1f268..58e5a5078132f4 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -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() @@ -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))."); diff --git a/Objects/listobject.c b/Objects/listobject.c index 930aefde325a7c..9af1417c78acc5 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -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() @@ -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 * diff --git a/Objects/longobject.c b/Objects/longobject.c index 4e948940485730..1c825730d6934f 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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; @@ -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; } @@ -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); } @@ -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; } @@ -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. @@ -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)) { @@ -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); @@ -5355,7 +5353,7 @@ _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) { @@ -5363,7 +5361,7 @@ _PyLong_Rshift(PyObject *a, int64_t shiftby) return PyLong_FromLong(-1); } else { - return PyLong_FromLong(0); + return Py_NewRef(_PyLong_GetZero()); } } #endif @@ -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; @@ -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) { @@ -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) @@ -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] diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 2942ab624edf72..dc233e0c8b4ccf 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -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() @@ -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)) {