Skip to content

bpo-42161: Use _PyLong_GetZero() and _PyLong_GetOne() #22998

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
Oct 27, 2020
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
4 changes: 2 additions & 2 deletions Modules/Setup
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ _symtable symtablemodule.c
#_elementtree -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c # elementtree accelerator
#_pickle _pickle.c # pickle accelerator
#_datetime _datetimemodule.c # datetime accelerator
#_zoneinfo _zoneinfo.c # zoneinfo accelerator
#_zoneinfo _zoneinfo.c -DPy_BUILD_CORE_MODULE # zoneinfo accelerator
#_bisect _bisectmodule.c # Bisection algorithms
#_heapq _heapqmodule.c -DPy_BUILD_CORE_MODULE # Heap queue algorithm
#_asyncio _asynciomodule.c # Fast asyncio Future
Expand Down Expand Up @@ -306,7 +306,7 @@ _symtable symtablemodule.c
# provided by the ncurses library. e.g. on Linux, link with -lncurses
# instead of -lcurses).

#_curses _cursesmodule.c -lcurses -ltermcap
#_curses _cursesmodule.c -lcurses -ltermcap -DPy_BUILD_CORE_MODULE
# Wrapper for the panel library that's part of ncurses and SYSV curses.
#_curses_panel _curses_panel.c -lpanel -lncurses

Expand Down
11 changes: 7 additions & 4 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "structmember.h" // PyMemberDef

#ifdef STDC_HEADERS
Expand Down Expand Up @@ -2323,10 +2324,10 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
if (oldval == NULL) {
if (PyErr_Occurred())
goto done;
if (_PyDict_SetItem_KnownHash(mapping, key, _PyLong_One, hash) < 0)
if (_PyDict_SetItem_KnownHash(mapping, key, _PyLong_GetOne(), hash) < 0)
goto done;
} else {
newval = PyNumber_Add(oldval, _PyLong_One);
newval = PyNumber_Add(oldval, _PyLong_GetOne());
if (newval == NULL)
goto done;
if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
Expand All @@ -2340,14 +2341,16 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
if (bound_get == NULL)
goto done;

PyObject *zero = _PyLong_GetZero(); // borrowed reference
PyObject *one = _PyLong_GetOne(); // borrowed reference
while (1) {
key = PyIter_Next(it);
if (key == NULL)
break;
oldval = PyObject_CallFunctionObjArgs(bound_get, key, _PyLong_Zero, NULL);
oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL);
if (oldval == NULL)
break;
newval = PyNumber_Add(oldval, _PyLong_One);
newval = PyNumber_Add(oldval, one);
Py_DECREF(oldval);
if (newval == NULL)
break;
Expand Down
7 changes: 5 additions & 2 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ bytes(cdata)
#endif
#include "ctypes.h"

#include "pycore_long.h" // _PyLong_GetZero()

PyObject *PyExc_ArgError = NULL;

/* This dict maps ctypes types to POINTER types */
Expand Down Expand Up @@ -3929,8 +3931,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_Zero;
if (defval == NULL) {
defval = _PyLong_GetZero();
}
Py_INCREF(defval);
PyTuple_SET_ITEM(callargs, i, defval);
break;
Expand Down
7 changes: 4 additions & 3 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ static const char PyCursesVersion[] = "2.2";
#define PY_SSIZE_T_CLEAN

#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()


#ifdef __hpux
Expand Down Expand Up @@ -1094,9 +1095,9 @@ _curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
_curses.window.box

[
verch: object(c_default="_PyLong_Zero") = 0
verch: object(c_default="_PyLong_GetZero()") = 0
Left and right side.
horch: object(c_default="_PyLong_Zero") = 0
horch: object(c_default="_PyLong_GetZero()") = 0
Top and bottom side.
]
/
Expand All @@ -1110,7 +1111,7 @@ horch. The default corner characters are always used by this function.
static PyObject *
_curses_window_box_impl(PyCursesWindowObject *self, int group_right_1,
PyObject *verch, PyObject *horch)
/*[clinic end generated code: output=f3fcb038bb287192 input=465a121741c1efdf]*/
/*[clinic end generated code: output=f3fcb038bb287192 input=f00435f9c8c98f60]*/
{
chtype ch1 = 0, ch2 = 0;
if (group_right_1) {
Expand Down
5 changes: 3 additions & 2 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define _PY_DATETIME_IMPL

#include "Python.h"
#include "pycore_long.h" // _PyLong_GetOne()
#include "pycore_object.h" // _PyObject_Init()
#include "datetime.h"
#include "structmember.h" // PyMemberDef
Expand Down Expand Up @@ -2448,7 +2449,7 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
goto Done

if (us) {
y = accum("microseconds", x, us, _PyLong_One, &leftover_us);
y = accum("microseconds", x, us, _PyLong_GetOne(), &leftover_us);
CLEANUP;
}
if (ms) {
Expand Down Expand Up @@ -2487,7 +2488,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, _PyLong_One); /* temp <- x & 1 */
temp = PyNumber_And(x, _PyLong_GetOne()); /* temp <- x & 1 */
if (temp == NULL) {
Py_DECREF(x);
goto Done;
Expand Down
3 changes: 2 additions & 1 deletion Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "structmember.h" // PyMemberDef
Expand Down Expand Up @@ -596,7 +597,7 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
return NULL;
}

answer = PyObject_RichCompare(res, _PyLong_Zero, op);
answer = PyObject_RichCompare(res, _PyLong_GetZero(), op);
Py_DECREF(res);
return answer;
}
Expand Down
3 changes: 2 additions & 1 deletion Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetOne()
#include "pycore_object.h"
#include <stddef.h> // offsetof()
#include "_iomodule.h"
Expand Down Expand Up @@ -556,7 +557,7 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
PyObject *b;

if (peek != NULL) {
PyObject *readahead = PyObject_CallOneArg(peek, _PyLong_One);
PyObject *readahead = PyObject_CallOneArg(peek, _PyLong_GetOne());
if (readahead == NULL) {
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
when EINTR occurs so we needn't do it ourselves. */
Expand Down
17 changes: 10 additions & 7 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_interp.h" // PyInterpreterState.fs_codec
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_object.h"
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "structmember.h" // PyMemberDef
Expand Down Expand Up @@ -971,7 +972,7 @@ _textiowrapper_fix_encoder_state(textio *self)
return -1;
}

int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_GetZero(), Py_EQ);
Py_DECREF(cookieObj);
if (cmp < 0) {
return -1;
Expand All @@ -980,7 +981,7 @@ _textiowrapper_fix_encoder_state(textio *self)
if (cmp == 0) {
self->encoding_start_of_stream = 0;
PyObject *res = PyObject_CallMethodOneArg(
self->encoder, _PyIO_str_setstate, _PyLong_Zero);
self->encoder, _PyIO_str_setstate, _PyLong_GetZero());
if (res == NULL) {
return -1;
}
Expand Down Expand Up @@ -2415,7 +2416,7 @@ _textiowrapper_encoder_reset(textio *self, int start_of_stream)
}
else {
res = PyObject_CallMethodOneArg(self->encoder, _PyIO_str_setstate,
_PyLong_Zero);
_PyLong_GetZero());
self->encoding_start_of_stream = 0;
}
if (res == NULL)
Expand Down Expand Up @@ -2459,10 +2460,12 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;
}

PyObject *zero = _PyLong_GetZero(); // borrowed reference

switch (whence) {
case SEEK_CUR:
/* seek relative to current position */
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ);
if (cmp < 0)
goto fail;

Expand All @@ -2482,7 +2485,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)

case SEEK_END:
/* seek relative to end of file */
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ);
if (cmp < 0)
goto fail;

Expand Down Expand Up @@ -2511,7 +2514,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, _PyLong_Zero, Py_EQ);
cmp = PyObject_RichCompareBool(res, zero, Py_EQ);
if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) {
Py_DECREF(res);
goto fail;
Expand All @@ -2529,7 +2532,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;
}

cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_LT);
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_LT);
if (cmp < 0)
goto fail;

Expand Down
3 changes: 2 additions & 1 deletion Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static const char copyright[] =
#define PY_SSIZE_T_CLEAN

#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "structmember.h" // PyMemberDef

#include "sre.h"
Expand Down Expand Up @@ -1999,7 +2000,7 @@ match_group(MatchObject* self, PyObject* args)

switch (size) {
case 0:
result = match_getslice(self, _PyLong_Zero, Py_None);
result = match_getslice(self, _PyLong_GetZero(), Py_None);
break;
case 1:
result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);
Expand Down
3 changes: 2 additions & 1 deletion Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetOne()
#include "structmember.h"

#include <ctype.h>
Expand Down Expand Up @@ -585,7 +586,7 @@ zoneinfo_fromutc(PyObject *obj_self, PyObject *dt)
}

dt = NULL;
if (!PyDict_SetItemString(kwargs, "fold", _PyLong_One)) {
if (!PyDict_SetItemString(kwargs, "fold", _PyLong_GetOne())) {
dt = PyObject_Call(replace, args, kwargs);
}

Expand Down
6 changes: 3 additions & 3 deletions Modules/clinic/_cursesmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include <stddef.h> // offsetof()

Expand Down Expand Up @@ -4040,13 +4041,14 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
}
} else {
cnt = 0;
long_cnt = _PyLong_Zero;
long_cnt = _PyLong_GetZero();
}
Py_INCREF(long_cnt);

/* If not specified, step defaults to 1 */
if (long_step == NULL)
long_step = _PyLong_One;
if (long_step == NULL) {
long_step = _PyLong_GetOne();
}
Py_INCREF(long_step);

assert(long_cnt != NULL && long_step != NULL);
Expand Down
11 changes: 6 additions & 5 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ raised for division by zero and mod by zero.
#include "Python.h"
#include "pycore_bitutils.h" // _Py_bit_length()
#include "pycore_dtoa.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "_math.h"

#include "clinic/mathmodule.c.h"
Expand Down Expand Up @@ -850,7 +851,7 @@ math_gcd(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
Py_DECREF(res);
return NULL;
}
if (res == _PyLong_One) {
if (res == _PyLong_GetOne()) {
/* Fast path: just check arguments.
It is okay to use identity comparison here. */
Py_DECREF(x);
Expand Down Expand Up @@ -923,7 +924,7 @@ math_lcm(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
Py_DECREF(res);
return NULL;
}
if (res == _PyLong_Zero) {
if (res == _PyLong_GetZero()) {
/* Fast path: just check arguments.
It is okay to use identity comparison here. */
Py_DECREF(x);
Expand Down Expand Up @@ -1837,7 +1838,7 @@ math_isqrt(PyObject *module, PyObject *n)
}

if (a_too_large) {
Py_SETREF(a, PyNumber_Subtract(a, _PyLong_One));
Py_SETREF(a, PyNumber_Subtract(a, _PyLong_GetOne()));
}
Py_DECREF(n);
return a;
Expand Down Expand Up @@ -3295,7 +3296,7 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
factor = n;
Py_INCREF(factor);
for (i = 1; i < factors; ++i) {
Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_GetOne()));
if (factor == NULL) {
goto error;
}
Expand Down Expand Up @@ -3417,7 +3418,7 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
factor = n;
Py_INCREF(factor);
for (i = 1; i < factors; ++i) {
Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_GetOne()));
if (factor == NULL) {
goto error;
}
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,8 @@ def detect_simple_extensions(self):
libraries=['m'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# zoneinfo module
self.add(Extension('_zoneinfo', ['_zoneinfo.c'])),
self.add(Extension('_zoneinfo', ['_zoneinfo.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
# random number generator implemented in C
self.add(Extension("_random", ["_randommodule.c"],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
Expand Down Expand Up @@ -1094,6 +1095,7 @@ def detect_readline_curses(self):
if curses_library.startswith('ncurses'):
curses_libs = [curses_library]
self.add(Extension('_curses', ['_cursesmodule.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
include_dirs=curses_includes,
define_macros=curses_defines,
libraries=curses_libs))
Expand All @@ -1108,6 +1110,7 @@ def detect_readline_curses(self):
curses_libs = ['curses']

self.add(Extension('_curses', ['_cursesmodule.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
define_macros=curses_defines,
libraries=curses_libs))
else:
Expand Down