Skip to content

Commit 76b542b

Browse files
committed
Reduce number of isinstance() calls in get_part
1 parent 3f460a5 commit 76b542b

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

jsonpointer.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,27 +196,14 @@ def set(self, doc, value, inplace=True):
196196
def get_part(self, doc, part):
197197
""" Returns the next step in the correct type """
198198

199-
if isinstance(doc, Mapping):
200-
return part
201-
202-
elif isinstance(doc, Sequence):
203-
199+
if isinstance(doc, Sequence):
204200
if part == '-':
205201
return part
206-
207202
if not RE_ARRAY_INDEX.match(str(part)):
208203
raise JsonPointerException("'%s' is not a valid list index" % (part, ))
209-
210204
return int(part)
211-
212-
elif hasattr(doc, '__getitem__'):
213-
# Allow indexing via ducktyping if the target has defined __getitem__
214-
return part
215-
216205
else:
217-
raise JsonPointerException("Document '%s' does not support indexing, "
218-
"must be dict/list or support __getitem__" % type(doc))
219-
206+
return part
220207

221208
def walk(self, doc, part):
222209
""" Walks one step in doc and returns the referenced part """
@@ -231,6 +218,10 @@ def walk(self, doc, part):
231218
raise JsonPointerException("member '%s' not found in %s" % (part, doc))
232219
except IndexError:
233220
raise JsonPointerException("index '%s' is out of bounds" % (part, ))
221+
except TypeError:
222+
raise JsonPointerException("Document '%s' does not support indexing, "
223+
"must be dict/list or support __getitem__" % type(doc))
224+
234225

235226
def contains(self, ptr):
236227
"""Returns True if self contains the given ptr"""

0 commit comments

Comments
 (0)