bpo-42161: Modules/ uses _PyLong_GetZero() and _PyLong_GetOne() (GH-22998)

Use _PyLong_GetZero() and _PyLong_GetOne() in Modules/ directory.

_cursesmodule.c and zoneinfo.c are now built with
Py_BUILD_CORE_MODULE macro defined.
This commit is contained in:
Victor Stinner 2020-10-27 17:12:53 +01:00 committed by GitHub
parent a6879d9445
commit 37834136d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 57 additions and 36 deletions

View File

@ -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
@ -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

View File

@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "structmember.h" // PyMemberDef
#ifdef STDC_HEADERS
@ -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)
@ -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;

View File

@ -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 */
@ -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;

View File

@ -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
@ -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.
]
/
@ -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) {

View File

@ -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
@ -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) {
@ -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;

View File

@ -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
@ -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;
}

View File

@ -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"
@ -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. */

View File

@ -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
@ -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;
@ -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;
}
@ -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)
@ -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;
@ -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;
@ -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;
@ -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;

View File

@ -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"
@ -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);

View File

@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetOne()
#include "structmember.h"
#include <ctype.h>
@ -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);
}

View File

@ -509,8 +509,8 @@ _curses_window_box(PyCursesWindowObject *self, PyObject *args)
{
PyObject *return_value = NULL;
int group_right_1 = 0;
PyObject *verch = _PyLong_Zero;
PyObject *horch = _PyLong_Zero;
PyObject *verch = _PyLong_GetZero();
PyObject *horch = _PyLong_GetZero();
switch (PyTuple_GET_SIZE(args)) {
case 0:
@ -4288,4 +4288,4 @@ _curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored
#ifndef _CURSES_USE_DEFAULT_COLORS_METHODDEF
#define _CURSES_USE_DEFAULT_COLORS_METHODDEF
#endif /* !defined(_CURSES_USE_DEFAULT_COLORS_METHODDEF) */
/*[clinic end generated code: output=38b2531d17f119e1 input=a9049054013a1b77]*/
/*[clinic end generated code: output=92bad2172fef9747 input=a9049054013a1b77]*/

View File

@ -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()
@ -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);

View File

@ -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"
@ -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);
@ -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);
@ -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;
@ -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;
}
@ -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;
}

View File

@ -856,7 +856,8 @@ class PyBuildExt(build_ext):
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']))
@ -1094,6 +1095,7 @@ class PyBuildExt(build_ext):
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))
@ -1108,6 +1110,7 @@ class PyBuildExt(build_ext):
curses_libs = ['curses']
self.add(Extension('_curses', ['_cursesmodule.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
define_macros=curses_defines,
libraries=curses_libs))
else: