mirror of
https://github.com/python/cpython.git
synced 2025-10-03 13:45:29 +00:00
when pass indices of wrong type. (cherry picked from commitd4edfc9abf
) (cherry picked from commitbf4bb2e430
)
This commit is contained in:
parent
c90ff1b78c
commit
8b8bde44f3
6 changed files with 31 additions and 11 deletions
|
@ -203,6 +203,7 @@ PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
|
||||||
|
|
||||||
#ifndef Py_LIMITED_API
|
#ifndef Py_LIMITED_API
|
||||||
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
|
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
|
||||||
|
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
|
||||||
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
|
PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@ Release date: XXXX-XX-XX
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- bpo-29935: Fixed error messages in the index() method of tuple, list and deque
|
||||||
|
when pass indices of wrong type.
|
||||||
|
|
||||||
- bpo-28876: ``bool(range)`` works even if ``len(range)``
|
- bpo-28876: ``bool(range)`` works even if ``len(range)``
|
||||||
raises :exc:`OverflowError`.
|
raises :exc:`OverflowError`.
|
||||||
|
|
||||||
|
|
|
@ -913,8 +913,8 @@ deque_index(dequeobject *deque, PyObject *args)
|
||||||
size_t start_state = deque->state;
|
size_t start_state = deque->state;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
||||||
_PyEval_SliceIndex, &start,
|
_PyEval_SliceIndexNotNone, &start,
|
||||||
_PyEval_SliceIndex, &stop))
|
_PyEval_SliceIndexNotNone, &stop))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (start < 0) {
|
if (start < 0) {
|
||||||
start += Py_SIZE(deque);
|
start += Py_SIZE(deque);
|
||||||
|
|
|
@ -2154,8 +2154,8 @@ listindex(PyListObject *self, PyObject *args)
|
||||||
PyObject *v;
|
PyObject *v;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
||||||
_PyEval_SliceIndex, &start,
|
_PyEval_SliceIndexNotNone, &start,
|
||||||
_PyEval_SliceIndex, &stop))
|
_PyEval_SliceIndexNotNone, &stop))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (start < 0) {
|
if (start < 0) {
|
||||||
start += Py_SIZE(self);
|
start += Py_SIZE(self);
|
||||||
|
|
|
@ -515,8 +515,8 @@ tupleindex(PyTupleObject *self, PyObject *args)
|
||||||
PyObject *v;
|
PyObject *v;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
||||||
_PyEval_SliceIndex, &start,
|
_PyEval_SliceIndexNotNone, &start,
|
||||||
_PyEval_SliceIndex, &stop))
|
_PyEval_SliceIndexNotNone, &stop))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (start < 0) {
|
if (start < 0) {
|
||||||
start += Py_SIZE(self);
|
start += Py_SIZE(self);
|
||||||
|
|
|
@ -5098,14 +5098,10 @@ ext_call_fail:
|
||||||
and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
|
and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
|
||||||
Return 0 on error, 1 on success.
|
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
|
int
|
||||||
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
|
_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
|
||||||
{
|
{
|
||||||
if (v != NULL) {
|
if (v != Py_None) {
|
||||||
Py_ssize_t x;
|
Py_ssize_t x;
|
||||||
if (PyIndex_Check(v)) {
|
if (PyIndex_Check(v)) {
|
||||||
x = PyNumber_AsSsize_t(v, NULL);
|
x = PyNumber_AsSsize_t(v, NULL);
|
||||||
|
@ -5123,6 +5119,26 @@ _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
|
||||||
return 1;
|
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 "\
|
#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
|
||||||
"BaseException is not allowed"
|
"BaseException is not allowed"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue