Skip to content

Commit a6fdddb

Browse files
bpo-32500: Fix error messages for sequence and mapping C API. (GH-7846)
Fix error messages for PySequence_Size(), PySequence_GetItem(), PySequence_SetItem() and PySequence_DelItem() called with a mapping and PyMapping_Size() called with a sequence.
1 parent aba24ff commit a6fdddb

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fixed error messages for :c:func:`PySequence_Size`,
2+
:c:func:`PySequence_GetItem`, :c:func:`PySequence_SetItem` and
3+
:c:func:`PySequence_DelItem` called with a mapping and
4+
:c:func:`PyMapping_Size` called with a sequence.

Objects/abstract.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,10 @@ PySequence_Size(PyObject *s)
15311531
return len;
15321532
}
15331533

1534+
if (s->ob_type->tp_as_mapping && s->ob_type->tp_as_mapping->mp_length) {
1535+
type_error("%.200s is not a sequence", s);
1536+
return -1;
1537+
}
15341538
type_error("object of type '%.200s' has no len()", s);
15351539
return -1;
15361540
}
@@ -1677,6 +1681,9 @@ PySequence_GetItem(PyObject *s, Py_ssize_t i)
16771681
return m->sq_item(s, i);
16781682
}
16791683

1684+
if (s->ob_type->tp_as_mapping && s->ob_type->tp_as_mapping->mp_subscript) {
1685+
return type_error("%.200s is not a sequence", s);
1686+
}
16801687
return type_error("'%.200s' object does not support indexing", s);
16811688
}
16821689

@@ -1728,6 +1735,10 @@ PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
17281735
return m->sq_ass_item(s, i, o);
17291736
}
17301737

1738+
if (s->ob_type->tp_as_mapping && s->ob_type->tp_as_mapping->mp_ass_subscript) {
1739+
type_error("%.200s is not a sequence", s);
1740+
return -1;
1741+
}
17311742
type_error("'%.200s' object does not support item assignment", s);
17321743
return -1;
17331744
}
@@ -1757,6 +1768,10 @@ PySequence_DelItem(PyObject *s, Py_ssize_t i)
17571768
return m->sq_ass_item(s, i, (PyObject *)NULL);
17581769
}
17591770

1771+
if (s->ob_type->tp_as_mapping && s->ob_type->tp_as_mapping->mp_ass_subscript) {
1772+
type_error("%.200s is not a sequence", s);
1773+
return -1;
1774+
}
17601775
type_error("'%.200s' object doesn't support item deletion", s);
17611776
return -1;
17621777
}
@@ -2093,6 +2108,11 @@ PyMapping_Size(PyObject *o)
20932108
return len;
20942109
}
20952110

2111+
if (o->ob_type->tp_as_sequence && o->ob_type->tp_as_sequence->sq_length) {
2112+
type_error("%.200s is not a mapping", o);
2113+
return -1;
2114+
}
2115+
/* PyMapping_Size() can be called from PyObject_Size(). */
20962116
type_error("object of type '%.200s' has no len()", o);
20972117
return -1;
20982118
}

0 commit comments

Comments
 (0)