Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jsonpointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ def _set_child(parent, part, value):
elif isinstance(parent, Sequence) and part == '-':
try:
parent.append(value)
except AttributeError as e:
# immutable sequences, such as tuples, can't be appended to
except (AttributeError, TypeError) as e:
# Immutable sequences can't be appended to; typed sequences
# can reject the value just like an indexed assignment.
raise JsonPointerException("Invalid assignment target: %s" % (e,))
else:
try:
Expand Down
28 changes: 28 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,34 @@ def test_set_create_off_by_default(self):
"/cat/name", "whiskers")
self.assertEqual(doc, {})

def test_set_sequence_append_invalid_type(self):
for pointer, create in (("/items/0", False), ("/items/-", False),
("/items/-", True), ("/items/1", True)):
for inplace in (False, True):
with self.subTest(pointer=pointer, create=create, inplace=inplace):
doc = {"items": bytearray([1])}
with self.assertRaisesRegex(JsonPointerException, "Invalid assignment target:"):
set_pointer(doc, pointer, "invalid", inplace=inplace, create=create)
self.assertEqual(doc, {"items": bytearray([1])})

def test_set_create_sequence_append_invalid_child(self):
for pointer in ("/items/-/leaf", "/items/1/leaf"):
with self.subTest(pointer=pointer):
doc = {"items": bytearray([1])}
with self.assertRaisesRegex(JsonPointerException, "Invalid assignment target:"):
set_pointer(doc, pointer, 7, create=True)
self.assertEqual(doc, {"items": bytearray([1])})

def test_set_sequence_append_valid_type(self):
for pointer, create in (("/items/-", False), ("/items/-", True),
("/items/1", True)):
for inplace in (False, True):
with self.subTest(pointer=pointer, create=create, inplace=inplace):
doc = {"items": bytearray([1])}
result = set_pointer(doc, pointer, 2, inplace=inplace, create=create)
self.assertEqual(result, {"items": bytearray([1, 2])})
self.assertEqual(doc, result if inplace else {"items": bytearray([1])})


class AltTypesTests(unittest.TestCase):
class Node(object):
Expand Down
Loading