Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2130824

Browse files
committed
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).
1 parent 54047c8 commit 2130824

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

Objects/abstract.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,11 @@ PyObject_GetItem(o, key)
232232
if (m && m->mp_subscript)
233233
return m->mp_subscript(o, key);
234234

235-
if (PyInt_Check(key))
236-
return PySequence_GetItem(o, PyInt_AsLong(key));
235+
if (o->ob_type->tp_as_sequence) {
236+
if (PyInt_Check(key))
237+
return PySequence_GetItem(o, PyInt_AsLong(key));
238+
return type_error("sequence index must be integer");
239+
}
237240

238241
return type_error("unsubscriptable object");
239242
}
@@ -254,8 +257,12 @@ PyObject_SetItem(o, key, value)
254257
if (m && m->mp_ass_subscript)
255258
return m->mp_ass_subscript(o, key, value);
256259

257-
if (PyInt_Check(key))
258-
return PySequence_SetItem(o, PyInt_AsLong(key), value);
260+
if (o->ob_type->tp_as_sequence) {
261+
if (PyInt_Check(key))
262+
return PySequence_SetItem(o, PyInt_AsLong(key), value);
263+
type_error("sequence index must be integer");
264+
return -1;
265+
}
259266

260267
type_error("object does not support item assignment");
261268
return -1;
@@ -276,8 +283,12 @@ PyObject_DelItem(o, key)
276283
if (m && m->mp_ass_subscript)
277284
return m->mp_ass_subscript(o, key, (PyObject*)NULL);
278285

279-
if (PyInt_Check(key))
280-
return PySequence_DelItem(o, PyInt_AsLong(key));
286+
if (o->ob_type->tp_as_sequence) {
287+
if (PyInt_Check(key))
288+
return PySequence_DelItem(o, PyInt_AsLong(key));
289+
type_error("sequence index must be integer");
290+
return -1;
291+
}
281292

282293
type_error("object does not support item deletion");
283294
return -1;

0 commit comments

Comments
 (0)