changeset 27:a607cb7f6333

Fix #1: KeyError instead of returning [] The `get_all` method would raise a KeyError instead of returning an empty list when it was looking for a single key and it didn't exist.
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Mon, 22 Jan 2018 11:31:52 +0100
parents f0858a46b6b9
children 0a216b982fd1
files jpath.py
diffstat 1 files changed, 7 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/jpath.py	Fri Aug 16 11:54:17 2013 +0200
+++ b/jpath.py	Mon Jan 22 11:31:52 2018 +0100
@@ -130,7 +130,11 @@
         under token's value in data.
         """
 
-        return Token.apply(tokens, data[self.value])
+        try:
+            value = data[self.value]
+        except (IndexError, KeyError):
+            return []
+        return Token.apply(tokens, value)
 
 
 @Tokenizer.token
@@ -395,6 +399,8 @@
     [{'a': 1, 'x': [2], 'c': [{'b': '3'}]}, 1, [2], [{'b': '3'}], {'b': '3'}, 2]
     >>> get_all('a[*].b.c', {'a': [{'b': {'c': 0}}, {'b': {'c': 1}}]})
     [0, 1]
+    >>> get_all('a', {})
+    []
     """
 
     tokens = list(TOKENIZER.tokenize(jpath))