Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Include/longobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ PyAPI_FUNC(long) PyOS_strtol(const char *, char **, int);
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
Expand Down
20 changes: 4 additions & 16 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -2369,8 +2359,6 @@ _count_elements(PyObject *self, PyObject *args)
Py_XDECREF(key);
Py_XDECREF(newval);
Py_XDECREF(bound_get);
Py_XDECREF(zero);
Py_XDECREF(one);
if (PyErr_Occurred())
return NULL;
Py_RETURN_NONE;
Expand Down
9 changes: 3 additions & 6 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 3 additions & 5 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
9 changes: 1 addition & 8 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
4 changes: 0 additions & 4 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion Modules/_io/_iomodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
14 changes: 7 additions & 7 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 5 additions & 13 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
/*[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.
Expand Down
8 changes: 1 addition & 7 deletions Objects/enumobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ enum_traverse(enumobject *en, visitproc visit, void *arg)
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;
Expand All @@ -110,14 +109,9 @@ enum_next_long(enumobject *en, PyObject* next_item)
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;
Expand Down
14 changes: 4 additions & 10 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ float_richcompare(PyObject *v, PyObject *w, int op)
double fracpart;
double intpart;
PyObject *result = NULL;
PyObject *one = NULL;
PyObject *vv = NULL;
PyObject *ww = w;

Expand All @@ -466,23 +465,19 @@ float_richcompare(PyObject *v, PyObject *w, int op)
*/
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);
Expand All @@ -496,7 +491,6 @@ float_richcompare(PyObject *v, PyObject *w, int op)
Error:
Py_XDECREF(vv);
Py_XDECREF(ww);
Py_XDECREF(one);
return result;
}
} /* else if (PyLong_Check(w)) */
Expand Down Expand Up @@ -1617,7 +1611,7 @@ float_subtype_new(PyTypeObject *type, PyObject *x);
/*[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.
Expand Down
Loading