summaryrefslogtreecommitdiff
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-03-30 09:09:41 +0300
committerGitHub <noreply@github.com>2017-03-30 09:09:41 +0300
commitba85d69a3e3610bdd05f0dd372cf4ebca178c7fb (patch)
treefe0766c34601880610c3399a8f01c35ab6e8fe8e /Objects
parente6911a44f69c0d302db60f49952a9cf69da69a2b (diff)
downloadcpython-git-ba85d69a3e3610bdd05f0dd372cf4ebca178c7fb.tar.gz
bpo-29878: Add global instances of int for 0 and 1. (#852)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/complexobject.c2
-rw-r--r--Objects/enumobject.c8
-rw-r--r--Objects/floatobject.c14
-rw-r--r--Objects/longobject.c47
-rw-r--r--Objects/rangeobject.c144
-rw-r--r--Objects/sliceobject.c11
6 files changed, 69 insertions, 157 deletions
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 5ebb50435e..9a416a873a 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -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.
diff --git a/Objects/enumobject.c b/Objects/enumobject.c
index 480768fbde..3eb1736200 100644
--- a/Objects/enumobject.c
+++ b/Objects/enumobject.c
@@ -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;
@@ -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;
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 86c3c88d64..87af0ba650 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -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;
@@ -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);
@@ -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)) */
@@ -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.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 0bf6ae6acc..459eed9ab0 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -31,6 +31,9 @@ _Py_IDENTIFIER(big);
(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 @@ long_divrem(PyLongObject *a, PyLongObject *b,
(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 @@ l_divmod(PyLongObject *v, PyLongObject *w,
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 @@ l_divmod(PyLongObject *v, PyLongObject *w,
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 @@ long_invert(PyLongObject *v)
{
/* 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 @@ PyObject *
_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 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
/* 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 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
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 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
/* 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 @@ _PyLong_Init(void)
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 @@ PyLong_Fini(void)
/* 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 820fea5623..554528db0d 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -83,17 +83,10 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
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 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
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 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
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 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
Py_XDECREF(tmp2);
Py_XDECREF(diff);
Py_XDECREF(tmp1);
- Py_XDECREF(one);
return NULL;
}
@@ -253,10 +236,6 @@ compute_range_item(rangeobject *r, PyObject *arg)
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 @@ compute_range_item(rangeobject *r, PyObject *arg)
* 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 @@ compute_range_item(rangeobject *r, PyObject *arg)
* <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 @@ range_contains_long(rangeobject *r, PyObject *ob)
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 @@ range_contains_long(rangeobject *r, PyObject *ob)
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 @@ static int
range_equals(rangeobject *r0, rangeobject *r1)
{
int cmp_result;
- PyObject *one;
if (r0 == r1)
return 1;
@@ -453,11 +422,7 @@ range_equals(rangeobject *r0, rangeobject *r1)
/* 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 @@ range_hash(rangeobject *r)
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 @@ range_count(rangeobject *r, PyObject *ob)
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 @@ longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
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;
+ }
+ else {
+ cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
+ if (cmp < 0)
+ return NULL;
+ if (cmp > 0)
+ state = r->len;
}
- 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;
-
Py_INCREF(state);
Py_XSETREF(r->index, state);
Py_RETURN_NONE;
@@ -1019,16 +971,11 @@ longrangeiter_dealloc(longrangeiterobject *r)
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 @@ range_iter(PyObject *seq)
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 @@ range_reverse(PyObject *seq)
{
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 @@ long_range:
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 @@ long_range:
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 d41ac105f6..ebc44642fe 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -374,9 +374,8 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
/* 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 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
goto error;
}
else {
- lower = PyLong_FromLong(0L);
- if (lower == NULL)
- goto error;
-
+ lower = _PyLong_Zero;
+ Py_INCREF(lower);
upper = length;
Py_INCREF(upper);
}