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

This commit is contained in:
Serhiy Storchaka 2017-03-30 09:09:41 +03:00 committed by GitHub
parent e6911a44f6
commit ba85d69a3e
18 changed files with 105 additions and 249 deletions

View file

@ -2267,8 +2267,6 @@ _count_elements(PyObject *self, PyObject *args)
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 @@ _count_elements(PyObject *self, PyObject *args)
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 @@ _count_elements(PyObject *self, PyObject *args)
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 @@ _count_elements(PyObject *self, PyObject *args)
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 @@ done:
Py_XDECREF(key);
Py_XDECREF(newval);
Py_XDECREF(bound_get);
Py_XDECREF(zero);
Py_XDECREF(one);
if (PyErr_Occurred())
return NULL;
Py_RETURN_NONE;

View file

@ -3606,12 +3606,9 @@ _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
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):

View file

@ -1481,7 +1481,6 @@ cmperror(PyObject *a, PyObject *b)
*/
/* 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 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
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 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
* 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 @@ PyInit__datetime(void)
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;

View file

@ -529,15 +529,8 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
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 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
return NULL;
}
answer = PyObject_RichCompare(res, zero, op);
answer = PyObject_RichCompare(res, _PyLong_Zero, op);
Py_DECREF(res);
return answer;
}

View file

@ -53,7 +53,6 @@ PyObject *_PyIO_str_write;
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 @@ PyInit__io(void)
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;

View file

@ -183,6 +183,5 @@ extern PyObject *_PyIO_str_write;
extern PyObject *_PyIO_empty_str;
extern PyObject *_PyIO_empty_bytes;
extern PyObject *_PyIO_zero;
extern PyTypeObject _PyBytesIOBuffer_Type;

View file

@ -1078,7 +1078,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
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 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
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 @@ _textiowrapper_encoder_reset(textio *self, int start_of_stream)
}
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 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
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 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
}
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 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
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 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;
}
cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_LT);
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_LT);
if (cmp < 0)
goto fail;

View file

@ -2041,7 +2041,7 @@ match_group(MatchObject* self, PyObject* args)
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);

View file

@ -3965,24 +3965,16 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
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);