bpo-29935: Fixed error messages in the index() method of tuple, list and deque (#887) (#907)

when pass indices of wrong type.
(cherry picked from commit d4edfc9abf)
This commit is contained in:
Serhiy Storchaka 2017-03-30 19:46:59 +03:00 committed by GitHub
parent a6b4e19022
commit bf4bb2e430
6 changed files with 31 additions and 11 deletions

View file

@ -5074,14 +5074,10 @@ do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Return 0 on error, 1 on success.
*/
/* Note: If v is NULL, return success without storing into *pi. This
is because_PyEval_SliceIndex() is called by apply_slice(), which can be
called by the SLICE opcode with v and/or w equal to NULL.
*/
int
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
{
if (v != NULL) {
if (v != Py_None) {
Py_ssize_t x;
if (PyIndex_Check(v)) {
x = PyNumber_AsSsize_t(v, NULL);
@ -5099,6 +5095,26 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
return 1;
}
int
_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
{
Py_ssize_t x;
if (PyIndex_Check(v)) {
x = PyNumber_AsSsize_t(v, NULL);
if (x == -1 && PyErr_Occurred())
return 0;
}
else {
PyErr_SetString(PyExc_TypeError,
"slice indices must be integers or "
"have an __index__ method");
return 0;
}
*pi = x;
return 1;
}
#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
"BaseException is not allowed"