Skip to content

Commit ba85d69

Browse files
bpo-29878: Add global instances of int for 0 and 1. (python#852)
1 parent e6911a4 commit ba85d69

File tree

18 files changed

+105
-249
lines changed

18 files changed

+105
-249
lines changed

Include/longobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ PyAPI_FUNC(long) PyOS_strtol(const char *, char **, int);
209209
PyAPI_FUNC(PyObject *) _PyLong_GCD(PyObject *, PyObject *);
210210
#endif /* !Py_LIMITED_API */
211211

212+
#ifndef Py_LIMITED_API
213+
PyAPI_DATA(PyObject *) _PyLong_Zero;
214+
PyAPI_DATA(PyObject *) _PyLong_One;
215+
#endif
216+
212217
#ifdef __cplusplus
213218
}
214219
#endif

Modules/_collectionsmodule.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,8 +2267,6 @@ _count_elements(PyObject *self, PyObject *args)
22672267
PyObject *it, *iterable, *mapping, *oldval;
22682268
PyObject *newval = NULL;
22692269
PyObject *key = NULL;
2270-
PyObject *zero = NULL;
2271-
PyObject *one = NULL;
22722270
PyObject *bound_get = NULL;
22732271
PyObject *mapping_get;
22742272
PyObject *dict_get;
@@ -2282,10 +2280,6 @@ _count_elements(PyObject *self, PyObject *args)
22822280
if (it == NULL)
22832281
return NULL;
22842282

2285-
one = PyLong_FromLong(1);
2286-
if (one == NULL)
2287-
goto done;
2288-
22892283
/* Only take the fast path when get() and __setitem__()
22902284
* have not been overridden.
22912285
*/
@@ -2325,10 +2319,10 @@ _count_elements(PyObject *self, PyObject *args)
23252319
if (oldval == NULL) {
23262320
if (PyErr_Occurred())
23272321
goto done;
2328-
if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) < 0)
2322+
if (_PyDict_SetItem_KnownHash(mapping, key, _PyLong_One, hash) < 0)
23292323
goto done;
23302324
} else {
2331-
newval = PyNumber_Add(oldval, one);
2325+
newval = PyNumber_Add(oldval, _PyLong_One);
23322326
if (newval == NULL)
23332327
goto done;
23342328
if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
@@ -2342,18 +2336,14 @@ _count_elements(PyObject *self, PyObject *args)
23422336
if (bound_get == NULL)
23432337
goto done;
23442338

2345-
zero = PyLong_FromLong(0);
2346-
if (zero == NULL)
2347-
goto done;
2348-
23492339
while (1) {
23502340
key = PyIter_Next(it);
23512341
if (key == NULL)
23522342
break;
2353-
oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL);
2343+
oldval = PyObject_CallFunctionObjArgs(bound_get, key, _PyLong_Zero, NULL);
23542344
if (oldval == NULL)
23552345
break;
2356-
newval = PyNumber_Add(oldval, one);
2346+
newval = PyNumber_Add(oldval, _PyLong_One);
23572347
Py_DECREF(oldval);
23582348
if (newval == NULL)
23592349
break;
@@ -2369,8 +2359,6 @@ _count_elements(PyObject *self, PyObject *args)
23692359
Py_XDECREF(key);
23702360
Py_XDECREF(newval);
23712361
Py_XDECREF(bound_get);
2372-
Py_XDECREF(zero);
2373-
Py_XDECREF(one);
23742362
if (PyErr_Occurred())
23752363
return NULL;
23762364
Py_RETURN_NONE;

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3606,12 +3606,9 @@ _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
36063606
case PARAMFLAG_FIN | PARAMFLAG_FLCID:
36073607
/* ['in', 'lcid'] parameter. Always taken from defval,
36083608
if given, else the integer 0. */
3609-
if (defval == NULL) {
3610-
defval = PyLong_FromLong(0);
3611-
if (defval == NULL)
3612-
goto error;
3613-
} else
3614-
Py_INCREF(defval);
3609+
if (defval == NULL)
3610+
defval = _PyLong_Zero;
3611+
Py_INCREF(defval);
36153612
PyTuple_SET_ITEM(callargs, i, defval);
36163613
break;
36173614
case (PARAMFLAG_FIN | PARAMFLAG_FOUT):

Modules/_datetimemodule.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,6 @@ cmperror(PyObject *a, PyObject *b)
14811481
*/
14821482

14831483
/* Conversion factors. */
1484-
static PyObject *one = NULL; /* 1 */
14851484
static PyObject *us_per_ms = NULL; /* 1000 */
14861485
static PyObject *us_per_second = NULL; /* 1000000 */
14871486
static PyObject *us_per_minute = NULL; /* 1e6 * 60 as Python int */
@@ -2201,7 +2200,7 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
22012200
goto Done
22022201

22032202
if (us) {
2204-
y = accum("microseconds", x, us, one, &leftover_us);
2203+
y = accum("microseconds", x, us, _PyLong_One, &leftover_us);
22052204
CLEANUP;
22062205
}
22072206
if (ms) {
@@ -2241,7 +2240,7 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
22412240
* is odd. Note that x is odd when it's last bit is 1. The
22422241
* code below uses bitwise and operation to check the last
22432242
* bit. */
2244-
temp = PyNumber_And(x, one); /* temp <- x & 1 */
2243+
temp = PyNumber_And(x, _PyLong_One); /* temp <- x & 1 */
22452244
if (temp == NULL) {
22462245
Py_DECREF(x);
22472246
goto Done;
@@ -5839,12 +5838,11 @@ PyInit__datetime(void)
58395838
Py_BUILD_ASSERT(DI100Y == 25 * DI4Y - 1);
58405839
assert(DI100Y == days_before_year(100+1));
58415840

5842-
one = PyLong_FromLong(1);
58435841
us_per_ms = PyLong_FromLong(1000);
58445842
us_per_second = PyLong_FromLong(1000000);
58455843
us_per_minute = PyLong_FromLong(60000000);
58465844
seconds_per_day = PyLong_FromLong(24 * 3600);
5847-
if (one == NULL || us_per_ms == NULL || us_per_second == NULL ||
5845+
if (us_per_ms == NULL || us_per_second == NULL ||
58485846
us_per_minute == NULL || seconds_per_day == NULL)
58495847
return NULL;
58505848

Modules/_functoolsmodule.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -529,15 +529,8 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
529529
PyObject *y;
530530
PyObject *compare;
531531
PyObject *answer;
532-
static PyObject *zero;
533532
PyObject* stack[2];
534533

535-
if (zero == NULL) {
536-
zero = PyLong_FromLong(0);
537-
if (!zero)
538-
return NULL;
539-
}
540-
541534
if (Py_TYPE(other) != &keyobject_type){
542535
PyErr_Format(PyExc_TypeError, "other argument must be K instance");
543536
return NULL;
@@ -561,7 +554,7 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
561554
return NULL;
562555
}
563556

564-
answer = PyObject_RichCompare(res, zero, op);
557+
answer = PyObject_RichCompare(res, _PyLong_Zero, op);
565558
Py_DECREF(res);
566559
return answer;
567560
}

Modules/_io/_iomodule.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ PyObject *_PyIO_str_write;
5353

5454
PyObject *_PyIO_empty_str;
5555
PyObject *_PyIO_empty_bytes;
56-
PyObject *_PyIO_zero;
5756

5857
PyDoc_STRVAR(module_doc,
5958
"The io module provides the Python interfaces to stream handling. The\n"
@@ -790,9 +789,6 @@ PyInit__io(void)
790789
if (!_PyIO_empty_bytes &&
791790
!(_PyIO_empty_bytes = PyBytes_FromStringAndSize(NULL, 0)))
792791
goto fail;
793-
if (!_PyIO_zero &&
794-
!(_PyIO_zero = PyLong_FromLong(0L)))
795-
goto fail;
796792

797793
state->initialized = 1;
798794

Modules/_io/_iomodule.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,5 @@ extern PyObject *_PyIO_str_write;
183183

184184
extern PyObject *_PyIO_empty_str;
185185
extern PyObject *_PyIO_empty_bytes;
186-
extern PyObject *_PyIO_zero;
187186

188187
extern PyTypeObject _PyBytesIOBuffer_Type;

Modules/_io/textio.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
10781078
if (cookieObj == NULL)
10791079
goto error;
10801080

1081-
cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
1081+
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
10821082
Py_DECREF(cookieObj);
10831083
if (cmp < 0) {
10841084
goto error;
@@ -1087,7 +1087,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
10871087
if (cmp == 0) {
10881088
self->encoding_start_of_stream = 0;
10891089
res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate,
1090-
_PyIO_zero, NULL);
1090+
_PyLong_Zero, NULL);
10911091
if (res == NULL)
10921092
goto error;
10931093
Py_DECREF(res);
@@ -2030,7 +2030,7 @@ _textiowrapper_encoder_reset(textio *self, int start_of_stream)
20302030
}
20312031
else {
20322032
res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate,
2033-
_PyIO_zero, NULL);
2033+
_PyLong_Zero, NULL);
20342034
self->encoding_start_of_stream = 0;
20352035
}
20362036
if (res == NULL)
@@ -2075,7 +2075,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
20752075

20762076
if (whence == 1) {
20772077
/* seek relative to current position */
2078-
cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
2078+
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
20792079
if (cmp < 0)
20802080
goto fail;
20812081

@@ -2094,7 +2094,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
20942094
}
20952095
else if (whence == 2) {
20962096
/* seek relative to end of file */
2097-
cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
2097+
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
20982098
if (cmp < 0)
20992099
goto fail;
21002100

@@ -2123,7 +2123,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
21232123
goto fail;
21242124
if (self->encoder) {
21252125
/* If seek() == 0, we are at the start of stream, otherwise not */
2126-
cmp = PyObject_RichCompareBool(res, _PyIO_zero, Py_EQ);
2126+
cmp = PyObject_RichCompareBool(res, _PyLong_Zero, Py_EQ);
21272127
if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) {
21282128
Py_DECREF(res);
21292129
goto fail;
@@ -2137,7 +2137,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
21372137
goto fail;
21382138
}
21392139

2140-
cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_LT);
2140+
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_LT);
21412141
if (cmp < 0)
21422142
goto fail;
21432143

Modules/_sre.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,7 @@ match_group(MatchObject* self, PyObject* args)
20412041

20422042
switch (size) {
20432043
case 0:
2044-
result = match_getslice(self, Py_False, Py_None);
2044+
result = match_getslice(self, _PyLong_Zero, Py_None);
20452045
break;
20462046
case 1:
20472047
result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);

Modules/itertoolsmodule.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,24 +3965,16 @@ count_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
39653965
fast_mode = 0;
39663966
}
39673967
}
3968-
Py_INCREF(long_cnt);
39693968
} else {
39703969
cnt = 0;
3971-
long_cnt = PyLong_FromLong(0);
3972-
if (long_cnt == NULL) {
3973-
return NULL;
3974-
}
3970+
long_cnt = _PyLong_Zero;
39753971
}
3972+
Py_INCREF(long_cnt);
39763973

39773974
/* If not specified, step defaults to 1 */
3978-
if (long_step == NULL) {
3979-
long_step = PyLong_FromLong(1);
3980-
if (long_step == NULL) {
3981-
Py_DECREF(long_cnt);
3982-
return NULL;
3983-
}
3984-
} else
3985-
Py_INCREF(long_step);
3975+
if (long_step == NULL)
3976+
long_step = _PyLong_One;
3977+
Py_INCREF(long_step);
39863978

39873979
assert(long_cnt != NULL && long_step != NULL);
39883980

0 commit comments

Comments
 (0)