diff --git a/Python/ceval.c b/Python/ceval.c index 8904d7a..ecbb7e2 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1591,7 +1591,30 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) TARGET(BINARY_SUBSCR) { PyObject *sub = POP(); PyObject *container = TOP(); - PyObject *res = PyObject_GetItem(container, sub); + PyObject *res; + Py_ssize_t index; + int slow_path = 1; + if (PyLong_CheckExact(sub) && Py_ABS(Py_SIZE(sub)) <= 1) { + if (PyList_CheckExact(container)) { + index = PyLong_AsSsize_t(sub); + if (index < 0) { + index += Py_SIZE(container); + } + if (index >= 0 && index < Py_SIZE(container)) { + res = PyList_GET_ITEM(container, index); + Py_INCREF(res); + } + else { + res = NULL; + PyErr_SetString(PyExc_IndexError, + "list index out of range"); + } + slow_path = 0; + } + } + if (slow_path) { + res = PyObject_GetItem(container, sub); + } Py_DECREF(container); Py_DECREF(sub); SET_TOP(res);