bpo-29878: Add global instances of int for 0 and 1. (#852)

diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 5ebb504..9a416a8 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -936,7 +936,7 @@
 /*[clinic input]
 @classmethod
 complex.__new__ as complex_new
-    real as r: object(c_default="Py_False") = 0
+    real as r: object(c_default="_PyLong_Zero") = 0
     imag as i: object(c_default="NULL") = 0
 
 Create a complex number from a real part and an optional imaginary part.
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index 480768f..3eb1736 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -100,7 +100,6 @@
 static PyObject *
 enum_next_long(enumobject *en, PyObject* next_item)
 {
-    static PyObject *one = NULL;
     PyObject *result = en->en_result;
     PyObject *next_index;
     PyObject *stepped_up;
@@ -110,14 +109,9 @@
         if (en->en_longindex == NULL)
             return NULL;
     }
-    if (one == NULL) {
-        one = PyLong_FromLong(1);
-        if (one == NULL)
-            return NULL;
-    }
     next_index = en->en_longindex;
     assert(next_index != NULL);
-    stepped_up = PyNumber_Add(next_index, one);
+    stepped_up = PyNumber_Add(next_index, _PyLong_One);
     if (stepped_up == NULL)
         return NULL;
     en->en_longindex = stepped_up;
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 86c3c88..87af0ba 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -443,7 +443,6 @@
             double fracpart;
             double intpart;
             PyObject *result = NULL;
-            PyObject *one = NULL;
             PyObject *vv = NULL;
             PyObject *ww = w;
 
@@ -466,23 +465,19 @@
                  */
                 PyObject *temp;
 
-                one = PyLong_FromLong(1);
-                if (one == NULL)
-                    goto Error;
-
-                temp = PyNumber_Lshift(ww, one);
+                temp = PyNumber_Lshift(ww, _PyLong_One);
                 if (temp == NULL)
                     goto Error;
                 Py_DECREF(ww);
                 ww = temp;
 
-                temp = PyNumber_Lshift(vv, one);
+                temp = PyNumber_Lshift(vv, _PyLong_One);
                 if (temp == NULL)
                     goto Error;
                 Py_DECREF(vv);
                 vv = temp;
 
-                temp = PyNumber_Or(vv, one);
+                temp = PyNumber_Or(vv, _PyLong_One);
                 if (temp == NULL)
                     goto Error;
                 Py_DECREF(vv);
@@ -496,7 +491,6 @@
          Error:
             Py_XDECREF(vv);
             Py_XDECREF(ww);
-            Py_XDECREF(one);
             return result;
         }
     } /* else if (PyLong_Check(w)) */
@@ -1617,7 +1611,7 @@
 /*[clinic input]
 @classmethod
 float.__new__ as float_new
-    x: object(c_default="Py_False") = 0
+    x: object(c_default="_PyLong_Zero") = 0
     /
 
 Convert a string or number to a floating point number, if possible.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 0bf6ae6..459eed9 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -31,6 +31,9 @@
              (Py_SIZE(x) == 0 ? (sdigit)0 :                             \
               (sdigit)(x)->ob_digit[0]))
 
+PyObject *_PyLong_Zero = NULL;
+PyObject *_PyLong_One = NULL;
+
 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
 /* Small integers are preallocated in this array so that they
    can be shared.
@@ -2551,14 +2554,12 @@
         (size_a == size_b &&
          a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
         /* |a| < |b|. */
-        *pdiv = (PyLongObject*)PyLong_FromLong(0);
-        if (*pdiv == NULL)
-            return -1;
         *prem = (PyLongObject *)long_long((PyObject *)a);
         if (*prem == NULL) {
-            Py_CLEAR(*pdiv);
             return -1;
         }
+        Py_INCREF(_PyLong_Zero);
+        *pdiv = (PyLongObject*)_PyLong_Zero;
         return 0;
     }
     if (size_b == 1) {
@@ -3695,7 +3696,6 @@
     if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
         (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
         PyLongObject *temp;
-        PyLongObject *one;
         temp = (PyLongObject *) long_add(mod, w);
         Py_DECREF(mod);
         mod = temp;
@@ -3703,15 +3703,12 @@
             Py_DECREF(div);
             return -1;
         }
-        one = (PyLongObject *) PyLong_FromLong(1L);
-        if (one == NULL ||
-            (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
+        temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_One);
+        if (temp == NULL) {
             Py_DECREF(mod);
             Py_DECREF(div);
-            Py_XDECREF(one);
             return -1;
         }
-        Py_DECREF(one);
         Py_DECREF(div);
         div = temp;
     }
@@ -4242,14 +4239,9 @@
 {
     /* Implement ~x as -(x+1) */
     PyLongObject *x;
-    PyLongObject *w;
     if (Py_ABS(Py_SIZE(v)) <=1)
         return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
-    w = (PyLongObject *)PyLong_FromLong(1L);
-    if (w == NULL)
-        return NULL;
-    x = (PyLongObject *) long_add(v, w);
-    Py_DECREF(w);
+    x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_One);
     if (x == NULL)
         return NULL;
     _PyLong_Negate(&x);
@@ -4932,7 +4924,7 @@
 _PyLong_DivmodNear(PyObject *a, PyObject *b)
 {
     PyLongObject *quo = NULL, *rem = NULL;
-    PyObject *one = NULL, *twice_rem, *result, *temp;
+    PyObject *twice_rem, *result, *temp;
     int cmp, quo_is_odd, quo_is_neg;
 
     /* Equivalent Python code:
@@ -4959,16 +4951,12 @@
     /* Do a and b have different signs?  If so, quotient is negative. */
     quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0);
 
-    one = PyLong_FromLong(1L);
-    if (one == NULL)
-        return NULL;
-
     if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0)
         goto error;
 
     /* compare twice the remainder with the divisor, to see
        if we need to adjust the quotient and remainder */
-    twice_rem = long_lshift((PyObject *)rem, one);
+    twice_rem = long_lshift((PyObject *)rem, _PyLong_One);
     if (twice_rem == NULL)
         goto error;
     if (quo_is_neg) {
@@ -4985,9 +4973,9 @@
     if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) {
         /* fix up quotient */
         if (quo_is_neg)
-            temp = long_sub(quo, (PyLongObject *)one);
+            temp = long_sub(quo, (PyLongObject *)_PyLong_One);
         else
-            temp = long_add(quo, (PyLongObject *)one);
+            temp = long_add(quo, (PyLongObject *)_PyLong_One);
         Py_DECREF(quo);
         quo = (PyLongObject *)temp;
         if (quo == NULL)
@@ -5010,13 +4998,11 @@
     /* PyTuple_SET_ITEM steals references */
     PyTuple_SET_ITEM(result, 0, (PyObject *)quo);
     PyTuple_SET_ITEM(result, 1, (PyObject *)rem);
-    Py_DECREF(one);
     return result;
 
   error:
     Py_XDECREF(quo);
     Py_XDECREF(rem);
-    Py_XDECREF(one);
     return NULL;
 }
 
@@ -5505,6 +5491,13 @@
         v->ob_digit[0] = (digit)abs(ival);
     }
 #endif
+    _PyLong_Zero = PyLong_FromLong(0);
+    if (_PyLong_Zero == NULL)
+        return 0;
+    _PyLong_One = PyLong_FromLong(1);
+    if (_PyLong_One == NULL)
+        return 0;
+
     /* initialize int_info */
     if (Int_InfoType.tp_name == NULL) {
         if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0)
@@ -5520,6 +5513,8 @@
     /* Integers are currently statically allocated. Py_DECREF is not
        needed, but Python must forget about the reference or multiple
        reinitializations will fail. */
+    Py_CLEAR(_PyLong_One);
+    Py_CLEAR(_PyLong_Zero);
 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
     int i;
     PyLongObject *v = small_ints;
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index 820fea5..554528d 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -83,17 +83,10 @@
         stop = PyNumber_Index(stop);
         if (!stop)
             return NULL;
-        start = PyLong_FromLong(0);
-        if (!start) {
-            Py_DECREF(stop);
-            return NULL;
-        }
-        step = PyLong_FromLong(1);
-        if (!step) {
-            Py_DECREF(stop);
-            Py_DECREF(start);
-            return NULL;
-        }
+        Py_INCREF(_PyLong_Zero);
+        start = _PyLong_Zero;
+        Py_INCREF(_PyLong_One);
+        step = _PyLong_One;
     }
     else {
         if (!PyArg_UnpackTuple(args, "range", 2, 3,
@@ -162,15 +155,10 @@
     int cmp_result;
     PyObject *lo, *hi;
     PyObject *diff = NULL;
-    PyObject *one = NULL;
     PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
                 /* holds sub-expression evaluations */
 
-    PyObject *zero = PyLong_FromLong(0);
-    if (zero == NULL)
-        return NULL;
-    cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
-    Py_DECREF(zero);
+    cmp_result = PyObject_RichCompareBool(step, _PyLong_Zero, Py_GT);
     if (cmp_result == -1)
         return NULL;
 
@@ -195,26 +183,22 @@
         return PyLong_FromLong(0);
     }
 
-    if ((one = PyLong_FromLong(1L)) == NULL)
-        goto Fail;
-
     if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
         goto Fail;
 
-    if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
+    if ((diff = PyNumber_Subtract(tmp1, _PyLong_One)) == NULL)
         goto Fail;
 
     if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
         goto Fail;
 
-    if ((result = PyNumber_Add(tmp2, one)) == NULL)
+    if ((result = PyNumber_Add(tmp2, _PyLong_One)) == NULL)
         goto Fail;
 
     Py_DECREF(tmp2);
     Py_DECREF(diff);
     Py_DECREF(step);
     Py_DECREF(tmp1);
-    Py_DECREF(one);
     return result;
 
   Fail:
@@ -222,7 +206,6 @@
     Py_XDECREF(tmp2);
     Py_XDECREF(diff);
     Py_XDECREF(tmp1);
-    Py_XDECREF(one);
     return NULL;
 }
 
@@ -253,10 +236,6 @@
     int cmp_result;
     PyObject *i, *result;
 
-    PyObject *zero = PyLong_FromLong(0);
-    if (zero == NULL)
-        return NULL;
-
     /* PyLong equivalent to:
      *   if (arg < 0) {
      *     i = r->length + arg
@@ -264,20 +243,18 @@
      *     i = arg
      *   }
      */
-    cmp_result = PyObject_RichCompareBool(arg, zero, Py_LT);
+    cmp_result = PyObject_RichCompareBool(arg, _PyLong_Zero, Py_LT);
     if (cmp_result == -1) {
-        Py_DECREF(zero);
         return NULL;
     }
     if (cmp_result == 1) {
-      i = PyNumber_Add(r->length, arg);
-      if (!i) {
-        Py_DECREF(zero);
-        return NULL;
-      }
+        i = PyNumber_Add(r->length, arg);
+        if (!i) {
+          return NULL;
+        }
     } else {
-      i = arg;
-      Py_INCREF(i);
+        i = arg;
+        Py_INCREF(i);
     }
 
     /* PyLong equivalent to:
@@ -285,8 +262,7 @@
      *     <report index out of bounds>
      *   }
      */
-    cmp_result = PyObject_RichCompareBool(i, zero, Py_LT);
-    Py_DECREF(zero);
+    cmp_result = PyObject_RichCompareBool(i, _PyLong_Zero, Py_LT);
     if (cmp_result == 0) {
         cmp_result = PyObject_RichCompareBool(i, r->length, Py_GE);
     }
@@ -364,16 +340,11 @@
     int cmp1, cmp2, cmp3;
     PyObject *tmp1 = NULL;
     PyObject *tmp2 = NULL;
-    PyObject *zero = NULL;
     int result = -1;
 
-    zero = PyLong_FromLong(0);
-    if (zero == NULL) /* MemoryError in int(0) */
-        goto end;
-
     /* Check if the value can possibly be in the range. */
 
-    cmp1 = PyObject_RichCompareBool(r->step, zero, Py_GT);
+    cmp1 = PyObject_RichCompareBool(r->step, _PyLong_Zero, Py_GT);
     if (cmp1 == -1)
         goto end;
     if (cmp1 == 1) { /* positive steps: start <= ob < stop */
@@ -400,11 +371,10 @@
     if (tmp2 == NULL)
         goto end;
     /* result = ((int(ob) - start) % step) == 0 */
-    result = PyObject_RichCompareBool(tmp2, zero, Py_EQ);
+    result = PyObject_RichCompareBool(tmp2, _PyLong_Zero, Py_EQ);
   end:
     Py_XDECREF(tmp1);
     Py_XDECREF(tmp2);
-    Py_XDECREF(zero);
     return result;
 }
 
@@ -437,7 +407,6 @@
 range_equals(rangeobject *r0, rangeobject *r1)
 {
     int cmp_result;
-    PyObject *one;
 
     if (r0 == r1)
         return 1;
@@ -453,11 +422,7 @@
     /* Return False or error to the caller. */
     if (cmp_result != 1)
         return cmp_result;
-    one = PyLong_FromLong(1);
-    if (!one)
-        return -1;
-    cmp_result = PyObject_RichCompareBool(r0->length, one, Py_EQ);
-    Py_DECREF(one);
+    cmp_result = PyObject_RichCompareBool(r0->length, _PyLong_One, Py_EQ);
     /* Return True or error to the caller. */
     if (cmp_result != 0)
         return cmp_result;
@@ -524,14 +489,9 @@
         PyTuple_SET_ITEM(t, 2, Py_None);
     }
     else {
-        PyObject *one;
         Py_INCREF(r->start);
         PyTuple_SET_ITEM(t, 1, r->start);
-        one = PyLong_FromLong(1);
-        if (!one)
-            goto end;
-        cmp_result = PyObject_RichCompareBool(r->length, one, Py_EQ);
-        Py_DECREF(one);
+        cmp_result = PyObject_RichCompareBool(r->length, _PyLong_One, Py_EQ);
         if (cmp_result == -1)
             goto end;
         if (cmp_result == 1) {
@@ -556,10 +516,7 @@
         int result = range_contains_long(r, ob);
         if (result == -1)
             return NULL;
-        else if (result)
-            return PyLong_FromLong(1);
-        else
-            return PyLong_FromLong(0);
+        return PyLong_FromLong(result);
     } else {
         Py_ssize_t count;
         count = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_COUNT);
@@ -973,24 +930,19 @@
     int cmp;
 
     /* clip the value */
-    PyObject *zero = PyLong_FromLong(0);
-    if (zero == NULL)
+    cmp = PyObject_RichCompareBool(state, _PyLong_Zero, Py_LT);
+    if (cmp < 0)
         return NULL;
-    cmp = PyObject_RichCompareBool(state, zero, Py_LT);
     if (cmp > 0) {
-        Py_XSETREF(r->index, zero);
-        Py_RETURN_NONE;
+        state = _PyLong_Zero;
     }
-    Py_DECREF(zero);
-    if (cmp < 0)
-        return NULL;
-
-    cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
-    if (cmp < 0)
-        return NULL;
-    if (cmp > 0)
-        state = r->len;
-
+    else {
+        cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
+        if (cmp < 0)
+            return NULL;
+        if (cmp > 0)
+            state = r->len;
+    }
     Py_INCREF(state);
     Py_XSETREF(r->index, state);
     Py_RETURN_NONE;
@@ -1019,16 +971,11 @@
 static PyObject *
 longrangeiter_next(longrangeiterobject *r)
 {
-    PyObject *one, *product, *new_index, *result;
+    PyObject *product, *new_index, *result;
     if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1)
         return NULL;
 
-    one = PyLong_FromLong(1);
-    if (!one)
-        return NULL;
-
-    new_index = PyNumber_Add(r->index, one);
-    Py_DECREF(one);
+    new_index = PyNumber_Add(r->index, _PyLong_One);
     if (!new_index)
         return NULL;
 
@@ -1122,23 +1069,15 @@
     if (it == NULL)
         return NULL;
 
-    /* Do all initialization here, so we can DECREF on failure. */
     it->start = r->start;
     it->step = r->step;
     it->len = r->length;
+    it->index = _PyLong_Zero;
     Py_INCREF(it->start);
     Py_INCREF(it->step);
     Py_INCREF(it->len);
-
-    it->index = PyLong_FromLong(0);
-    if (!it->index)
-        goto create_failure;
-
+    Py_INCREF(it->index);
     return (PyObject *)it;
-
-create_failure:
-    Py_DECREF(it);
-    return NULL;
 }
 
 static PyObject *
@@ -1146,7 +1085,7 @@
 {
     rangeobject *range = (rangeobject*) seq;
     longrangeiterobject *it;
-    PyObject *one, *sum, *diff, *product;
+    PyObject *sum, *diff, *product;
     long lstart, lstop, lstep, new_start, new_stop;
     unsigned long ulen;
 
@@ -1220,12 +1159,7 @@
     it->len = range->length;
     Py_INCREF(it->len);
 
-    one = PyLong_FromLong(1);
-    if (!one)
-        goto create_failure;
-
-    diff = PyNumber_Subtract(it->len, one);
-    Py_DECREF(one);
+    diff = PyNumber_Subtract(it->len, _PyLong_One);
     if (!diff)
         goto create_failure;
 
@@ -1244,10 +1178,8 @@
     if (!it->step)
         goto create_failure;
 
-    it->index = PyLong_FromLong(0);
-    if (!it->index)
-        goto create_failure;
-
+    it->index = _PyLong_Zero;
+    Py_INCREF(it->index);
     return (PyObject *)it;
 
 create_failure:
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index d41ac10..ebc4464 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -374,9 +374,8 @@
 
     /* Convert step to an integer; raise for zero step. */
     if (self->step == Py_None) {
-        step = PyLong_FromLong(1L);
-        if (step == NULL)
-            goto error;
+        step = _PyLong_One;
+        Py_INCREF(step);
         step_is_negative = 0;
     }
     else {
@@ -404,10 +403,8 @@
             goto error;
     }
     else {
-        lower = PyLong_FromLong(0L);
-        if (lower == NULL)
-            goto error;
-
+        lower = _PyLong_Zero;
+        Py_INCREF(lower);
         upper = length;
         Py_INCREF(upper);
     }