From b9d10d6954e28c0cb451542a0533d0d67c5171c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=8E=AE?= Date: Sat, 9 Nov 2019 00:53:09 +0800 Subject: [PATCH] fix slice value is None --- jsonpath_rw/jsonpath.py | 3 +++ tests/test_jsonpath.py | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/jsonpath_rw/jsonpath.py b/jsonpath_rw/jsonpath.py index c47653e..4dc9bdd 100644 --- a/jsonpath_rw/jsonpath.py +++ b/jsonpath_rw/jsonpath.py @@ -535,6 +535,9 @@ def __init__(self, start=None, end=None, step=None): def find(self, datum): datum = DatumInContext.wrap(datum) + if datum.value is None: + return [] + # Here's the hack. If it is a dictionary or some kind of constant, # put it in a single-element list if (isinstance(datum.value, dict) or isinstance(datum.value, six.integer_types) or isinstance(datum.value, six.string_types)): diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py index 33c0ea6..4912141 100644 --- a/tests/test_jsonpath.py +++ b/tests/test_jsonpath.py @@ -138,7 +138,8 @@ def test_slice_value(self): self.check_cases([('[*]', [1, 2, 3], [1, 2, 3]), ('[*]', xrange(1, 4), [1, 2, 3]), ('[1:]', [1, 2, 3, 4], [2, 3, 4]), - ('[:2]', [1, 2, 3, 4], [1, 2])]) + ('[:2]', [1, 2, 3, 4], [1, 2]), + ('[*]', None, [])]) # Funky slice hacks self.check_cases([