diff options
author | Guido van Rossum <guido@python.org> | 1998-08-13 16:44:44 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-08-13 16:44:44 (GMT) |
commit | 21308243ca028e06f03336f9d84edbeebcfb1da2 (patch) | |
tree | 875845f5b2b9c5c30500f0047d5a13c59034886c | |
parent | 54047c84d11e8c3731aef553316f12b7f15397cf (diff) | |
download | cpython-21308243ca028e06f03336f9d84edbeebcfb1da2.zip cpython-21308243ca028e06f03336f9d84edbeebcfb1da2.tar.gz cpython-21308243ca028e06f03336f9d84edbeebcfb1da2.tar.bz2 |
Better error messages when a sequence is indexed with a non-integer.
Previously, this said "unsubscriptable object"; in 1.5.1, the reverse
problem existed, where None[''] would complain about a non-integer
index. This fix does the right thing in all cases (for get, set and
del item).
-rw-r--r-- | Objects/abstract.c | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 7458b94..123455a 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -232,8 +232,11 @@ PyObject_GetItem(o, key) if (m && m->mp_subscript) return m->mp_subscript(o, key); - if (PyInt_Check(key)) - return PySequence_GetItem(o, PyInt_AsLong(key)); + if (o->ob_type->tp_as_sequence) { + if (PyInt_Check(key)) + return PySequence_GetItem(o, PyInt_AsLong(key)); + return type_error("sequence index must be integer"); + } return type_error("unsubscriptable object"); } @@ -254,8 +257,12 @@ PyObject_SetItem(o, key, value) if (m && m->mp_ass_subscript) return m->mp_ass_subscript(o, key, value); - if (PyInt_Check(key)) - return PySequence_SetItem(o, PyInt_AsLong(key), value); + if (o->ob_type->tp_as_sequence) { + if (PyInt_Check(key)) + return PySequence_SetItem(o, PyInt_AsLong(key), value); + type_error("sequence index must be integer"); + return -1; + } type_error("object does not support item assignment"); return -1; @@ -276,8 +283,12 @@ PyObject_DelItem(o, key) if (m && m->mp_ass_subscript) return m->mp_ass_subscript(o, key, (PyObject*)NULL); - if (PyInt_Check(key)) - return PySequence_DelItem(o, PyInt_AsLong(key)); + if (o->ob_type->tp_as_sequence) { + if (PyInt_Check(key)) + return PySequence_DelItem(o, PyInt_AsLong(key)); + type_error("sequence index must be integer"); + return -1; + } type_error("object does not support item deletion"); return -1; |