summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-03-30 06:09:41 (GMT)
committerGitHub <noreply@github.com>2017-03-30 06:09:41 (GMT)
commitba85d69a3e3610bdd05f0dd372cf4ebca178c7fb (patch)
treefe0766c34601880610c3399a8f01c35ab6e8fe8e /Modules
parente6911a44f69c0d302db60f49952a9cf69da69a2b (diff)
downloadcpython-ba85d69a3e3610bdd05f0dd372cf4ebca178c7fb.zip
cpython-ba85d69a3e3610bdd05f0dd372cf4ebca178c7fb.tar.gz
cpython-ba85d69a3e3610bdd05f0dd372cf4ebca178c7fb.tar.bz2
bpo-29878: Add global instances of int for 0 and 1. (#852)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_collectionsmodule.c20
-rw-r--r--Modules/_ctypes/_ctypes.c9
-rw-r--r--Modules/_datetimemodule.c8
-rw-r--r--Modules/_functoolsmodule.c9
-rw-r--r--Modules/_io/_iomodule.c4
-rw-r--r--Modules/_io/_iomodule.h1
-rw-r--r--Modules/_io/textio.c14
-rw-r--r--Modules/_sre.c2
-rw-r--r--Modules/itertoolsmodule.c18
9 files changed, 24 insertions, 61 deletions
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 @@ _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;
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 @@ _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):
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 @@ 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;
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 @@ 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;
}
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_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;
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_str_write;
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 @@ _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;
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 @@ 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);
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 @@ 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);