Skip to content

Commit 4c5cb49

Browse files
committed
Using the same handling for the case in which the doc is a mapping or an object that support __getitem__(but not a sequence), Updated the tests
1 parent 8a371c7 commit 4c5cb49

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

jsonpointer.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,7 @@ def walk(self, doc, part):
225225

226226
assert (type(doc) in (dict, list) or hasattr(doc, '__getitem__')), "invalid document type %s" % (type(doc),)
227227

228-
if isinstance(doc, Mapping):
229-
try:
230-
return doc[part]
231-
232-
except KeyError:
233-
raise JsonPointerException("member '%s' not found in %s" % (part, doc))
234-
235-
elif isinstance(doc, Sequence):
236-
228+
if isinstance(doc, Sequence):
237229
if part == '-':
238230
return EndOfList(doc)
239231

@@ -243,10 +235,14 @@ def walk(self, doc, part):
243235
except IndexError:
244236
raise JsonPointerException("index '%s' is out of bounds" % (part, ))
245237

246-
else:
247-
# Object supports __getitem__, assume custom indexing
238+
# Else the object is a mapping or supports __getitem__(so assume custom indexing)
239+
try:
248240
return doc[part]
249241

242+
except KeyError:
243+
raise JsonPointerException("member '%s' not found in %s" % (part, doc))
244+
245+
250246
def contains(self, ptr):
251247
""" Returns True if self contains the given ptr """
252248
return len(self.parts) > len(ptr.parts) and \

tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ def test_mock_dict_returns_default(self):
277277

278278
def test_mock_dict_raises_key_error(self):
279279
doc = self.mdict
280-
self.assertRaises(KeyError, resolve_pointer, doc, '/foo')
280+
self.assertRaises(JsonPointerException, resolve_pointer, doc, '/foo')
281+
self.assertRaises(JsonPointerException, resolve_pointer, doc, '/root/1/2/3/4')
281282

282283

283284

0 commit comments

Comments
 (0)