Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch:
Changeset: r94684:6167d7594c32
Date: 2018-05-25 10:13 +0200
http://bitbucket.org/pypy/pypy/changeset/6167d7594c32/
Log: add the key used in the subscription when trying to use getitem on
an object that doesn't support that.
This is a commonly named unhelpful error message that occurs when
doing subscriptions in a chain (eg when processing JSON data):
d[a][b][c][d]
if one of the intermediate dictionaries is None, it's very hard to
see which one just from at the exception.
diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -298,7 +298,8 @@
w_descr = space.lookup(w_obj, '__getitem__')
if w_descr is None:
raise oefmt(space.w_TypeError,
- "'%T' object is not subscriptable", w_obj)
+ "'%T' object is not subscriptable (key %R)",
+ w_obj, w_key)
return space.get_and_call_function(w_descr, w_obj, w_key)
def setitem(space, w_obj, w_key, w_val):
diff --git a/pypy/objspace/std/test/test_noneobject.py
b/pypy/objspace/std/test/test_noneobject.py
--- a/pypy/objspace/std/test/test_noneobject.py
+++ b/pypy/objspace/std/test/test_noneobject.py
@@ -8,3 +8,4 @@
def test_false(self):
assert not self.space.is_true(self.space.w_None)
+
diff --git a/pypy/objspace/test/test_descroperation.py
b/pypy/objspace/test/test_descroperation.py
--- a/pypy/objspace/test/test_descroperation.py
+++ b/pypy/objspace/test/test_descroperation.py
@@ -812,3 +812,8 @@
class A(object):
d = D()
raises(AttributeError, "A().d = 5")
+
+ def test_not_subscriptable_error_gives_keys(self):
+ d = {'key1': {'key2': {'key3': None}}}
+ excinfo = raises(TypeError,
"d['key1']['key2']['key3']['key4']['key5']")
+ assert "key4" in str(excinfo.value)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit