Skip to content

Commit 3783413

Browse files
authored
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.
1 parent a6879d9 commit 3783413

File tree

14 files changed

+57
-36
lines changed

14 files changed

+57
-36
lines changed

Modules/Setup

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ _symtable symtablemodule.c
178178
#_elementtree -I$(srcdir)/Modules/expat -DHAVE_EXPAT_CONFIG_H -DUSE_PYEXPAT_CAPI _elementtree.c # elementtree accelerator
179179
#_pickle _pickle.c # pickle accelerator
180180
#_datetime _datetimemodule.c # datetime accelerator
181-
#_zoneinfo _zoneinfo.c # zoneinfo accelerator
181+
#_zoneinfo _zoneinfo.c -DPy_BUILD_CORE_MODULE # zoneinfo accelerator
182182
#_bisect _bisectmodule.c # Bisection algorithms
183183
#_heapq _heapqmodule.c -DPy_BUILD_CORE_MODULE # Heap queue algorithm
184184
#_asyncio _asynciomodule.c # Fast asyncio Future
@@ -306,7 +306,7 @@ _symtable symtablemodule.c
306306
# provided by the ncurses library. e.g. on Linux, link with -lncurses
307307
# instead of -lcurses).
308308

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

Modules/_collectionsmodule.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_long.h" // _PyLong_GetZero()
23
#include "structmember.h" // PyMemberDef
34

45
#ifdef STDC_HEADERS
@@ -2323,10 +2324,10 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
23232324
if (oldval == NULL) {
23242325
if (PyErr_Occurred())
23252326
goto done;
2326-
if (_PyDict_SetItem_KnownHash(mapping, key, _PyLong_One, hash) < 0)
2327+
if (_PyDict_SetItem_KnownHash(mapping, key, _PyLong_GetOne(), hash) < 0)
23272328
goto done;
23282329
} else {
2329-
newval = PyNumber_Add(oldval, _PyLong_One);
2330+
newval = PyNumber_Add(oldval, _PyLong_GetOne());
23302331
if (newval == NULL)
23312332
goto done;
23322333
if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
@@ -2340,14 +2341,16 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
23402341
if (bound_get == NULL)
23412342
goto done;
23422343

2344+
PyObject *zero = _PyLong_GetZero(); // borrowed reference
2345+
PyObject *one = _PyLong_GetOne(); // borrowed reference
23432346
while (1) {
23442347
key = PyIter_Next(it);
23452348
if (key == NULL)
23462349
break;
2347-
oldval = PyObject_CallFunctionObjArgs(bound_get, key, _PyLong_Zero, NULL);
2350+
oldval = PyObject_CallFunctionObjArgs(bound_get, key, zero, NULL);
23482351
if (oldval == NULL)
23492352
break;
2350-
newval = PyNumber_Add(oldval, _PyLong_One);
2353+
newval = PyNumber_Add(oldval, one);
23512354
Py_DECREF(oldval);
23522355
if (newval == NULL)
23532356
break;

Modules/_ctypes/_ctypes.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ bytes(cdata)
116116
#endif
117117
#include "ctypes.h"
118118

119+
#include "pycore_long.h" // _PyLong_GetZero()
120+
119121
PyObject *PyExc_ArgError = NULL;
120122

121123
/* This dict maps ctypes types to POINTER types */
@@ -3929,8 +3931,9 @@ _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
39293931
case PARAMFLAG_FIN | PARAMFLAG_FLCID:
39303932
/* ['in', 'lcid'] parameter. Always taken from defval,
39313933
if given, else the integer 0. */
3932-
if (defval == NULL)
3933-
defval = _PyLong_Zero;
3934+
if (defval == NULL) {
3935+
defval = _PyLong_GetZero();
3936+
}
39343937
Py_INCREF(defval);
39353938
PyTuple_SET_ITEM(callargs, i, defval);
39363939
break;

Modules/_cursesmodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static const char PyCursesVersion[] = "2.2";
103103
#define PY_SSIZE_T_CLEAN
104104

105105
#include "Python.h"
106+
#include "pycore_long.h" // _PyLong_GetZero()
106107

107108

108109
#ifdef __hpux
@@ -1094,9 +1095,9 @@ _curses_window_border_impl(PyCursesWindowObject *self, PyObject *ls,
10941095
_curses.window.box
10951096
10961097
[
1097-
verch: object(c_default="_PyLong_Zero") = 0
1098+
verch: object(c_default="_PyLong_GetZero()") = 0
10981099
Left and right side.
1099-
horch: object(c_default="_PyLong_Zero") = 0
1100+
horch: object(c_default="_PyLong_GetZero()") = 0
11001101
Top and bottom side.
11011102
]
11021103
/
@@ -1110,7 +1111,7 @@ horch. The default corner characters are always used by this function.
11101111
static PyObject *
11111112
_curses_window_box_impl(PyCursesWindowObject *self, int group_right_1,
11121113
PyObject *verch, PyObject *horch)
1113-
/*[clinic end generated code: output=f3fcb038bb287192 input=465a121741c1efdf]*/
1114+
/*[clinic end generated code: output=f3fcb038bb287192 input=f00435f9c8c98f60]*/
11141115
{
11151116
chtype ch1 = 0, ch2 = 0;
11161117
if (group_right_1) {

Modules/_datetimemodule.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _PY_DATETIME_IMPL
99

1010
#include "Python.h"
11+
#include "pycore_long.h" // _PyLong_GetOne()
1112
#include "pycore_object.h" // _PyObject_Init()
1213
#include "datetime.h"
1314
#include "structmember.h" // PyMemberDef
@@ -2448,7 +2449,7 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
24482449
goto Done
24492450

24502451
if (us) {
2451-
y = accum("microseconds", x, us, _PyLong_One, &leftover_us);
2452+
y = accum("microseconds", x, us, _PyLong_GetOne(), &leftover_us);
24522453
CLEANUP;
24532454
}
24542455
if (ms) {
@@ -2487,7 +2488,7 @@ delta_new(PyTypeObject *type, PyObject *args, PyObject *kw)
24872488
* is odd. Note that x is odd when it's last bit is 1. The
24882489
* code below uses bitwise and operation to check the last
24892490
* bit. */
2490-
temp = PyNumber_And(x, _PyLong_One); /* temp <- x & 1 */
2491+
temp = PyNumber_And(x, _PyLong_GetOne()); /* temp <- x & 1 */
24912492
if (temp == NULL) {
24922493
Py_DECREF(x);
24932494
goto Done;

Modules/_functoolsmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_long.h" // _PyLong_GetZero()
23
#include "pycore_pystate.h" // _PyThreadState_GET()
34
#include "pycore_tuple.h" // _PyTuple_ITEMS()
45
#include "structmember.h" // PyMemberDef
@@ -596,7 +597,7 @@ keyobject_richcompare(PyObject *ko, PyObject *other, int op)
596597
return NULL;
597598
}
598599

599-
answer = PyObject_RichCompare(res, _PyLong_Zero, op);
600+
answer = PyObject_RichCompare(res, _PyLong_GetZero(), op);
600601
Py_DECREF(res);
601602
return answer;
602603
}

Modules/_io/iobase.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#define PY_SSIZE_T_CLEAN
1212
#include "Python.h"
13+
#include "pycore_long.h" // _PyLong_GetOne()
1314
#include "pycore_object.h"
1415
#include <stddef.h> // offsetof()
1516
#include "_iomodule.h"
@@ -556,7 +557,7 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
556557
PyObject *b;
557558

558559
if (peek != NULL) {
559-
PyObject *readahead = PyObject_CallOneArg(peek, _PyLong_One);
560+
PyObject *readahead = PyObject_CallOneArg(peek, _PyLong_GetOne());
560561
if (readahead == NULL) {
561562
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals()
562563
when EINTR occurs so we needn't do it ourselves. */

Modules/_io/textio.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define PY_SSIZE_T_CLEAN
1010
#include "Python.h"
1111
#include "pycore_interp.h" // PyInterpreterState.fs_codec
12+
#include "pycore_long.h" // _PyLong_GetZero()
1213
#include "pycore_object.h"
1314
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1415
#include "structmember.h" // PyMemberDef
@@ -971,7 +972,7 @@ _textiowrapper_fix_encoder_state(textio *self)
971972
return -1;
972973
}
973974

974-
int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
975+
int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_GetZero(), Py_EQ);
975976
Py_DECREF(cookieObj);
976977
if (cmp < 0) {
977978
return -1;
@@ -980,7 +981,7 @@ _textiowrapper_fix_encoder_state(textio *self)
980981
if (cmp == 0) {
981982
self->encoding_start_of_stream = 0;
982983
PyObject *res = PyObject_CallMethodOneArg(
983-
self->encoder, _PyIO_str_setstate, _PyLong_Zero);
984+
self->encoder, _PyIO_str_setstate, _PyLong_GetZero());
984985
if (res == NULL) {
985986
return -1;
986987
}
@@ -2415,7 +2416,7 @@ _textiowrapper_encoder_reset(textio *self, int start_of_stream)
24152416
}
24162417
else {
24172418
res = PyObject_CallMethodOneArg(self->encoder, _PyIO_str_setstate,
2418-
_PyLong_Zero);
2419+
_PyLong_GetZero());
24192420
self->encoding_start_of_stream = 0;
24202421
}
24212422
if (res == NULL)
@@ -2459,10 +2460,12 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
24592460
goto fail;
24602461
}
24612462

2463+
PyObject *zero = _PyLong_GetZero(); // borrowed reference
2464+
24622465
switch (whence) {
24632466
case SEEK_CUR:
24642467
/* seek relative to current position */
2465-
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
2468+
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ);
24662469
if (cmp < 0)
24672470
goto fail;
24682471

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

24832486
case SEEK_END:
24842487
/* seek relative to end of file */
2485-
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
2488+
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ);
24862489
if (cmp < 0)
24872490
goto fail;
24882491

@@ -2511,7 +2514,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
25112514
goto fail;
25122515
if (self->encoder) {
25132516
/* If seek() == 0, we are at the start of stream, otherwise not */
2514-
cmp = PyObject_RichCompareBool(res, _PyLong_Zero, Py_EQ);
2517+
cmp = PyObject_RichCompareBool(res, zero, Py_EQ);
25152518
if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) {
25162519
Py_DECREF(res);
25172520
goto fail;
@@ -2529,7 +2532,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
25292532
goto fail;
25302533
}
25312534

2532-
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_LT);
2535+
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_LT);
25332536
if (cmp < 0)
25342537
goto fail;
25352538

Modules/_sre.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static const char copyright[] =
4141
#define PY_SSIZE_T_CLEAN
4242

4343
#include "Python.h"
44+
#include "pycore_long.h" // _PyLong_GetZero()
4445
#include "structmember.h" // PyMemberDef
4546

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

20002001
switch (size) {
20012002
case 0:
2002-
result = match_getslice(self, _PyLong_Zero, Py_None);
2003+
result = match_getslice(self, _PyLong_GetZero(), Py_None);
20032004
break;
20042005
case 1:
20052006
result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);

Modules/_zoneinfo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_long.h" // _PyLong_GetOne()
23
#include "structmember.h"
34

45
#include <ctype.h>
@@ -585,7 +586,7 @@ zoneinfo_fromutc(PyObject *obj_self, PyObject *dt)
585586
}
586587

587588
dt = NULL;
588-
if (!PyDict_SetItemString(kwargs, "fold", _PyLong_One)) {
589+
if (!PyDict_SetItemString(kwargs, "fold", _PyLong_GetOne())) {
589590
dt = PyObject_Call(replace, args, kwargs);
590591
}
591592

Modules/clinic/_cursesmodule.c.h

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

Modules/itertoolsmodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#define PY_SSIZE_T_CLEAN
33
#include "Python.h"
4+
#include "pycore_long.h" // _PyLong_GetZero()
45
#include "pycore_tuple.h" // _PyTuple_ITEMS()
56
#include <stddef.h> // offsetof()
67

@@ -4040,13 +4041,14 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
40404041
}
40414042
} else {
40424043
cnt = 0;
4043-
long_cnt = _PyLong_Zero;
4044+
long_cnt = _PyLong_GetZero();
40444045
}
40454046
Py_INCREF(long_cnt);
40464047

40474048
/* If not specified, step defaults to 1 */
4048-
if (long_step == NULL)
4049-
long_step = _PyLong_One;
4049+
if (long_step == NULL) {
4050+
long_step = _PyLong_GetOne();
4051+
}
40504052
Py_INCREF(long_step);
40514053

40524054
assert(long_cnt != NULL && long_step != NULL);

Modules/mathmodule.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ raised for division by zero and mod by zero.
5555
#include "Python.h"
5656
#include "pycore_bitutils.h" // _Py_bit_length()
5757
#include "pycore_dtoa.h"
58+
#include "pycore_long.h" // _PyLong_GetZero()
5859
#include "_math.h"
5960

6061
#include "clinic/mathmodule.c.h"
@@ -850,7 +851,7 @@ math_gcd(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
850851
Py_DECREF(res);
851852
return NULL;
852853
}
853-
if (res == _PyLong_One) {
854+
if (res == _PyLong_GetOne()) {
854855
/* Fast path: just check arguments.
855856
It is okay to use identity comparison here. */
856857
Py_DECREF(x);
@@ -923,7 +924,7 @@ math_lcm(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
923924
Py_DECREF(res);
924925
return NULL;
925926
}
926-
if (res == _PyLong_Zero) {
927+
if (res == _PyLong_GetZero()) {
927928
/* Fast path: just check arguments.
928929
It is okay to use identity comparison here. */
929930
Py_DECREF(x);
@@ -1837,7 +1838,7 @@ math_isqrt(PyObject *module, PyObject *n)
18371838
}
18381839

18391840
if (a_too_large) {
1840-
Py_SETREF(a, PyNumber_Subtract(a, _PyLong_One));
1841+
Py_SETREF(a, PyNumber_Subtract(a, _PyLong_GetOne()));
18411842
}
18421843
Py_DECREF(n);
18431844
return a;
@@ -3295,7 +3296,7 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
32953296
factor = n;
32963297
Py_INCREF(factor);
32973298
for (i = 1; i < factors; ++i) {
3298-
Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
3299+
Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_GetOne()));
32993300
if (factor == NULL) {
33003301
goto error;
33013302
}
@@ -3417,7 +3418,7 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
34173418
factor = n;
34183419
Py_INCREF(factor);
34193420
for (i = 1; i < factors; ++i) {
3420-
Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_One));
3421+
Py_SETREF(factor, PyNumber_Subtract(factor, _PyLong_GetOne()));
34213422
if (factor == NULL) {
34223423
goto error;
34233424
}

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,8 @@ def detect_simple_extensions(self):
856856
libraries=['m'],
857857
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
858858
# zoneinfo module
859-
self.add(Extension('_zoneinfo', ['_zoneinfo.c'])),
859+
self.add(Extension('_zoneinfo', ['_zoneinfo.c'],
860+
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
860861
# random number generator implemented in C
861862
self.add(Extension("_random", ["_randommodule.c"],
862863
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))
@@ -1094,6 +1095,7 @@ def detect_readline_curses(self):
10941095
if curses_library.startswith('ncurses'):
10951096
curses_libs = [curses_library]
10961097
self.add(Extension('_curses', ['_cursesmodule.c'],
1098+
extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
10971099
include_dirs=curses_includes,
10981100
define_macros=curses_defines,
10991101
libraries=curses_libs))
@@ -1108,6 +1110,7 @@ def detect_readline_curses(self):
11081110
curses_libs = ['curses']
11091111

11101112
self.add(Extension('_curses', ['_cursesmodule.c'],
1113+
extra_compile_args=['-DPy_BUILD_CORE_MODULE'],
11111114
define_macros=curses_defines,
11121115
libraries=curses_libs))
11131116
else:

0 commit comments

Comments
 (0)