22
33#include "Python.h"
44#include "pycore_abstract.h" // _PyIndex_Check()
5+ #include "pycore_long.h" // _PyLong_GetZero()
56#include "pycore_tuple.h" // _PyTuple_ITEMS()
67#include "structmember.h" // PyMemberDef
78
@@ -105,10 +106,10 @@ range_from_array(PyTypeObject *type, PyObject *const *args, Py_ssize_t num_args)
105106 if (!stop ) {
106107 return NULL ;
107108 }
108- Py_INCREF ( _PyLong_Zero );
109- start = _PyLong_Zero ;
110- Py_INCREF ( _PyLong_One );
111- step = _PyLong_One ;
109+ start = _PyLong_GetZero ( );
110+ Py_INCREF ( start ) ;
111+ step = _PyLong_GetOne ( );
112+ Py_INCREF ( step ) ;
112113 break ;
113114 case 0 :
114115 PyErr_SetString (PyExc_TypeError ,
@@ -190,7 +191,10 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
190191 PyObject * tmp1 = NULL , * tmp2 = NULL , * result ;
191192 /* holds sub-expression evaluations */
192193
193- cmp_result = PyObject_RichCompareBool (step , _PyLong_Zero , Py_GT );
194+ PyObject * zero = _PyLong_GetZero (); // borrowed reference
195+ PyObject * one = _PyLong_GetOne (); // borrowed reference
196+
197+ cmp_result = PyObject_RichCompareBool (step , zero , Py_GT );
194198 if (cmp_result == -1 )
195199 return NULL ;
196200
@@ -212,19 +216,21 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
212216 Py_DECREF (step );
213217 if (cmp_result < 0 )
214218 return NULL ;
215- return PyLong_FromLong (0 );
219+ result = zero ;
220+ Py_INCREF (result );
221+ return result ;
216222 }
217223
218224 if ((tmp1 = PyNumber_Subtract (hi , lo )) == NULL )
219225 goto Fail ;
220226
221- if ((diff = PyNumber_Subtract (tmp1 , _PyLong_One )) == NULL )
227+ if ((diff = PyNumber_Subtract (tmp1 , one )) == NULL )
222228 goto Fail ;
223229
224230 if ((tmp2 = PyNumber_FloorDivide (diff , step )) == NULL )
225231 goto Fail ;
226232
227- if ((result = PyNumber_Add (tmp2 , _PyLong_One )) == NULL )
233+ if ((result = PyNumber_Add (tmp2 , one )) == NULL )
228234 goto Fail ;
229235
230236 Py_DECREF (tmp2 );
@@ -254,7 +260,7 @@ compute_item(rangeobject *r, PyObject *i)
254260 /* PyLong equivalent to:
255261 * return r->start + (i * r->step)
256262 */
257- if (r -> step == _PyLong_One ) {
263+ if (r -> step == _PyLong_GetOne () ) {
258264 result = PyNumber_Add (r -> start , i );
259265 }
260266 else {
@@ -271,6 +277,7 @@ compute_item(rangeobject *r, PyObject *i)
271277static PyObject *
272278compute_range_item (rangeobject * r , PyObject * arg )
273279{
280+ PyObject * zero = _PyLong_GetZero (); // borrowed reference
274281 int cmp_result ;
275282 PyObject * i , * result ;
276283
@@ -281,7 +288,7 @@ compute_range_item(rangeobject *r, PyObject *arg)
281288 * i = arg
282289 * }
283290 */
284- cmp_result = PyObject_RichCompareBool (arg , _PyLong_Zero , Py_LT );
291+ cmp_result = PyObject_RichCompareBool (arg , zero , Py_LT );
285292 if (cmp_result == -1 ) {
286293 return NULL ;
287294 }
@@ -300,7 +307,7 @@ compute_range_item(rangeobject *r, PyObject *arg)
300307 * <report index out of bounds>
301308 * }
302309 */
303- cmp_result = PyObject_RichCompareBool (i , _PyLong_Zero , Py_LT );
310+ cmp_result = PyObject_RichCompareBool (i , zero , Py_LT );
304311 if (cmp_result == 0 ) {
305312 cmp_result = PyObject_RichCompareBool (i , r -> length , Py_GE );
306313 }
@@ -375,14 +382,15 @@ compute_slice(rangeobject *r, PyObject *_slice)
375382static int
376383range_contains_long (rangeobject * r , PyObject * ob )
377384{
385+ PyObject * zero = _PyLong_GetZero (); // borrowed reference
378386 int cmp1 , cmp2 , cmp3 ;
379387 PyObject * tmp1 = NULL ;
380388 PyObject * tmp2 = NULL ;
381389 int result = -1 ;
382390
383391 /* Check if the value can possibly be in the range. */
384392
385- cmp1 = PyObject_RichCompareBool (r -> step , _PyLong_Zero , Py_GT );
393+ cmp1 = PyObject_RichCompareBool (r -> step , zero , Py_GT );
386394 if (cmp1 == -1 )
387395 goto end ;
388396 if (cmp1 == 1 ) { /* positive steps: start <= ob < stop */
@@ -409,7 +417,7 @@ range_contains_long(rangeobject *r, PyObject *ob)
409417 if (tmp2 == NULL )
410418 goto end ;
411419 /* result = ((int(ob) - start) % step) == 0 */
412- result = PyObject_RichCompareBool (tmp2 , _PyLong_Zero , Py_EQ );
420+ result = PyObject_RichCompareBool (tmp2 , zero , Py_EQ );
413421 end :
414422 Py_XDECREF (tmp1 );
415423 Py_XDECREF (tmp2 );
@@ -460,7 +468,7 @@ range_equals(rangeobject *r0, rangeobject *r1)
460468 /* Return False or error to the caller. */
461469 if (cmp_result != 1 )
462470 return cmp_result ;
463- cmp_result = PyObject_RichCompareBool (r0 -> length , _PyLong_One , Py_EQ );
471+ cmp_result = PyObject_RichCompareBool (r0 -> length , _PyLong_GetOne () , Py_EQ );
464472 /* Return True or error to the caller. */
465473 if (cmp_result != 0 )
466474 return cmp_result ;
@@ -529,7 +537,7 @@ range_hash(rangeobject *r)
529537 else {
530538 Py_INCREF (r -> start );
531539 PyTuple_SET_ITEM (t , 1 , r -> start );
532- cmp_result = PyObject_RichCompareBool (r -> length , _PyLong_One , Py_EQ );
540+ cmp_result = PyObject_RichCompareBool (r -> length , _PyLong_GetOne () , Py_EQ );
533541 if (cmp_result == -1 )
534542 goto end ;
535543 if (cmp_result == 1 ) {
@@ -587,7 +595,7 @@ range_index(rangeobject *r, PyObject *ob)
587595 return NULL ;
588596 }
589597
590- if (r -> step == _PyLong_One ) {
598+ if (r -> step == _PyLong_GetOne () ) {
591599 return idx ;
592600 }
593601
@@ -974,14 +982,15 @@ longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored))
974982static PyObject *
975983longrangeiter_setstate (longrangeiterobject * r , PyObject * state )
976984{
985+ PyObject * zero = _PyLong_GetZero (); // borrowed reference
977986 int cmp ;
978987
979988 /* clip the value */
980- cmp = PyObject_RichCompareBool (state , _PyLong_Zero , Py_LT );
989+ cmp = PyObject_RichCompareBool (state , zero , Py_LT );
981990 if (cmp < 0 )
982991 return NULL ;
983992 if (cmp > 0 ) {
984- state = _PyLong_Zero ;
993+ state = zero ;
985994 }
986995 else {
987996 cmp = PyObject_RichCompareBool (r -> len , state , Py_LT );
@@ -1022,7 +1031,7 @@ longrangeiter_next(longrangeiterobject *r)
10221031 if (PyObject_RichCompareBool (r -> index , r -> len , Py_LT ) != 1 )
10231032 return NULL ;
10241033
1025- new_index = PyNumber_Add (r -> index , _PyLong_One );
1034+ new_index = PyNumber_Add (r -> index , _PyLong_GetOne () );
10261035 if (!new_index )
10271036 return NULL ;
10281037
@@ -1119,7 +1128,7 @@ range_iter(PyObject *seq)
11191128 it -> start = r -> start ;
11201129 it -> step = r -> step ;
11211130 it -> len = r -> length ;
1122- it -> index = _PyLong_Zero ;
1131+ it -> index = _PyLong_GetZero () ;
11231132 Py_INCREF (it -> start );
11241133 Py_INCREF (it -> step );
11251134 Py_INCREF (it -> len );
@@ -1207,7 +1216,7 @@ range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
12071216 it -> len = range -> length ;
12081217 Py_INCREF (it -> len );
12091218
1210- diff = PyNumber_Subtract (it -> len , _PyLong_One );
1219+ diff = PyNumber_Subtract (it -> len , _PyLong_GetOne () );
12111220 if (!diff )
12121221 goto create_failure ;
12131222
@@ -1226,7 +1235,7 @@ range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
12261235 if (!it -> step )
12271236 goto create_failure ;
12281237
1229- it -> index = _PyLong_Zero ;
1238+ it -> index = _PyLong_GetZero () ;
12301239 Py_INCREF (it -> index );
12311240 return (PyObject * )it ;
12321241
0 commit comments