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

Skip to content

Commit 2194b16

Browse files
committed
Allow using long integers as slice indexes
1 parent 52fccfd commit 2194b16

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

Python/ceval.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2535,12 +2535,44 @@ slice_index(v, pi)
25352535
{
25362536
if (v != NULL) {
25372537
long x;
2538-
if (!PyInt_Check(v)) {
2538+
if (PyInt_Check(v)) {
2539+
x = PyInt_AsLong(v);
2540+
} else if (PyLong_Check(v)) {
2541+
x = PyLong_AsLong(v);
2542+
if (x==-1 && PyErr_Occurred()) {
2543+
PyObject *long_zero;
2544+
2545+
if (!PyErr_ExceptionMatches( PyExc_OverflowError ) ) {
2546+
/* It's not an overflow error, so just
2547+
signal an error */
2548+
return -1;
2549+
}
2550+
2551+
/* It's an overflow error, so we need to
2552+
check the sign of the long integer,
2553+
set the value to INT_MAX or 0, and clear
2554+
the error. */
2555+
2556+
/* Create a long integer with a value of 0 */
2557+
long_zero = PyLong_FromLong( 0L );
2558+
if (long_zero == NULL) return -1;
2559+
2560+
/* Check sign */
2561+
if (PyObject_Compare(long_zero, v) < 0)
2562+
x = INT_MAX;
2563+
else
2564+
x = 0;
2565+
2566+
/* Free the long integer we created, and clear the
2567+
OverflowError */
2568+
Py_DECREF(long_zero);
2569+
PyErr_Clear();
2570+
}
2571+
} else {
25392572
PyErr_SetString(PyExc_TypeError,
25402573
"slice index must be int");
25412574
return -1;
25422575
}
2543-
x = PyInt_AsLong(v);
25442576
/* Truncate -- very long indices are truncated anyway */
25452577
if (x > INT_MAX)
25462578
x = INT_MAX;

0 commit comments

Comments
 (0)