Skip to content

Commit 48c8bf2

Browse files
[2.7] bpo-34234: Use _PyAnyInt_Check() and _PyAnyInt_CheckExact(). (GH-8479)
1 parent dc9039d commit 48c8bf2

36 files changed

+75
-81
lines changed

Include/intobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ PyAPI_DATA(PyTypeObject) PyInt_Type;
3131
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_INT_SUBCLASS)
3232
#define PyInt_CheckExact(op) (Py_TYPE(op) == &PyInt_Type)
3333

34+
#define _PyAnyInt_Check(op) (PyInt_Check(op) || PyLong_Check(op))
35+
#define _PyAnyInt_CheckExact(op) (PyInt_CheckExact(op) || PyLong_CheckExact(op))
36+
3437
PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
3538
#ifdef Py_USING_UNICODE
3639
PyAPI_FUNC(PyObject *) PyInt_FromUnicode(Py_UNICODE*, Py_ssize_t, int);

Modules/_csv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ _set_int(const char *name, int *target, PyObject *src, int dflt)
224224
if (src == NULL)
225225
*target = dflt;
226226
else {
227-
if (!PyInt_Check(src) && !PyLong_Check(src)) {
227+
if (!_PyAnyInt_Check(src)) {
228228
PyErr_Format(PyExc_TypeError,
229229
"\"%s\" must be an integer", name);
230230
return -1;
@@ -1452,7 +1452,7 @@ csv_field_size_limit(PyObject *module, PyObject *args)
14521452
if (!PyArg_UnpackTuple(args, "field_size_limit", 0, 1, &new_limit))
14531453
return NULL;
14541454
if (new_limit != NULL) {
1455-
if (!PyInt_Check(new_limit) && !PyLong_Check(new_limit)) {
1455+
if (!_PyAnyInt_Check(new_limit)) {
14561456
PyErr_Format(PyExc_TypeError,
14571457
"limit must be an integer");
14581458
return NULL;

Modules/_ctypes/_ctypes.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static PyObject *
547547
CDataType_from_address(PyObject *type, PyObject *value)
548548
{
549549
void *buf;
550-
if (!PyInt_Check(value) && !PyLong_Check(value)) {
550+
if (!_PyAnyInt_Check(value)) {
551551
PyErr_SetString(PyExc_TypeError,
552552
"integer expected");
553553
return NULL;
@@ -691,7 +691,7 @@ CDataType_in_dll(PyObject *type, PyObject *args)
691691
obj = PyObject_GetAttrString(dll, "_handle");
692692
if (!obj)
693693
return NULL;
694-
if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
694+
if (!_PyAnyInt_Check(obj)) {
695695
PyErr_SetString(PyExc_TypeError,
696696
"the _handle attribute of the second argument must be an integer");
697697
Py_DECREF(obj);
@@ -1779,7 +1779,7 @@ c_void_p_from_param(PyObject *type, PyObject *value)
17791779
}
17801780
/* Should probably allow buffer interface as well */
17811781
/* int, long */
1782-
if (PyInt_Check(value) || PyLong_Check(value)) {
1782+
if (_PyAnyInt_Check(value)) {
17831783
PyCArgObject *parg;
17841784
struct fielddesc *fd = _ctypes_get_fielddesc("P");
17851785

@@ -3419,7 +3419,7 @@ static int
34193419
_get_name(PyObject *obj, char **pname)
34203420
{
34213421
#ifdef MS_WIN32
3422-
if (PyInt_Check(obj) || PyLong_Check(obj)) {
3422+
if (_PyAnyInt_Check(obj)) {
34233423
/* We have to use MAKEINTRESOURCEA for Windows CE.
34243424
Works on Windows as well, of course.
34253425
*/
@@ -3469,7 +3469,7 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
34693469
Py_DECREF(ftuple);
34703470
return NULL;
34713471
}
3472-
if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
3472+
if (!_PyAnyInt_Check(obj)) {
34733473
PyErr_SetString(PyExc_TypeError,
34743474
"the _handle attribute of the second argument must be an integer");
34753475
Py_DECREF(ftuple);
@@ -3600,8 +3600,7 @@ PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
36003600
#endif
36013601

36023602
if (1 == PyTuple_GET_SIZE(args)
3603-
&& (PyInt_Check(PyTuple_GET_ITEM(args, 0))
3604-
|| PyLong_Check(PyTuple_GET_ITEM(args, 0)))) {
3603+
&& _PyAnyInt_Check(PyTuple_GET_ITEM(args, 0))) {
36053604
CDataObject *ob;
36063605
void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0));
36073606
if (ptr == NULL && PyErr_Occurred())

Modules/_ctypes/cfield.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ z_set(void *ptr, PyObject *value, Py_ssize_t size)
13611361
return NULL;
13621362
*(char **)ptr = PyString_AS_STRING(str);
13631363
return str;
1364-
} else if (PyInt_Check(value) || PyLong_Check(value)) {
1364+
} else if (_PyAnyInt_Check(value)) {
13651365
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
13661366
*(char **)ptr = (char *)PyInt_AsUnsignedLongLongMask(value);
13671367
#else
@@ -1410,7 +1410,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
14101410
_ctypes_conversion_errors);
14111411
if (!value)
14121412
return NULL;
1413-
} else if (PyInt_Check(value) || PyLong_Check(value)) {
1413+
} else if (_PyAnyInt_Check(value)) {
14141414
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
14151415
*(wchar_t **)ptr = (wchar_t *)PyInt_AsUnsignedLongLongMask(value);
14161416
#else
@@ -1565,7 +1565,7 @@ P_set(void *ptr, PyObject *value, Py_ssize_t size)
15651565
_RET(value);
15661566
}
15671567

1568-
if (!PyInt_Check(value) && !PyLong_Check(value)) {
1568+
if (!_PyAnyInt_Check(value)) {
15691569
PyErr_SetString(PyExc_TypeError,
15701570
"cannot be converted to pointer");
15711571
return NULL;

Modules/_cursesmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ PyCursesCheckERR(int code, char *fname)
194194
static int
195195
PyCurses_ConvertToChtype(PyObject *obj, chtype *ch)
196196
{
197-
if (PyInt_Check(obj) || PyLong_Check(obj)) {
197+
if (_PyAnyInt_Check(obj)) {
198198
*ch = (chtype) PyInt_AsLong(obj);
199199
if (*ch == (chtype) -1 && PyErr_Occurred())
200200
return 0;
@@ -2603,7 +2603,7 @@ PyCurses_UnCtrl(PyObject *self, PyObject *args)
26032603

26042604
if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
26052605

2606-
if (PyInt_Check(temp) || PyLong_Check(temp)) {
2606+
if (_PyAnyInt_Check(temp)) {
26072607
ch = (chtype) PyInt_AsLong(temp);
26082608
if (ch == (chtype) -1 && PyErr_Occurred())
26092609
return NULL;
@@ -2628,7 +2628,7 @@ PyCurses_UngetCh(PyObject *self, PyObject *args)
26282628

26292629
if (!PyArg_ParseTuple(args,"O;ch or int",&temp)) return NULL;
26302630

2631-
if (PyInt_Check(temp) || PyLong_Check(temp)) {
2631+
if (_PyAnyInt_Check(temp)) {
26322632
ch = (int) PyInt_AsLong(temp);
26332633
if (ch == -1 && PyErr_Occurred())
26342634
return NULL;

Modules/_elementtree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ element_subscr(PyObject* self_, PyObject* item)
13471347
ElementObject* self = (ElementObject*) self_;
13481348

13491349
#if (PY_VERSION_HEX < 0x02050000)
1350-
if (PyInt_Check(item) || PyLong_Check(item)) {
1350+
if (_PyAnyInt_Check(item)) {
13511351
long i = PyInt_AsLong(item);
13521352
#else
13531353
if (PyIndex_Check(item)) {
@@ -1404,7 +1404,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
14041404
ElementObject* self = (ElementObject*) self_;
14051405

14061406
#if (PY_VERSION_HEX < 0x02050000)
1407-
if (PyInt_Check(item) || PyLong_Check(item)) {
1407+
if (_PyAnyInt_Check(item)) {
14081408
long i = PyInt_AsLong(item);
14091409
#else
14101410
if (PyIndex_Check(item)) {

Modules/_json.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,7 @@ encoder_listencode_obj(PyEncoderObject *s, PyObject *rval, PyObject *obj, Py_ssi
19811981
return -1;
19821982
return _steal_list_append(rval, encoded);
19831983
}
1984-
else if (PyInt_Check(obj) || PyLong_Check(obj)) {
1984+
else if (_PyAnyInt_Check(obj)) {
19851985
PyObject *encoded = PyObject_Str(obj);
19861986
if (encoded == NULL)
19871987
return -1;
@@ -2131,7 +2131,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss
21312131
if (kstr == NULL)
21322132
goto bail;
21332133
}
2134-
else if (PyInt_Check(key) || PyLong_Check(key)) {
2134+
else if (_PyAnyInt_Check(key)) {
21352135
kstr = PyObject_Str(key);
21362136
if (kstr == NULL)
21372137
goto bail;

Modules/_randommodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ random_jumpahead(RandomObject *self, PyObject *n)
415415
PyObject *remobj;
416416
unsigned long *mt, tmp, nonzero;
417417

418-
if (!PyInt_Check(n) && !PyLong_Check(n)) {
418+
if (!_PyAnyInt_Check(n)) {
419419
PyErr_Format(PyExc_TypeError, "jumpahead requires an "
420420
"integer, not '%s'",
421421
Py_TYPE(n)->tp_name);

Modules/_sqlite/statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static int _need_adapt(PyObject* obj)
205205
return 1;
206206
}
207207

208-
if (PyInt_CheckExact(obj) || PyLong_CheckExact(obj)
208+
if (_PyAnyInt_CheckExact(obj)
209209
|| PyFloat_CheckExact(obj) || PyString_CheckExact(obj)
210210
|| PyUnicode_CheckExact(obj) || PyBuffer_Check(obj)) {
211211
return 0;

Modules/_sre.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,15 +3327,15 @@ match_getindex(MatchObject* self, PyObject* index)
33273327
{
33283328
Py_ssize_t i;
33293329

3330-
if (PyInt_Check(index) || PyLong_Check(index))
3330+
if (_PyAnyInt_Check(index))
33313331
return PyInt_AsSsize_t(index);
33323332

33333333
i = -1;
33343334

33353335
if (self->pattern->groupindex) {
33363336
index = PyObject_GetItem(self->pattern->groupindex, index);
33373337
if (index) {
3338-
if (PyInt_Check(index) || PyLong_Check(index))
3338+
if (_PyAnyInt_Check(index))
33393339
i = PyInt_AsSsize_t(index);
33403340
Py_DECREF(index);
33413341
} else

Modules/_struct.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ get_pylong(PyObject *v)
110110
PyObject *r, *w;
111111
int converted = 0;
112112
assert(v != NULL);
113-
if (!PyInt_Check(v) && !PyLong_Check(v)) {
113+
if (!_PyAnyInt_Check(v)) {
114114
PyNumberMethods *m;
115115
/* Not an integer; first try to use __index__ to
116116
convert to an integer. If the __index__ method
@@ -150,7 +150,7 @@ get_pylong(PyObject *v)
150150
v = m->nb_int(v);
151151
if (v == NULL)
152152
return NULL;
153-
if (!PyInt_Check(v) && !PyLong_Check(v)) {
153+
if (!_PyAnyInt_Check(v)) {
154154
PyErr_SetString(PyExc_TypeError,
155155
"__int__ method returned "
156156
"non-integer");
@@ -169,7 +169,7 @@ get_pylong(PyObject *v)
169169
/* Ensure we own a reference to v. */
170170
Py_INCREF(v);
171171

172-
assert(PyInt_Check(v) || PyLong_Check(v));
172+
assert(_PyAnyInt_Check(v));
173173
if (PyInt_Check(v)) {
174174
r = PyLong_FromLong(PyInt_AS_LONG(v));
175175
Py_DECREF(v);

Modules/_tkinter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2107,7 +2107,7 @@ Tkapp_GetInt(PyObject *self, PyObject *args)
21072107

21082108
if (PyTuple_Size(args) == 1) {
21092109
PyObject* o = PyTuple_GetItem(args, 0);
2110-
if (PyInt_Check(o) || PyLong_Check(o)) {
2110+
if (_PyAnyInt_Check(o)) {
21112111
Py_INCREF(o);
21122112
return o;
21132113
}

Modules/cjkcodecs/multibytecodec.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ multibytecodec_encerror(MultibyteCodec *codec,
310310

311311
if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
312312
!PyUnicode_Check((tobj = PyTuple_GET_ITEM(retobj, 0))) ||
313-
!(PyInt_Check(PyTuple_GET_ITEM(retobj, 1)) ||
314-
PyLong_Check(PyTuple_GET_ITEM(retobj, 1)))) {
313+
!_PyAnyInt_Check(PyTuple_GET_ITEM(retobj, 1))) {
315314
PyErr_SetString(PyExc_TypeError,
316315
"encoding error handler must return "
317316
"(unicode, int) tuple");
@@ -430,8 +429,7 @@ multibytecodec_decerror(MultibyteCodec *codec,
430429

431430
if (!PyTuple_Check(retobj) || PyTuple_GET_SIZE(retobj) != 2 ||
432431
!PyUnicode_Check((retuni = PyTuple_GET_ITEM(retobj, 0))) ||
433-
!(PyInt_Check(PyTuple_GET_ITEM(retobj, 1)) ||
434-
PyLong_Check(PyTuple_GET_ITEM(retobj, 1)))) {
432+
!_PyAnyInt_Check(PyTuple_GET_ITEM(retobj, 1))) {
435433
PyErr_SetString(PyExc_TypeError,
436434
"decoding error handler must return "
437435
"(unicode, int) tuple");

Modules/datetimemodule.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
15371537
if (x2 == NULL)
15381538
goto Done;
15391539
result = PyNumber_Add(x1, x2);
1540-
assert(result == NULL || PyInt_CheckExact(result) || PyLong_CheckExact(result));
1540+
assert(result == NULL || _PyAnyInt_CheckExact(result));
15411541

15421542
Done:
15431543
Py_XDECREF(x1);
@@ -1560,7 +1560,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
15601560
PyObject *num = NULL;
15611561
PyObject *result = NULL;
15621562

1563-
assert(PyInt_CheckExact(pyus) || PyLong_CheckExact(pyus));
1563+
assert(_PyAnyInt_CheckExact(pyus));
15641564
tuple = PyNumber_Divmod(pyus, us_per_second);
15651565
if (tuple == NULL)
15661566
goto Done;
@@ -1803,11 +1803,11 @@ delta_multiply(PyObject *left, PyObject *right)
18031803

18041804
if (PyDelta_Check(left)) {
18051805
/* delta * ??? */
1806-
if (PyInt_Check(right) || PyLong_Check(right))
1806+
if (_PyAnyInt_Check(right))
18071807
result = multiply_int_timedelta(right,
18081808
(PyDateTime_Delta *) left);
18091809
}
1810-
else if (PyInt_Check(left) || PyLong_Check(left))
1810+
else if (_PyAnyInt_Check(left))
18111811
result = multiply_int_timedelta(left,
18121812
(PyDateTime_Delta *) right);
18131813

@@ -1823,7 +1823,7 @@ delta_divide(PyObject *left, PyObject *right)
18231823

18241824
if (PyDelta_Check(left)) {
18251825
/* delta * ??? */
1826-
if (PyInt_Check(right) || PyLong_Check(right))
1826+
if (_PyAnyInt_Check(right))
18271827
result = divide_timedelta_int(
18281828
(PyDateTime_Delta *)left,
18291829
right);
@@ -1852,14 +1852,14 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
18521852

18531853
assert(num != NULL);
18541854

1855-
if (PyInt_Check(num) || PyLong_Check(num)) {
1855+
if (_PyAnyInt_Check(num)) {
18561856
prod = PyNumber_Multiply(factor, num);
18571857
if (prod == NULL)
18581858
return NULL;
1859-
assert(PyInt_CheckExact(prod) || PyLong_CheckExact(prod));
1859+
assert(_PyAnyInt_CheckExact(prod));
18601860
sum = PyNumber_Add(sofar, prod);
18611861
Py_DECREF(prod);
1862-
assert(sum == NULL || PyInt_CheckExact(sum) || PyLong_CheckExact(sum));
1862+
assert(sum == NULL || _PyAnyInt_CheckExact(sum));
18631863
return sum;
18641864
}
18651865

@@ -1902,7 +1902,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
19021902
* fractional part requires float arithmetic, and may
19031903
* lose a little info.
19041904
*/
1905-
assert(PyInt_CheckExact(factor) || PyLong_CheckExact(factor));
1905+
assert(_PyAnyInt_CheckExact(factor));
19061906
if (PyInt_Check(factor))
19071907
dnum = (double)PyInt_AsLong(factor);
19081908
else
@@ -1920,7 +1920,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
19201920
Py_DECREF(sum);
19211921
Py_DECREF(x);
19221922
*leftover += fracpart;
1923-
assert(y == NULL || PyInt_CheckExact(y) || PyLong_CheckExact(y));
1923+
assert(y == NULL || _PyAnyInt_CheckExact(y));
19241924
return y;
19251925
}
19261926

Modules/dlmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ dl_call(dlobject *xp, PyObject *args)
107107
}
108108
for (i = 1; i < n; i++) {
109109
PyObject *v = PyTuple_GetItem(args, i);
110-
if (PyInt_Check(v) || PyLong_Check(v)) {
110+
if (_PyAnyInt_Check(v)) {
111111
alist[i-1] = PyInt_AsLong(v);
112112
if (alist[i-1] == -1 && PyErr_Occurred())
113113
return NULL;

Modules/mathmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ math_ldexp(PyObject *self, PyObject *args)
11881188
if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp))
11891189
return NULL;
11901190

1191-
if (PyLong_Check(oexp) || PyInt_Check(oexp)) {
1191+
if (_PyAnyInt_Check(oexp)) {
11921192
/* on overflow, replace exponent with either LONG_MAX
11931193
or LONG_MIN, depending on the sign. */
11941194
exp = PyLong_AsLongAndOverflow(oexp, &overflow);

Modules/posixmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6121,7 +6121,7 @@ posix_setgroups(PyObject *self, PyObject *groups)
61216121
elem = PySequence_GetItem(groups, i);
61226122
if (!elem)
61236123
return NULL;
6124-
if (!PyInt_Check(elem) && !PyLong_Check(elem)) {
6124+
if (!_PyAnyInt_Check(elem)) {
61256125
PyErr_SetString(PyExc_TypeError,
61266126
"groups must be integers");
61276127
Py_DECREF(elem);

Modules/socketmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4194,7 +4194,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args)
41944194
"getaddrinfo() argument 1 must be string or None");
41954195
return NULL;
41964196
}
4197-
if (PyInt_Check(pobj) || PyLong_Check(pobj)) {
4197+
if (_PyAnyInt_Check(pobj)) {
41984198
long value = PyLong_AsLong(pobj);
41994199
if (value == -1 && PyErr_Occurred())
42004200
return NULL;

Modules/svmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ sv_LoadMap(svobject *self, PyObject *args)
686686
if (!cell)
687687
goto finally;
688688

689-
if (!PyInt_Check(cell) && !PyLong_Check(cell)) {
689+
if (!_PyAnyInt_Check(cell)) {
690690
PyErr_BadArgument();
691691
goto finally;
692692
}
@@ -757,7 +757,7 @@ doParams(svobject *self, PyObject *args,
757757
if (!v)
758758
goto finally;
759759

760-
if (!PyInt_Check(v) && !PyLong_Check(v)) {
760+
if (!_PyAnyInt_Check(v)) {
761761
PyErr_BadArgument();
762762
goto finally;
763763
}

0 commit comments

Comments
 (0)