Skip to content

Commit c9bc290

Browse files
authored
bpo-42161: Use _PyLong_GetZero() and _PyLong_GetOne() (GH-22995)
Use _PyLong_GetZero() and _PyLong_GetOne() in Objects/ and Python/ directories.
1 parent 303aac8 commit c9bc290

File tree

9 files changed

+68
-40
lines changed

9 files changed

+68
-40
lines changed

Objects/clinic/complexobject.c.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/floatobject.c.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/complexobject.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/* Submitted by Jim Hugunin */
77

88
#include "Python.h"
9+
#include "pycore_long.h" // _PyLong_GetZero()
910
#include "pycore_object.h" // _PyObject_Init()
1011
#include "structmember.h" // PyMemberDef
1112

@@ -870,7 +871,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
870871
/*[clinic input]
871872
@classmethod
872873
complex.__new__ as complex_new
873-
real as r: object(c_default="_PyLong_Zero") = 0
874+
real as r: object(c_default="NULL") = 0
874875
imag as i: object(c_default="NULL") = 0
875876
876877
Create a complex number from a real part and an optional imaginary part.
@@ -880,7 +881,7 @@ This is equivalent to (real + imag*1j) where imag defaults to 0.
880881

881882
static PyObject *
882883
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
883-
/*[clinic end generated code: output=b6c7dd577b537dc1 input=6f6b0bedba29bcb5]*/
884+
/*[clinic end generated code: output=b6c7dd577b537dc1 input=f4c667f2596d4fd1]*/
884885
{
885886
PyObject *tmp;
886887
PyNumberMethods *nbr, *nbi = NULL;
@@ -889,6 +890,10 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
889890
int cr_is_complex = 0;
890891
int ci_is_complex = 0;
891892

893+
if (r == NULL) {
894+
r = _PyLong_GetZero();
895+
}
896+
892897
/* Special-case for a single argument when type(arg) is complex. */
893898
if (PyComplex_CheckExact(r) && i == NULL &&
894899
type == &PyComplex_Type) {

Objects/enumobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* enumerate object */
22

33
#include "Python.h"
4+
#include "pycore_long.h" // _PyLong_GetOne()
45

56
#include "clinic/enumobject.c.h"
67

@@ -115,7 +116,7 @@ enum_next_long(enumobject *en, PyObject* next_item)
115116
}
116117
next_index = en->en_longindex;
117118
assert(next_index != NULL);
118-
stepped_up = PyNumber_Add(next_index, _PyLong_One);
119+
stepped_up = PyNumber_Add(next_index, _PyLong_GetOne());
119120
if (stepped_up == NULL) {
120121
Py_DECREF(next_item);
121122
return NULL;

Objects/floatobject.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Python.h"
77
#include "pycore_dtoa.h" // _Py_dg_dtoa()
88
#include "pycore_interp.h" // _PyInterpreterState.float_state
9+
#include "pycore_long.h" // _PyLong_GetOne()
910
#include "pycore_object.h" // _PyObject_Init()
1011
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1112

@@ -504,7 +505,7 @@ float_richcompare(PyObject *v, PyObject *w, int op)
504505
Py_DECREF(vv);
505506
vv = temp;
506507

507-
temp = PyNumber_Or(vv, _PyLong_One);
508+
temp = PyNumber_Or(vv, _PyLong_GetOne());
508509
if (temp == NULL)
509510
goto Error;
510511
Py_DECREF(vv);
@@ -1605,18 +1606,26 @@ float_subtype_new(PyTypeObject *type, PyObject *x);
16051606
/*[clinic input]
16061607
@classmethod
16071608
float.__new__ as float_new
1608-
x: object(c_default="_PyLong_Zero") = 0
1609+
x: object(c_default="NULL") = 0
16091610
/
16101611
16111612
Convert a string or number to a floating point number, if possible.
16121613
[clinic start generated code]*/
16131614

16141615
static PyObject *
16151616
float_new_impl(PyTypeObject *type, PyObject *x)
1616-
/*[clinic end generated code: output=ccf1e8dc460ba6ba input=540ee77c204ff87a]*/
1617+
/*[clinic end generated code: output=ccf1e8dc460ba6ba input=f43661b7de03e9d8]*/
16171618
{
1618-
if (type != &PyFloat_Type)
1619+
if (type != &PyFloat_Type) {
1620+
if (x == NULL) {
1621+
x = _PyLong_GetZero();
1622+
}
16191623
return float_subtype_new(type, x); /* Wimp out */
1624+
}
1625+
1626+
if (x == NULL) {
1627+
return PyFloat_FromDouble(0.0);
1628+
}
16201629
/* If it's a string, but not a string subclass, use
16211630
PyFloat_FromString. */
16221631
if (PyUnicode_CheckExact(x))
@@ -1662,7 +1671,7 @@ float_vectorcall(PyObject *type, PyObject * const*args,
16621671
return NULL;
16631672
}
16641673

1665-
PyObject *x = nargs >= 1 ? args[0] : _PyLong_Zero;
1674+
PyObject *x = nargs >= 1 ? args[0] : NULL;
16661675
return float_new_impl((PyTypeObject *)type, x);
16671676
}
16681677

Objects/rangeobject.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "Python.h"
44
#include "pycore_abstract.h" // _PyIndex_Check()
5+
#include "pycore_long.h" // _PyLong_GetZero()
56
#include "pycore_tuple.h" // _PyTuple_ITEMS()
67
#include "structmember.h" // PyMemberDef
78

@@ -105,10 +106,10 @@ range_from_array(PyTypeObject *type, PyObject *const *args, Py_ssize_t num_args)
105106
if (!stop) {
106107
return NULL;
107108
}
108-
Py_INCREF(_PyLong_Zero);
109-
start = _PyLong_Zero;
110-
Py_INCREF(_PyLong_One);
111-
step = _PyLong_One;
109+
start = _PyLong_GetZero();
110+
Py_INCREF(start);
111+
step = _PyLong_GetOne();
112+
Py_INCREF(step);
112113
break;
113114
case 0:
114115
PyErr_SetString(PyExc_TypeError,
@@ -190,7 +191,10 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
190191
PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
191192
/* holds sub-expression evaluations */
192193

193-
cmp_result = PyObject_RichCompareBool(step, _PyLong_Zero, Py_GT);
194+
PyObject *zero = _PyLong_GetZero(); // borrowed reference
195+
PyObject *one = _PyLong_GetOne(); // borrowed reference
196+
197+
cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
194198
if (cmp_result == -1)
195199
return NULL;
196200

@@ -212,19 +216,21 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
212216
Py_DECREF(step);
213217
if (cmp_result < 0)
214218
return NULL;
215-
return PyLong_FromLong(0);
219+
result = zero;
220+
Py_INCREF(result);
221+
return result;
216222
}
217223

218224
if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
219225
goto Fail;
220226

221-
if ((diff = PyNumber_Subtract(tmp1, _PyLong_One)) == NULL)
227+
if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
222228
goto Fail;
223229

224230
if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
225231
goto Fail;
226232

227-
if ((result = PyNumber_Add(tmp2, _PyLong_One)) == NULL)
233+
if ((result = PyNumber_Add(tmp2, one)) == NULL)
228234
goto Fail;
229235

230236
Py_DECREF(tmp2);
@@ -254,7 +260,7 @@ compute_item(rangeobject *r, PyObject *i)
254260
/* PyLong equivalent to:
255261
* return r->start + (i * r->step)
256262
*/
257-
if (r->step == _PyLong_One) {
263+
if (r->step == _PyLong_GetOne()) {
258264
result = PyNumber_Add(r->start, i);
259265
}
260266
else {
@@ -271,6 +277,7 @@ compute_item(rangeobject *r, PyObject *i)
271277
static PyObject *
272278
compute_range_item(rangeobject *r, PyObject *arg)
273279
{
280+
PyObject *zero = _PyLong_GetZero(); // borrowed reference
274281
int cmp_result;
275282
PyObject *i, *result;
276283

@@ -281,7 +288,7 @@ compute_range_item(rangeobject *r, PyObject *arg)
281288
* i = arg
282289
* }
283290
*/
284-
cmp_result = PyObject_RichCompareBool(arg, _PyLong_Zero, Py_LT);
291+
cmp_result = PyObject_RichCompareBool(arg, zero, Py_LT);
285292
if (cmp_result == -1) {
286293
return NULL;
287294
}
@@ -300,7 +307,7 @@ compute_range_item(rangeobject *r, PyObject *arg)
300307
* <report index out of bounds>
301308
* }
302309
*/
303-
cmp_result = PyObject_RichCompareBool(i, _PyLong_Zero, Py_LT);
310+
cmp_result = PyObject_RichCompareBool(i, zero, Py_LT);
304311
if (cmp_result == 0) {
305312
cmp_result = PyObject_RichCompareBool(i, r->length, Py_GE);
306313
}
@@ -375,14 +382,15 @@ compute_slice(rangeobject *r, PyObject *_slice)
375382
static int
376383
range_contains_long(rangeobject *r, PyObject *ob)
377384
{
385+
PyObject *zero = _PyLong_GetZero(); // borrowed reference
378386
int cmp1, cmp2, cmp3;
379387
PyObject *tmp1 = NULL;
380388
PyObject *tmp2 = NULL;
381389
int result = -1;
382390

383391
/* Check if the value can possibly be in the range. */
384392

385-
cmp1 = PyObject_RichCompareBool(r->step, _PyLong_Zero, Py_GT);
393+
cmp1 = PyObject_RichCompareBool(r->step, zero, Py_GT);
386394
if (cmp1 == -1)
387395
goto end;
388396
if (cmp1 == 1) { /* positive steps: start <= ob < stop */
@@ -409,7 +417,7 @@ range_contains_long(rangeobject *r, PyObject *ob)
409417
if (tmp2 == NULL)
410418
goto end;
411419
/* result = ((int(ob) - start) % step) == 0 */
412-
result = PyObject_RichCompareBool(tmp2, _PyLong_Zero, Py_EQ);
420+
result = PyObject_RichCompareBool(tmp2, zero, Py_EQ);
413421
end:
414422
Py_XDECREF(tmp1);
415423
Py_XDECREF(tmp2);
@@ -460,7 +468,7 @@ range_equals(rangeobject *r0, rangeobject *r1)
460468
/* Return False or error to the caller. */
461469
if (cmp_result != 1)
462470
return cmp_result;
463-
cmp_result = PyObject_RichCompareBool(r0->length, _PyLong_One, Py_EQ);
471+
cmp_result = PyObject_RichCompareBool(r0->length, _PyLong_GetOne(), Py_EQ);
464472
/* Return True or error to the caller. */
465473
if (cmp_result != 0)
466474
return cmp_result;
@@ -529,7 +537,7 @@ range_hash(rangeobject *r)
529537
else {
530538
Py_INCREF(r->start);
531539
PyTuple_SET_ITEM(t, 1, r->start);
532-
cmp_result = PyObject_RichCompareBool(r->length, _PyLong_One, Py_EQ);
540+
cmp_result = PyObject_RichCompareBool(r->length, _PyLong_GetOne(), Py_EQ);
533541
if (cmp_result == -1)
534542
goto end;
535543
if (cmp_result == 1) {
@@ -587,7 +595,7 @@ range_index(rangeobject *r, PyObject *ob)
587595
return NULL;
588596
}
589597

590-
if (r->step == _PyLong_One) {
598+
if (r->step == _PyLong_GetOne()) {
591599
return idx;
592600
}
593601

@@ -974,14 +982,15 @@ longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored))
974982
static PyObject *
975983
longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
976984
{
985+
PyObject *zero = _PyLong_GetZero(); // borrowed reference
977986
int cmp;
978987

979988
/* clip the value */
980-
cmp = PyObject_RichCompareBool(state, _PyLong_Zero, Py_LT);
989+
cmp = PyObject_RichCompareBool(state, zero, Py_LT);
981990
if (cmp < 0)
982991
return NULL;
983992
if (cmp > 0) {
984-
state = _PyLong_Zero;
993+
state = zero;
985994
}
986995
else {
987996
cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
@@ -1022,7 +1031,7 @@ longrangeiter_next(longrangeiterobject *r)
10221031
if (PyObject_RichCompareBool(r->index, r->len, Py_LT) != 1)
10231032
return NULL;
10241033

1025-
new_index = PyNumber_Add(r->index, _PyLong_One);
1034+
new_index = PyNumber_Add(r->index, _PyLong_GetOne());
10261035
if (!new_index)
10271036
return NULL;
10281037

@@ -1119,7 +1128,7 @@ range_iter(PyObject *seq)
11191128
it->start = r->start;
11201129
it->step = r->step;
11211130
it->len = r->length;
1122-
it->index = _PyLong_Zero;
1131+
it->index = _PyLong_GetZero();
11231132
Py_INCREF(it->start);
11241133
Py_INCREF(it->step);
11251134
Py_INCREF(it->len);
@@ -1207,7 +1216,7 @@ range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
12071216
it->len = range->length;
12081217
Py_INCREF(it->len);
12091218

1210-
diff = PyNumber_Subtract(it->len, _PyLong_One);
1219+
diff = PyNumber_Subtract(it->len, _PyLong_GetOne());
12111220
if (!diff)
12121221
goto create_failure;
12131222

@@ -1226,7 +1235,7 @@ range_reverse(PyObject *seq, PyObject *Py_UNUSED(ignored))
12261235
if (!it->step)
12271236
goto create_failure;
12281237

1229-
it->index = _PyLong_Zero;
1238+
it->index = _PyLong_GetZero();
12301239
Py_INCREF(it->index);
12311240
return (PyObject *)it;
12321241

Objects/sliceobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ this type and there is exactly one in existence.
1515

1616
#include "Python.h"
1717
#include "pycore_abstract.h" // _PyIndex_Check()
18+
#include "pycore_long.h" // _PyLong_GetZero()
1819
#include "pycore_object.h" // _PyObject_GC_TRACK()
1920
#include "structmember.h" // PyMemberDef
2021

@@ -388,7 +389,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
388389

389390
/* Convert step to an integer; raise for zero step. */
390391
if (self->step == Py_None) {
391-
step = _PyLong_One;
392+
step = _PyLong_GetOne();
392393
Py_INCREF(step);
393394
step_is_negative = 0;
394395
}
@@ -417,7 +418,7 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
417418
goto error;
418419
}
419420
else {
420-
lower = _PyLong_Zero;
421+
lower = _PyLong_GetZero();
421422
Py_INCREF(lower);
422423
upper = length;
423424
Py_INCREF(upper);

Python/_warnings.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Python.h"
22
#include "pycore_initconfig.h"
33
#include "pycore_interp.h" // PyInterpreterState.warnings
4+
#include "pycore_long.h" // _PyLong_GetZero()
45
#include "pycore_pyerrors.h"
56
#include "pycore_pystate.h" // _PyThreadState_GET()
67
#include "frameobject.h" // PyFrame_GetBack()
@@ -73,7 +74,7 @@ create_filter(PyObject *category, _Py_Identifier *id, const char *modname)
7374

7475
/* This assumes the line number is zero for now. */
7576
return PyTuple_Pack(5, action_str, Py_None,
76-
category, modname_obj, _PyLong_Zero);
77+
category, modname_obj, _PyLong_GetZero());
7778
}
7879
#endif
7980

@@ -472,7 +473,7 @@ update_registry(PyObject *registry, PyObject *text, PyObject *category,
472473
int rc;
473474

474475
if (add_zero)
475-
altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
476+
altkey = PyTuple_Pack(3, text, category, _PyLong_GetZero());
476477
else
477478
altkey = PyTuple_Pack(2, text, category);
478479

0 commit comments

Comments
 (0)