jsonpatch 1.45__tar.gz → 1.46__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.
Files changed (28) hide show
  1. {jsonpatch-1.45/jsonpatch.egg-info → jsonpatch-1.46}/PKG-INFO +1 -1
  2. {jsonpatch-1.45 → jsonpatch-1.46/jsonpatch.egg-info}/PKG-INFO +1 -1
  3. {jsonpatch-1.45 → jsonpatch-1.46}/jsonpatch.py +5 -5
  4. {jsonpatch-1.45 → jsonpatch-1.46}/jsonpatch_cli.py +4 -4
  5. {jsonpatch-1.45 → jsonpatch-1.46}/tests.py +205 -1
  6. {jsonpatch-1.45 → jsonpatch-1.46}/AUTHORS +0 -0
  7. {jsonpatch-1.45 → jsonpatch-1.46}/LICENSE +0 -0
  8. {jsonpatch-1.45 → jsonpatch-1.46}/MANIFEST.in +0 -0
  9. {jsonpatch-1.45 → jsonpatch-1.46}/README.md +0 -0
  10. {jsonpatch-1.45 → jsonpatch-1.46}/doc/commandline.rst +0 -0
  11. {jsonpatch-1.45 → jsonpatch-1.46}/doc/index.rst +0 -0
  12. {jsonpatch-1.45 → jsonpatch-1.46}/doc/mod-jsonpatch.rst +0 -0
  13. {jsonpatch-1.45 → jsonpatch-1.46}/doc/tutorial.rst +0 -0
  14. {jsonpatch-1.45 → jsonpatch-1.46}/ext_tests.py +0 -0
  15. {jsonpatch-1.45 → jsonpatch-1.46}/jsonpatch-stubs/__init__.pyi +0 -0
  16. {jsonpatch-1.45 → jsonpatch-1.46}/jsonpatch.egg-info/SOURCES.txt +0 -0
  17. {jsonpatch-1.45 → jsonpatch-1.46}/jsonpatch.egg-info/dependency_links.txt +0 -0
  18. {jsonpatch-1.45 → jsonpatch-1.46}/jsonpatch.egg-info/entry_points.txt +0 -0
  19. {jsonpatch-1.45 → jsonpatch-1.46}/jsonpatch.egg-info/requires.txt +0 -0
  20. {jsonpatch-1.45 → jsonpatch-1.46}/jsonpatch.egg-info/top_level.txt +0 -0
  21. {jsonpatch-1.45 → jsonpatch-1.46}/property_tests.py +0 -0
  22. {jsonpatch-1.45 → jsonpatch-1.46}/pyproject.toml +0 -0
  23. {jsonpatch-1.45 → jsonpatch-1.46}/pytest.ini +0 -0
  24. {jsonpatch-1.45 → jsonpatch-1.46}/requirements.txt +0 -0
  25. {jsonpatch-1.45 → jsonpatch-1.46}/setup.cfg +0 -0
  26. {jsonpatch-1.45 → jsonpatch-1.46}/setup.py +0 -0
  27. {jsonpatch-1.45 → jsonpatch-1.46}/tests.js +0 -0
  28. {jsonpatch-1.45 → jsonpatch-1.46}/typetests/usage.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpatch
3
- Version: 1.45
3
+ Version: 1.46
4
4
  Summary: Apply JSON-Patches (RFC 6902)
5
5
  Home-page: https://github.com/stefankoegl/python-json-patch
6
6
  Author: Stefan Kögl
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpatch
3
- Version: 1.45
3
+ Version: 1.46
4
4
  Summary: Apply JSON-Patches (RFC 6902)
5
5
  Home-page: https://github.com/stefankoegl/python-json-patch
6
6
  Author: Stefan Kögl
@@ -59,7 +59,7 @@ _JSONPOINTER_FROM_PARTS = JsonPointer.from_parts.__func__
59
59
 
60
60
  # Will be parsed by setup.py to determine package metadata
61
61
  __author__ = 'Stefan Kögl <stefan@skoegl.net>'
62
- __version__ = '1.45'
62
+ __version__ = '1.46'
63
63
  __website__ = 'https://github.com/stefankoegl/python-json-patch'
64
64
  __license__ = 'BSD-3-Clause'
65
65
 
@@ -1577,13 +1577,13 @@ def _json_equal(first, second):
1577
1577
  if isinstance(first, bool) or isinstance(second, bool):
1578
1578
  return isinstance(first, bool) and isinstance(second, bool) and \
1579
1579
  first == second
1580
- if isinstance(first, MutableMapping) and \
1581
- isinstance(second, MutableMapping):
1580
+ if isinstance(first, Mapping) and isinstance(second, Mapping):
1582
1581
  return len(first) == len(second) and all(
1583
1582
  key in second and _json_equal(first[key], second[key])
1584
1583
  for key in first)
1585
- if isinstance(first, MutableSequence) and \
1586
- isinstance(second, MutableSequence):
1584
+ if isinstance(first, Sequence) and isinstance(second, Sequence) and \
1585
+ not isinstance(first, (str, bytes)) and \
1586
+ not isinstance(second, (str, bytes)):
1587
1587
  return len(first) == len(second) and all(
1588
1588
  _json_equal(*items) for items in zip(first, second))
1589
1589
  return first == second
@@ -13,8 +13,8 @@ from contextlib import ExitStack
13
13
 
14
14
  jsondiff_parser = argparse.ArgumentParser(
15
15
  prog='jsondiff', description='Diff two JSON files')
16
- jsondiff_parser.add_argument('FILE1', type=argparse.FileType('r', encoding='utf-8'))
17
- jsondiff_parser.add_argument('FILE2', type=argparse.FileType('r', encoding='utf-8'))
16
+ jsondiff_parser.add_argument('FILE1', type=argparse.FileType('r', encoding='utf-8-sig'))
17
+ jsondiff_parser.add_argument('FILE2', type=argparse.FileType('r', encoding='utf-8-sig'))
18
18
  jsondiff_parser.add_argument('--indent', type=int, default=None,
19
19
  help='Indent output by n spaces')
20
20
  jsondiff_parser.add_argument('-u', '--preserve-unicode', action='store_true',
@@ -51,9 +51,9 @@ def diff_files():
51
51
  jsonpatch_parser = argparse.ArgumentParser(
52
52
  prog='jsonpatch',
53
53
  description='Apply a JSON patch on a JSON file')
54
- jsonpatch_parser.add_argument('ORIGINAL', type=argparse.FileType('r', encoding='utf-8'),
54
+ jsonpatch_parser.add_argument('ORIGINAL', type=argparse.FileType('r', encoding='utf-8-sig'),
55
55
  help='Original file')
56
- jsonpatch_parser.add_argument('PATCH', type=argparse.FileType('r', encoding='utf-8'),
56
+ jsonpatch_parser.add_argument('PATCH', type=argparse.FileType('r', encoding='utf-8-sig'),
57
57
  nargs='?', default=sys.stdin,
58
58
  help='Patch file (read from stdin if omitted)')
59
59
  jsonpatch_parser.add_argument('--indent', type=int, default=None,
@@ -19,7 +19,7 @@ import os
19
19
  import stat
20
20
  import subprocess
21
21
  import tempfile
22
- from collections.abc import Mapping
22
+ from collections.abc import Mapping, Sequence
23
23
  from types import MappingProxyType
24
24
  from unittest import mock
25
25
 
@@ -446,6 +446,187 @@ class ApplyPatchTestCase(unittest.TestCase):
446
446
  jsonpatch.apply_patch(
447
447
  doc, [{'op': 'test', 'path': '/a', 'value': value}])
448
448
 
449
+ def test_test_read_only_mapping_preserves_boolean_types(self):
450
+ for mapping_type in (dict, MappingProxyType):
451
+ for nested in (False, True):
452
+ for actual, expected in ((True, 1), (False, 0),
453
+ (1, True), (0, False)):
454
+ with self.subTest(mapping_type=mapping_type, nested=nested,
455
+ actual=actual, expected=expected):
456
+ mapping = mapping_type({'a': actual})
457
+ doc = {'nested': mapping} if nested else mapping
458
+ path = '/nested' if nested else ''
459
+ original = json.dumps(doc, default=dict)
460
+ patch = [{'op': 'test', 'path': path,
461
+ 'value': {'a': expected}}]
462
+
463
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
464
+ jsonpatch.apply_patch(doc, patch, in_place=True)
465
+
466
+ self.assertEqual(json.dumps(doc, default=dict), original)
467
+ if nested:
468
+ self.assertIs(doc['nested'], mapping)
469
+
470
+ def test_test_nested_read_only_mapping_preserves_boolean_types(self):
471
+ documents = [
472
+ ({'nested': MappingProxyType({'a': True})},
473
+ {'nested': {'a': 1}}),
474
+ ([MappingProxyType({'a': False})], [{'a': 0}]),
475
+ (MappingProxyType({'nested': [True]}), {'nested': [1]}),
476
+ ]
477
+ for doc, value in documents:
478
+ with self.subTest(doc=doc, value=value):
479
+ original = json.dumps(doc, default=dict)
480
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
481
+ jsonpatch.apply_patch(
482
+ doc, [{'op': 'test', 'path': '', 'value': value}],
483
+ in_place=True)
484
+ self.assertEqual(json.dumps(doc, default=dict), original)
485
+
486
+ def test_test_read_only_mapping_success_preserves_input(self):
487
+ for mapping_type in (dict, MappingProxyType):
488
+ for nested in (False, True):
489
+ for actual, expected in ((1, 1.0), (1.0, 1), (True, True),
490
+ (False, False), (None, None)):
491
+ with self.subTest(mapping_type=mapping_type, nested=nested,
492
+ actual=actual, expected=expected):
493
+ mapping = mapping_type({'a': actual})
494
+ doc = {'nested': mapping} if nested else mapping
495
+ path = '/nested' if nested else ''
496
+ original = json.dumps(doc, default=dict)
497
+ result = jsonpatch.apply_patch(
498
+ doc, [{'op': 'test', 'path': path,
499
+ 'value': {'a': expected}}], in_place=True)
500
+
501
+ self.assertIs(result, doc)
502
+ self.assertEqual(json.dumps(doc, default=dict), original)
503
+ if nested:
504
+ self.assertIs(result['nested'], mapping)
505
+
506
+
507
+ def test_test_read_only_sequences_preserve_boolean_types(self):
508
+ for actual_type in (list, tuple):
509
+ for expected_type in (list, tuple):
510
+ for nested in (False, True):
511
+ for actual, expected in ((True, 1), (False, 0),
512
+ (1, True), (0, False)):
513
+ with self.subTest(actual_type=actual_type,
514
+ expected_type=expected_type,
515
+ nested=nested, actual=actual,
516
+ expected=expected):
517
+ sequence = actual_type([actual])
518
+ doc = {'nested': sequence} if nested else sequence
519
+ path = '/nested' if nested else ''
520
+ original = json.dumps(doc)
521
+ patch = [{'op': 'test', 'path': path,
522
+ 'value': expected_type([expected])}]
523
+
524
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
525
+ jsonpatch.apply_patch(doc, patch, in_place=True)
526
+
527
+ self.assertEqual(json.dumps(doc), original)
528
+ if nested:
529
+ self.assertIs(doc['nested'], sequence)
530
+
531
+ def test_test_read_only_sequence_success_preserves_input(self):
532
+ for actual_type in (list, tuple):
533
+ for expected_type in (list, tuple):
534
+ for nested in (False, True):
535
+ for actual, expected in ((1, 1.0), (1.0, 1), (True, True),
536
+ (False, False), (None, None)):
537
+ with self.subTest(actual_type=actual_type,
538
+ expected_type=expected_type,
539
+ nested=nested, actual=actual,
540
+ expected=expected):
541
+ sequence = actual_type([actual])
542
+ doc = {'nested': sequence} if nested else sequence
543
+ path = '/nested' if nested else ''
544
+ original = json.dumps(doc)
545
+ patch = [{'op': 'test', 'path': path,
546
+ 'value': expected_type([expected])}]
547
+
548
+ result = jsonpatch.apply_patch(doc, patch, in_place=True)
549
+
550
+ self.assertIs(result, doc)
551
+ self.assertEqual(json.dumps(doc), original)
552
+ if nested:
553
+ self.assertIs(doc['nested'], sequence)
554
+
555
+ def test_test_custom_read_only_sequences(self):
556
+ class FrozenSequence(Sequence):
557
+ def __init__(self, values):
558
+ self.values = tuple(values)
559
+
560
+ def __getitem__(self, index):
561
+ return self.values[index]
562
+
563
+ def __len__(self):
564
+ return len(self.values)
565
+
566
+ def snapshot(value):
567
+ return json.dumps(value, default=lambda value:
568
+ dict(value) if isinstance(value, Mapping)
569
+ else list(value))
570
+
571
+ cases = [
572
+ (FrozenSequence([True]), [1], False),
573
+ ([True], FrozenSequence([1]), False),
574
+ (FrozenSequence([1]), [1.0], True),
575
+ ([True], FrozenSequence([True]), True),
576
+ (FrozenSequence([]), (), True),
577
+ (FrozenSequence([{'a': (True,)}]), [{'a': [True]}], True),
578
+ (FrozenSequence([MappingProxyType({'a': (True,)})]),
579
+ [{'a': [1]}], False),
580
+ ([{'a': FrozenSequence([False])}], ({'a': (0,)},), False),
581
+ ]
582
+ for doc, value, succeeds in cases:
583
+ with self.subTest(doc=doc, value=value):
584
+ original, original_value = snapshot(doc), snapshot(value)
585
+ patch = [{'op': 'test', 'path': '', 'value': value}]
586
+ if succeeds:
587
+ self.assertIs(jsonpatch.apply_patch(
588
+ doc, patch, in_place=True), doc)
589
+ else:
590
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
591
+ jsonpatch.apply_patch(doc, patch, in_place=True)
592
+ self.assertEqual(snapshot(doc), original)
593
+ self.assertEqual(snapshot(value), original_value)
594
+
595
+ def test_test_read_only_sequence_contents_and_lengths(self):
596
+ for doc, value in [
597
+ ((True,), (1,)),
598
+ (({'a': (False,)},), [{'a': [0]}]),
599
+ ((), [1]),
600
+ ((1,), [1, 2]),
601
+ ((1, 2), [2, 1]),
602
+ ]:
603
+ with self.subTest(doc=doc, value=value):
604
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
605
+ jsonpatch.apply_patch(
606
+ doc, [{'op': 'test', 'path': '', 'value': value}],
607
+ in_place=True)
608
+
609
+ def test_test_strings_and_bytes_remain_scalar(self):
610
+ for doc, value, succeeds in [
611
+ ('', '', True),
612
+ ('text', 'text', True),
613
+ (b'', b'', True),
614
+ (b'bytes', b'bytes', True),
615
+ ('text', 'next', False),
616
+ (b'bytes', b'other', False),
617
+ ('text', ('t', 'e', 'x', 't'), False),
618
+ (('t', 'e', 'x', 't'), 'text', False),
619
+ (b'12', (49, 50), False),
620
+ ((49, 50), b'12', False),
621
+ ]:
622
+ with self.subTest(doc=doc, value=value):
623
+ patch = [{'op': 'test', 'path': '', 'value': value}]
624
+ if succeeds:
625
+ self.assertIs(jsonpatch.apply_patch(
626
+ doc, patch, in_place=True), doc)
627
+ else:
628
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
629
+ jsonpatch.apply_patch(doc, patch, in_place=True)
449
630
 
450
631
  def test_test_not_existing(self):
451
632
  obj = {'bar': 'qux'}
@@ -3148,6 +3329,29 @@ class CommandLineTests(ScriptRunner, unittest.TestCase):
3148
3329
  self.assertEqual(jsonpatch.apply_patch(self.read(src), patch),
3149
3330
  self.read(dst))
3150
3331
 
3332
+ def test_jsondiff_utf8_bom_files(self):
3333
+ for path, value in (('a.json', {'a': 1}), ('b.json', {'a': 2})):
3334
+ with open(os.path.join(self.dir, path), 'w', encoding='utf-8-sig') as f:
3335
+ json.dump(value, f)
3336
+ src = os.path.join(self.dir, 'a.json')
3337
+ dst = os.path.join(self.dir, 'b.json')
3338
+ res = self.run_script('jsondiff', src, dst)
3339
+ # like diff, it exits with 1 if the files differ
3340
+ self.assertEqual(res.returncode, 1, res.stderr)
3341
+ patch = json.loads(res.stdout)
3342
+ self.assertEqual(jsonpatch.apply_patch({'a': 1}, patch), {'a': 2})
3343
+
3344
+ def test_jsonpatch_utf8_bom_files(self):
3345
+ doc = os.path.join(self.dir, 'doc.json')
3346
+ patch = os.path.join(self.dir, 'patch.json')
3347
+ with open(doc, 'w', encoding='utf-8-sig') as f:
3348
+ json.dump({'a': 1}, f)
3349
+ with open(patch, 'w', encoding='utf-8-sig') as f:
3350
+ json.dump([{'op': 'replace', 'path': '/a', 'value': 2}], f)
3351
+ res = self.run_script('jsonpatch', doc, patch)
3352
+ self.assertEqual(res.returncode, 0, res.stderr)
3353
+ self.assertEqual(json.loads(res.stdout), {'a': 2})
3354
+
3151
3355
  def test_jsonpatch(self):
3152
3356
  doc = self.write('doc.json', {'a': [1, 2], 'b': 0})
3153
3357
  patch = self.write('patch.json', [{'op': 'add', 'path': '/a/-', 'value': 3},
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
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes