Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dde99d2

Browse files
committed
Remove size constraints in SLICE opcodes.
1 parent 5c97c79 commit dde99d2

2 files changed

Lines changed: 14 additions & 19 deletions

File tree

Objects/classobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
11701170
return NULL;
11711171
arg = Py_BuildValue("(N)", sliceobj_from_intint(i, j));
11721172
} else
1173-
arg = Py_BuildValue("(ii)", i, j);
1173+
arg = Py_BuildValue("(nn)", i, j);
11741174

11751175
if (arg == NULL) {
11761176
Py_DECREF(func);
@@ -1241,7 +1241,7 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject
12411241
arg = Py_BuildValue("(N)",
12421242
sliceobj_from_intint(i, j));
12431243
} else
1244-
arg = Py_BuildValue("(ii)", i, j);
1244+
arg = Py_BuildValue("(nn)", i, j);
12451245
}
12461246
else {
12471247
if (setslicestr == NULL)
@@ -1262,7 +1262,7 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject
12621262
arg = Py_BuildValue("(NO)",
12631263
sliceobj_from_intint(i, j), value);
12641264
} else
1265-
arg = Py_BuildValue("(iiO)", i, j, value);
1265+
arg = Py_BuildValue("(nnO)", i, j, value);
12661266
}
12671267
if (arg == NULL) {
12681268
Py_DECREF(func);

Python/ceval.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,8 +3851,9 @@ ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
38513851
}
38523852

38533853
/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3854-
Silently reduce values larger than INT_MAX to INT_MAX, and silently
3855-
boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3854+
Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3855+
and silently boost values less than -PY_SSIZE_T_MAX to 0.
3856+
Return 0 on error, 1 on success.
38563857
*/
38573858
/* Note: If v is NULL, return success without storing into *pi. This
38583859
is because_PyEval_SliceIndex() is called by apply_slice(), which can be
@@ -3862,11 +3863,11 @@ int
38623863
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
38633864
{
38643865
if (v != NULL) {
3865-
long x;
3866+
Py_ssize_t x;
38663867
if (PyInt_Check(v)) {
38673868
x = PyInt_AsLong(v);
38683869
} else if (PyLong_Check(v)) {
3869-
x = PyLong_AsLong(v);
3870+
x = PyInt_AsSsize_t(v);
38703871
if (x==-1 && PyErr_Occurred()) {
38713872
PyObject *long_zero;
38723873
int cmp;
@@ -3883,8 +3884,8 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
38833884

38843885
/* It's an overflow error, so we need to
38853886
check the sign of the long integer,
3886-
set the value to INT_MAX or -INT_MAX,
3887-
and clear the error. */
3887+
set the value to PY_SSIZE_T_MAX or
3888+
-PY_SSIZE_T_MAX, and clear the error. */
38883889

38893890
/* Create a long integer with a value of 0 */
38903891
long_zero = PyLong_FromLong(0L);
@@ -3898,21 +3899,15 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
38983899
if (cmp < 0)
38993900
return 0;
39003901
else if (cmp)
3901-
x = INT_MAX;
3902+
x = PY_SSIZE_T_MAX;
39023903
else
3903-
x = -INT_MAX;
3904+
x = -PY_SSIZE_T_MAX;
39043905
}
39053906
} else {
39063907
PyErr_SetString(PyExc_TypeError,
39073908
"slice indices must be integers");
39083909
return 0;
39093910
}
3910-
/* Truncate -- very long indices are truncated anyway */
3911-
/* XXX truncate by ssize maximum */
3912-
if (x > INT_MAX)
3913-
x = INT_MAX;
3914-
else if (x < -INT_MAX)
3915-
x = -INT_MAX;
39163911
*pi = x;
39173912
}
39183913
return 1;
@@ -3928,7 +3923,7 @@ apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
39283923
PySequenceMethods *sq = tp->tp_as_sequence;
39293924

39303925
if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3931-
Py_ssize_t ilow = 0, ihigh = INT_MAX;
3926+
Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
39323927
if (!_PyEval_SliceIndex(v, &ilow))
39333928
return NULL;
39343929
if (!_PyEval_SliceIndex(w, &ihigh))
@@ -3955,7 +3950,7 @@ assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
39553950
PySequenceMethods *sq = tp->tp_as_sequence;
39563951

39573952
if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3958-
Py_ssize_t ilow = 0, ihigh = INT_MAX;
3953+
Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
39593954
if (!_PyEval_SliceIndex(v, &ilow))
39603955
return -1;
39613956
if (!_PyEval_SliceIndex(w, &ihigh))

0 commit comments

Comments
 (0)