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

diff --git a/Include/longobject.h b/Include/longobject.h
index ac09a00..7bdd047 100644
--- a/Include/longobject.h
+++ b/Include/longobject.h
@@ -209,6 +209,11 @@
 PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *);
 #endif /* !Py_LIMITED_API */
 
+#ifndef Py_LIMITED_API
+PyAPI_DATA(PyObject *) _PyLong_Zero;
+PyAPI_DATA(PyObject *) _PyLong_One;
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 44e9e11..b052535 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -2267,8 +2267,6 @@
     PyObject *it, *iterable, *mapping, *oldval;
     PyObject *newval = NULL;
     PyObject *key = NULL;
-    PyObject *zero = NULL;
-    PyObject *one = NULL;
     PyObject *bound_get = NULL;
     PyObject *mapping_get;
     PyObject *dict_get;
@@ -2282,10 +2280,6 @@
     if (it == NULL)
         return NULL;
 
-    one = PyLong_FromLong(1);
-    if (one == NULL)
-        goto done;
-
     /* Only take the fast path when get() and __setitem__()
      * have not been overridden.
      */
@@ -2325,10 +2319,10 @@
             if (oldval == NULL) {
                 if (PyErr_Occurred())
                     goto done;
-                if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) < 0)
+                if (_PyDict_SetItem_KnownHash(mapping, key, _PyLong_One, hash) < 0)
                     goto done;
             } else {
-                newval = PyNumber_Add(oldval, one);
+                newval = PyNumber_Add(oldval, _PyLong_One);
                 if (newval == NULL)
                     goto done;
                 if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
@@ -2342,18 +2336,14 @@
         if (bound_get == NULL)
             goto done;
 
-        zero = PyLong_FromLong(0);
-        if (zero == NULL)
-            goto done;
-
         while (1) {
             key = PyIter_Next(it);
             if (key == NULL)
                 break;
-            oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL);
+            oldval = PyObject_CallFunctionObjArgs(bound_get, key, _PyLong_Zero, NULL);
             if (oldval == NULL)
                 break;
-            newval = PyNumber_Add(oldval, one);
+            newval = PyNumber_Add(oldval, _PyLong_One);
             Py_DECREF(oldval);
             if (newval == NULL)
                 break;
@@ -2369,8 +2359,6 @@
     Py_XDECREF(key);
     Py_XDECREF(newval);
     Py_XDECREF(bound_get);
-    Py_XDECREF(zero);
-    Py_XDECREF(one);
     if (PyErr_Occurred())
         return NULL;
     Py_RETURN_NONE;
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 9d42109..9e662f9 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -3606,12 +3606,9 @@
         case PARAMFLAG_FIN | PARAMFLAG_FLCID:
             /* ['in', 'lcid'] parameter.  Always taken from defval,
              if given, else the integer 0. */
-            if (defval == NULL) {
-                defval = PyLong_FromLong(0);
-                if (defval == NULL)
-                    goto error;
-            } else
-                Py_INCREF(defval);
+            if (defval == NULL)
+                defval = _PyLong_Zero;
+            Py_INCREF(defval);
             PyTuple_SET_ITEM(callargs, i, defval);
             break;
         case (PARAMFLAG_FIN | PARAMFLAG_FOUT):
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 1803d85..3661c78 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -1481,7 +1481,6 @@
  */
 
 /* Conversion factors. */
-static PyObject *one = NULL;      /* 1 */
 static PyObject *us_per_ms = NULL;      /* 1000 */
 static PyObject *us_per_second = NULL;  /* 1000000 */
 static PyObject *us_per_minute = NULL;  /* 1e6 * 60 as Python int */
@@ -2201,7 +2200,7 @@
         goto Done
 
     if (us) {
-        y = accum("microseconds", x, us, one, &leftover_us);
+        y = accum("microseconds", x, us, _PyLong_One, &leftover_us);
         CLEANUP;
     }
     if (ms) {
@@ -2241,7 +2240,7 @@
              * is odd. Note that x is odd when it's last bit is 1. The
              * code below uses bitwise and operation to check the last
              * bit. */
-            temp = PyNumber_And(x, one);  /* temp <- x & 1 */
+            temp = PyNumber_And(x, _PyLong_One);  /* temp <- x & 1 */
             if (temp == NULL) {
                 Py_DECREF(x);
                 goto Done;
@@ -5839,12 +5838,11 @@
     Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1);
     assert(DI100Y == days_before_year(100+1));
 
-    one = PyLong_FromLong(1);
     us_per_ms = PyLong_FromLong(1000);
     us_per_second = PyLong_FromLong(1000000);
     us_per_minute = PyLong_FromLong(60000000);
     seconds_per_day = PyLong_FromLong(24 * 3600);
-    if (one == NULL || us_per_ms == NULL || us_per_second == NULL ||
+    if (us_per_ms == NULL || us_per_second == NULL ||
         us_per_minute == NULL || seconds_per_day == NULL)
         return NULL;
 
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 567300e..da1d2e1 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -529,15 +529,8 @@
     PyObject *y;
     PyObject *compare;
     PyObject *answer;
-    static PyObject *zero;
     PyObject* stack[2];
 
-    if (zero == NULL) {
-        zero = PyLong_FromLong(0);
-        if (!zero)
-            return NULL;
-    }
-
     if (Py_TYPE(other) != &keyobject_type){
         PyErr_Format(PyExc_TypeError, "other argument must be K instance");
         return NULL;
@@ -561,7 +554,7 @@
         return NULL;
     }
 
-    answer = PyObject_RichCompare(res, zero, op);
+    answer = PyObject_RichCompare(res, _PyLong_Zero, op);
     Py_DECREF(res);
     return answer;
 }
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 5d804ec..0b795c8 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -53,7 +53,6 @@
 
 PyObject *_PyIO_empty_str;
 PyObject *_PyIO_empty_bytes;
-PyObject *_PyIO_zero;
 
 PyDoc_STRVAR(module_doc,
 "The io module provides the Python interfaces to stream handling. The\n"
@@ -790,9 +789,6 @@
     if (!_PyIO_empty_bytes &&
         !(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
         goto fail;
-    if (!_PyIO_zero &&
-        !(_PyIO_zero = PyLong_FromLong(0L)))
-        goto fail;
 
     state->initialized = 1;
 
diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h
index daaebd2..b5f0f57 100644
--- a/Modules/_io/_iomodule.h
+++ b/Modules/_io/_iomodule.h
@@ -183,6 +183,5 @@
 
 extern PyObject *_PyIO_empty_str;
 extern PyObject *_PyIO_empty_bytes;
-extern PyObject *_PyIO_zero;
 
 extern PyTypeObject _PyBytesIOBuffer_Type;
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index fce662a..0211ad9 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -1078,7 +1078,7 @@
         if (cookieObj == NULL)
             goto error;
 
-        cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
+        cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
         Py_DECREF(cookieObj);
         if (cmp < 0) {
             goto error;
@@ -1087,7 +1087,7 @@
         if (cmp == 0) {
             self->encoding_start_of_stream = 0;
             res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate,
-                                             _PyIO_zero, NULL);
+                                             _PyLong_Zero, NULL);
             if (res == NULL)
                 goto error;
             Py_DECREF(res);
@@ -2030,7 +2030,7 @@
     }
     else {
         res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate,
-                                         _PyIO_zero, NULL);
+                                         _PyLong_Zero, NULL);
         self->encoding_start_of_stream = 0;
     }
     if (res == NULL)
@@ -2075,7 +2075,7 @@
 
     if (whence == 1) {
         /* seek relative to current position */
-        cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
+        cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
         if (cmp < 0)
             goto fail;
 
@@ -2094,7 +2094,7 @@
     }
     else if (whence == 2) {
         /* seek relative to end of file */
-        cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
+        cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
         if (cmp < 0)
             goto fail;
 
@@ -2123,7 +2123,7 @@
             goto fail;
         if (self->encoder) {
             /* If seek() == 0, we are at the start of stream, otherwise not */
-            cmp = PyObject_RichCompareBool(res, _PyIO_zero, Py_EQ);
+            cmp = PyObject_RichCompareBool(res, _PyLong_Zero, Py_EQ);
             if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) {
                 Py_DECREF(res);
                 goto fail;
@@ -2137,7 +2137,7 @@
         goto fail;
     }
 
-    cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_LT);
+    cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_LT);
     if (cmp < 0)
         goto fail;
 
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 2a2fd27..84c47bb 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -2041,7 +2041,7 @@
 
     switch (size) {
     case 0:
-        result = match_getslice(self, Py_False, Py_None);
+        result = match_getslice(self, _PyLong_Zero, Py_None);
         break;
     case 1:
         result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index f867252..e91a029 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -3965,24 +3965,16 @@
                 fast_mode = 0;
             }
         }
-        Py_INCREF(long_cnt);
     } else {
         cnt = 0;
-        long_cnt = PyLong_FromLong(0);
-        if (long_cnt == NULL) {
-            return NULL;
-        }
+        long_cnt = _PyLong_Zero;
     }
+    Py_INCREF(long_cnt);
 
     /* If not specified, step defaults to 1 */
-    if (long_step == NULL) {
-        long_step = PyLong_FromLong(1);
-        if (long_step == NULL) {
-            Py_DECREF(long_cnt);
-            return NULL;
-        }
-    } else
-        Py_INCREF(long_step);
+    if (long_step == NULL)
+        long_step = _PyLong_One;
+    Py_INCREF(long_step);
 
     assert(long_cnt != NULL && long_step != NULL);
 
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);
     }
diff --git a/Python/_warnings.c b/Python/_warnings.c
index 67f4c6b..add72e4 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -287,20 +287,15 @@
 update_registry(PyObject *registry, PyObject *text, PyObject *category,
                 int add_zero)
 {
-    PyObject *altkey, *zero = NULL;
+    PyObject *altkey;
     int rc;
 
-    if (add_zero) {
-        zero = PyLong_FromLong(0);
-        if (zero == NULL)
-            return -1;
-        altkey = PyTuple_Pack(3, text, category, zero);
-    }
+    if (add_zero)
+        altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
     else
         altkey = PyTuple_Pack(2, text, category);
 
     rc = already_warned(registry, altkey, 1);
-    Py_XDECREF(zero);
     Py_XDECREF(altkey);
     return rc;
 }
@@ -1130,7 +1125,6 @@
     static PyObject *default_str = NULL;
     static PyObject *always_str = NULL;
     PyObject *action_obj = NULL;
-    PyObject *lineno, *result;
 
     if (!strcmp(action, "ignore")) {
         if (ignore_str == NULL) {
@@ -1169,12 +1163,7 @@
     }
 
     /* This assumes the line number is zero for now. */
-    lineno = PyLong_FromLong(0);
-    if (lineno == NULL)
-        return NULL;
-    result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno);
-    Py_DECREF(lineno);
-    return result;
+    return PyTuple_Pack(5, action_obj, Py_None, category, Py_None, _PyLong_Zero);
 }
 
 static PyObject *
diff --git a/Python/compile.c b/Python/compile.c
index 064364e..b630863 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -561,7 +561,7 @@
     if (u->u_ste->ste_needs_class_closure) {
         /* Cook up an implicit __class__ cell. */
         _Py_IDENTIFIER(__class__);
-        PyObject *tuple, *name, *zero;
+        PyObject *tuple, *name;
         int res;
         assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
         assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
@@ -575,15 +575,8 @@
             compiler_unit_free(u);
             return 0;
         }
-        zero = PyLong_FromLong(0);
-        if (!zero) {
-            Py_DECREF(tuple);
-            compiler_unit_free(u);
-            return 0;
-        }
-        res = PyDict_SetItem(u->u_cellvars, tuple, zero);
+        res = PyDict_SetItem(u->u_cellvars, tuple, _PyLong_Zero);
         Py_DECREF(tuple);
-        Py_DECREF(zero);
         if (res < 0) {
             compiler_unit_free(u);
             return 0;
@@ -2596,14 +2589,8 @@
     for (i = 0; i < n; i++) {
         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
         int r;
-        PyObject *level;
 
-        level = PyLong_FromLong(0);
-        if (level == NULL)
-            return 0;
-
-        ADDOP_O(c, LOAD_CONST, level, consts);
-        Py_DECREF(level);
+        ADDOP_O(c, LOAD_CONST, _PyLong_Zero, consts);
         ADDOP_O(c, LOAD_CONST, Py_None, consts);
         ADDOP_NAME(c, IMPORT_NAME, alias->name, names);