jsonpointer 3.8__tar.gz → 3.10__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpointer
3
- Version: 3.8
3
+ Version: 3.10
4
4
  Summary: Identify specific nodes in a JSON document (RFC 6901)
5
5
  Home-page: https://github.com/stefankoegl/python-json-pointer
6
6
  Author: Stefan Kögl
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpointer
3
- Version: 3.8
3
+ Version: 3.10
4
4
  Summary: Identify specific nodes in a JSON document (RFC 6901)
5
5
  Home-page: https://github.com/stefankoegl/python-json-pointer
6
6
  Author: Stefan Kögl
@@ -32,7 +32,7 @@
32
32
 
33
33
  # Will be parsed by setup.py to determine package metadata
34
34
  __author__ = 'Stefan Kögl <stefan@skoegl.net>'
35
- __version__ = '3.8'
35
+ __version__ = '3.10'
36
36
  __website__ = 'https://github.com/stefankoegl/python-json-pointer'
37
37
  __license__ = 'Modified BSD License'
38
38
 
@@ -275,8 +275,9 @@ class JsonPointer:
275
275
  elif isinstance(parent, Sequence) and part == '-':
276
276
  try:
277
277
  parent.append(value)
278
- except AttributeError as e:
279
- # immutable sequences, such as tuples, can't be appended to
278
+ except (AttributeError, TypeError) as e:
279
+ # Immutable sequences can't be appended to; typed sequences
280
+ # can reject the value just like an indexed assignment.
280
281
  raise JsonPointerException("Invalid assignment target: %s" % (e,))
281
282
  else:
282
283
  try:
@@ -340,6 +341,12 @@ class JsonPointer:
340
341
 
341
342
  # Else the object is a mapping or supports __getitem__(so assume custom indexing)
342
343
  try:
344
+ # Check membership first so that mappings with a fallback for
345
+ # missing keys (e.g. defaultdict.__missing__) are not mutated and
346
+ # absent members are reported as missing.
347
+ if isinstance(doc, Mapping) and part not in doc:
348
+ raise KeyError(part)
349
+
343
350
  return doc[part]
344
351
 
345
352
  except KeyError:
@@ -12,6 +12,7 @@ import sys
12
12
  import tempfile
13
13
  import textwrap
14
14
  import unittest
15
+ from collections import defaultdict
15
16
  from contextlib import redirect_stderr, redirect_stdout
16
17
  from unittest import mock
17
18
 
@@ -83,6 +84,17 @@ class SpecificationTests(unittest.TestCase):
83
84
  new_ptr = JsonPointer.from_parts(parts)
84
85
  self.assertEqual(ptr, new_ptr)
85
86
 
87
+ def test_escaped_tilde_is_not_decoded_twice(self):
88
+ # RFC 6901 section 4: decode ~1 before ~0, so ~01 becomes ~1, not /.
89
+ pointer = JsonPointer('/~01')
90
+ self.assertEqual(pointer.parts, ['~1'])
91
+ self.assertEqual(JsonPointer.from_parts(pointer.parts).path, '/~01')
92
+
93
+ doc = {'~1': 'tilde member', '/': 'slash member'}
94
+ self.assertEqual(resolve_pointer(doc, '/~01'), 'tilde member')
95
+ set_pointer(doc, '/~01', 'updated')
96
+ self.assertEqual(doc, {'~1': 'updated', '/': 'slash member'})
97
+
86
98
  def test_str_and_repr(self):
87
99
  paths = [
88
100
  ("", "", "JsonPointer('')"),
@@ -456,6 +468,34 @@ class SetTests(unittest.TestCase):
456
468
  "/cat/name", "whiskers")
457
469
  self.assertEqual(doc, {})
458
470
 
471
+ def test_set_sequence_append_invalid_type(self):
472
+ for pointer, create in (("/items/0", False), ("/items/-", False),
473
+ ("/items/-", True), ("/items/1", True)):
474
+ for inplace in (False, True):
475
+ with self.subTest(pointer=pointer, create=create, inplace=inplace):
476
+ doc = {"items": bytearray([1])}
477
+ with self.assertRaisesRegex(JsonPointerException, "Invalid assignment target:"):
478
+ set_pointer(doc, pointer, "invalid", inplace=inplace, create=create)
479
+ self.assertEqual(doc, {"items": bytearray([1])})
480
+
481
+ def test_set_create_sequence_append_invalid_child(self):
482
+ for pointer in ("/items/-/leaf", "/items/1/leaf"):
483
+ with self.subTest(pointer=pointer):
484
+ doc = {"items": bytearray([1])}
485
+ with self.assertRaisesRegex(JsonPointerException, "Invalid assignment target:"):
486
+ set_pointer(doc, pointer, 7, create=True)
487
+ self.assertEqual(doc, {"items": bytearray([1])})
488
+
489
+ def test_set_sequence_append_valid_type(self):
490
+ for pointer, create in (("/items/-", False), ("/items/-", True),
491
+ ("/items/1", True)):
492
+ for inplace in (False, True):
493
+ with self.subTest(pointer=pointer, create=create, inplace=inplace):
494
+ doc = {"items": bytearray([1])}
495
+ result = set_pointer(doc, pointer, 2, inplace=inplace, create=create)
496
+ self.assertEqual(result, {"items": bytearray([1, 2])})
497
+ self.assertEqual(doc, result if inplace else {"items": bytearray([1])})
498
+
459
499
 
460
500
  class AltTypesTests(unittest.TestCase):
461
501
  class Node(object):
@@ -554,6 +594,35 @@ class AltTypesTests(unittest.TestCase):
554
594
  self.assertRaises(JsonPointerException, resolve_pointer, doc, '/root/1/2/3/4')
555
595
 
556
596
 
597
+ class DefaultDictTests(unittest.TestCase):
598
+ """Missing members of mappings with __missing__ must stay missing (#101)"""
599
+
600
+ def test_resolve_missing_returns_default(self):
601
+ doc = defaultdict(dict)
602
+ self.assertEqual(resolve_pointer(doc, '/missing', 'SENTINEL'), 'SENTINEL')
603
+ self.assertEqual(dict(doc), {})
604
+
605
+ def test_resolve_missing_raises(self):
606
+ doc = defaultdict(dict)
607
+ self.assertRaises(JsonPointerException, resolve_pointer, doc, '/missing')
608
+ self.assertEqual(dict(doc), {})
609
+
610
+ def test_resolve_existing_member(self):
611
+ doc = defaultdict(dict, {'foo': {'bar': 1}})
612
+ self.assertEqual(resolve_pointer(doc, '/foo/bar'), 1)
613
+
614
+ def test_set_without_create_raises(self):
615
+ doc = defaultdict(dict)
616
+ self.assertRaises(JsonPointerException, set_pointer, doc,
617
+ '/missing/child', 1)
618
+ self.assertEqual(dict(doc), {})
619
+
620
+ def test_set_with_create(self):
621
+ doc = defaultdict(dict)
622
+ set_pointer(doc, '/missing/child', 1, create=True)
623
+ self.assertEqual(dict(doc), {'missing': {'child': 1}})
624
+
625
+
557
626
  class CommandLineTests(unittest.TestCase):
558
627
  """ Tests the jsonpointer command line utility """
559
628
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes