From 6dd658934ac7c6d1eba7b726d73552c13e2ebc1d Mon Sep 17 00:00:00 2001 From: Alexander Shorin Date: Sun, 25 Dec 2011 16:56:06 +0400 Subject: [PATCH] Do not hide any problems. --- jsonpatch.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/jsonpatch.py b/jsonpatch.py index be92fbe..14b2162 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -131,15 +131,20 @@ class PatchOperation(object): def _step(self, obj, loc_part, must_exist=True): """ Goes one step in a locate() call """ - # Its not clear if a location "1" should be considered as 1 or "1" - # We prefer the integer-variant if possible - part_variants = self._try_parse(loc_part) + [loc_part] - - for variant in part_variants: - try: + if isinstance(obj, dict): + part_variants = [loc_part] + for variant in part_variants: + if variant not in obj: + continue return obj[variant], variant - except: - continue + elif isinstance(obj, list): + part_variants = [int(loc_part)] + for variant in part_variants: + if variant >= len(obj): + continue + return obj[variant], variant + else: + raise ValueError('list or dict expected, got %r' % type(obj)) if must_exist: raise JsonPatchConflict('key %s not found' % loc_part) @@ -147,14 +152,6 @@ class PatchOperation(object): return obj, part_variants[0] - @staticmethod - def _try_parse(val, cls=int): - try: - return [cls(val)] - except: - return [] - - class RemoveOperation(PatchOperation): """ Removes an object property or an array element