Skip to content

Commit 1fd1eb5

Browse files
vstinnermiss-islington
authored andcommitted
gh-126835: Fix _PY_IS_SMALL_INT() macro (GH-146631)
(cherry picked from commit adf2c47) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 2d1515d commit 1fd1eb5

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

Include/internal/pycore_long.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ PyAPI_FUNC(void) _PyLong_ExactDealloc(PyObject *self);
6464
# error "_PY_NSMALLPOSINTS must be greater than or equal to 257"
6565
#endif
6666

67-
#define _PY_IS_SMALL_INT(val) ((val) >= 0 && (val) < 256 && (val) < _PY_NSMALLPOSINTS)
67+
#define _PY_IS_SMALL_INT(val) \
68+
(-_PY_NSMALLNEGINTS <= (val) && (val) < _PY_NSMALLPOSINTS)
6869

6970
// Return a reference to the immortal zero singleton.
7071
// The function cannot return NULL.

Objects/longobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class int "PyObject *" "&PyLong_Type"
2424

2525
#define medium_value(x) ((stwodigits)_PyLong_CompactValue(x))
2626

27-
#define IS_SMALL_INT(ival) (-_PY_NSMALLNEGINTS <= (ival) && (ival) < _PY_NSMALLPOSINTS)
27+
#define IS_SMALL_INT(ival) _PY_IS_SMALL_INT(ival)
2828
#define IS_SMALL_UINT(ival) ((ival) < _PY_NSMALLPOSINTS)
2929

3030
#define _MAX_STR_DIGITS_ERROR_FMT_TO_INT "Exceeds the limit (%d digits) for integer string conversion: value has %zd digits; use sys.set_int_max_str_digits() to increase the limit"

Python/flowgraph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ maybe_instr_make_load_smallint(cfg_instr *instr, PyObject *newconst,
13971397
if (val == -1 && PyErr_Occurred()) {
13981398
return -1;
13991399
}
1400-
if (!overflow && _PY_IS_SMALL_INT(val)) {
1400+
if (!overflow && _PY_IS_SMALL_INT(val) && 0 <= val && val <= 255) {
14011401
assert(_Py_IsImmortal(newconst));
14021402
INSTR_SET_OP1(instr, LOAD_SMALL_INT, (int)val);
14031403
return 1;

0 commit comments

Comments
 (0)