jsonpointer 3.5__tar.gz → 3.7__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.
- {jsonpointer-3.5/jsonpointer.egg-info → jsonpointer-3.7}/PKG-INFO +1 -1
- {jsonpointer-3.5 → jsonpointer-3.7}/bin/jsonpointer +10 -1
- {jsonpointer-3.5 → jsonpointer-3.7/jsonpointer.egg-info}/PKG-INFO +1 -1
- {jsonpointer-3.5 → jsonpointer-3.7}/jsonpointer.py +26 -3
- {jsonpointer-3.5 → jsonpointer-3.7}/setup.cfg +1 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/tests.py +100 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/AUTHORS +0 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/LICENSE.txt +0 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/MANIFEST.in +0 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/README.md +0 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/jsonpointer.egg-info/SOURCES.txt +0 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/jsonpointer.egg-info/dependency_links.txt +0 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/jsonpointer.egg-info/top_level.txt +0 -0
- {jsonpointer-3.5 → jsonpointer-3.7}/setup.py +0 -0
|
@@ -58,7 +58,13 @@ def parse_pointer(args):
|
|
|
58
58
|
ptr = args.pointer
|
|
59
59
|
else:
|
|
60
60
|
# 2026-10-11: Trim file line endings without changing member names.
|
|
61
|
-
|
|
61
|
+
try:
|
|
62
|
+
ptr = args.pointer_file.read().rstrip("\r\n")
|
|
63
|
+
except UnicodeDecodeError as e:
|
|
64
|
+
parser.error('could not decode pointer file: %s' % e)
|
|
65
|
+
finally:
|
|
66
|
+
if args.pointer_file is not sys.stdin:
|
|
67
|
+
args.pointer_file.close()
|
|
62
68
|
|
|
63
69
|
elif args.POINTER is not None:
|
|
64
70
|
ptr = args.POINTER
|
|
@@ -101,6 +107,9 @@ def resolve_files():
|
|
|
101
107
|
file=sys.stderr)
|
|
102
108
|
ok = False
|
|
103
109
|
continue
|
|
110
|
+
finally:
|
|
111
|
+
if f is not sys.stdin:
|
|
112
|
+
f.close()
|
|
104
113
|
|
|
105
114
|
try:
|
|
106
115
|
result = jsonpointer.resolve_pointer(doc, ptr)
|
|
@@ -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.
|
|
35
|
+
__version__ = '3.7'
|
|
36
36
|
__website__ = 'https://github.com/stefankoegl/python-json-pointer'
|
|
37
37
|
__license__ = 'Modified BSD License'
|
|
38
38
|
|
|
@@ -349,11 +349,34 @@ class JsonPointer:
|
|
|
349
349
|
raise JsonPointerException("member '%s' not found" % (part,))
|
|
350
350
|
|
|
351
351
|
def contains(self, ptr):
|
|
352
|
-
"""
|
|
352
|
+
"""Returns True if ptr's parts are a prefix of this pointer's parts.
|
|
353
|
+
|
|
354
|
+
Equal pointers and the root pointer are also prefixes. For example,
|
|
355
|
+
the longer pointer calls contains() with its parent:
|
|
356
|
+
|
|
357
|
+
>>> child = JsonPointer('/foo/bar')
|
|
358
|
+
>>> parent = JsonPointer('/foo')
|
|
359
|
+
>>> child.contains(parent)
|
|
360
|
+
True
|
|
361
|
+
>>> parent.contains(child)
|
|
362
|
+
False
|
|
363
|
+
>>> child.contains(child)
|
|
364
|
+
True
|
|
365
|
+
>>> child.contains(JsonPointer(''))
|
|
366
|
+
True
|
|
367
|
+
>>> child.contains(JsonPointer('/foo/b'))
|
|
368
|
+
False
|
|
369
|
+
"""
|
|
353
370
|
return self.parts[:len(ptr.parts)] == ptr.parts
|
|
354
371
|
|
|
355
372
|
def __contains__(self, item):
|
|
356
|
-
"""
|
|
373
|
+
"""Equivalent to contains(): item must be a prefix of self.
|
|
374
|
+
|
|
375
|
+
>>> JsonPointer('/foo') in JsonPointer('/foo/bar')
|
|
376
|
+
True
|
|
377
|
+
>>> JsonPointer('/foo/bar') in JsonPointer('/foo')
|
|
378
|
+
False
|
|
379
|
+
"""
|
|
357
380
|
return self.contains(item)
|
|
358
381
|
|
|
359
382
|
def join(self, suffix):
|
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
import copy
|
|
4
4
|
import doctest
|
|
5
|
+
import io
|
|
5
6
|
import json
|
|
6
7
|
import os
|
|
8
|
+
import runpy
|
|
7
9
|
import subprocess
|
|
8
10
|
import sys
|
|
9
11
|
import tempfile
|
|
10
12
|
import unittest
|
|
13
|
+
from contextlib import redirect_stderr, redirect_stdout
|
|
14
|
+
from unittest import mock
|
|
11
15
|
|
|
12
16
|
import jsonpointer
|
|
13
17
|
from jsonpointer import resolve_pointer, EndOfList, JsonPointerException, \
|
|
@@ -580,6 +584,89 @@ class CommandLineTests(unittest.TestCase):
|
|
|
580
584
|
cwd=self.dir, env=env, capture_output=True,
|
|
581
585
|
encoding='utf-8')
|
|
582
586
|
|
|
587
|
+
def _run_with_retained_files(self, *args):
|
|
588
|
+
cli = runpy.run_path(self.SCRIPT)
|
|
589
|
+
opened = []
|
|
590
|
+
original_open = open
|
|
591
|
+
|
|
592
|
+
def retain_file(*args, **kwargs):
|
|
593
|
+
file_obj = original_open(*args, **kwargs)
|
|
594
|
+
opened.append(file_obj)
|
|
595
|
+
self.addCleanup(file_obj.close)
|
|
596
|
+
return file_obj
|
|
597
|
+
|
|
598
|
+
with mock.patch.object(sys, 'argv', [self.SCRIPT] + list(args)), \
|
|
599
|
+
mock.patch('builtins.open', side_effect=retain_file), \
|
|
600
|
+
redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()):
|
|
601
|
+
try:
|
|
602
|
+
return cli['resolve_files']()
|
|
603
|
+
finally:
|
|
604
|
+
self.assertTrue(opened)
|
|
605
|
+
self.assertTrue(all(file_obj.closed for file_obj in opened))
|
|
606
|
+
|
|
607
|
+
def test_closes_json_inputs(self):
|
|
608
|
+
self.assertTrue(self._run_with_retained_files(
|
|
609
|
+
'/a', os.path.join(self.dir, 'a.json'),
|
|
610
|
+
os.path.join(self.dir, 'b.json')))
|
|
611
|
+
|
|
612
|
+
def test_closes_pointer_file(self):
|
|
613
|
+
self.assertTrue(self._run_with_retained_files(
|
|
614
|
+
'-f', os.path.join(self.dir, 'ptr.txt'),
|
|
615
|
+
os.path.join(self.dir, 'a.json')))
|
|
616
|
+
|
|
617
|
+
def test_closes_invalid_pointer_file(self):
|
|
618
|
+
self._write('invalid-pointer.txt', 'missing-leading-slash')
|
|
619
|
+
with self.assertRaises(SystemExit) as caught:
|
|
620
|
+
self._run_with_retained_files(
|
|
621
|
+
'-f', os.path.join(self.dir, 'invalid-pointer.txt'),
|
|
622
|
+
os.path.join(self.dir, 'a.json'))
|
|
623
|
+
self.assertEqual(caught.exception.code, 2)
|
|
624
|
+
|
|
625
|
+
def test_closes_pointer_file_after_decoding_error(self):
|
|
626
|
+
path = os.path.join(self.dir, 'invalid-pointer.txt')
|
|
627
|
+
with open(path, 'wb') as f:
|
|
628
|
+
f.write(b'/\xff')
|
|
629
|
+
with self.assertRaises(SystemExit) as caught:
|
|
630
|
+
self._run_with_retained_files(
|
|
631
|
+
'-f', path, os.path.join(self.dir, 'a.json'))
|
|
632
|
+
self.assertEqual(caught.exception.code, 2)
|
|
633
|
+
|
|
634
|
+
def test_pointer_decoding_error_keeps_stdin_open(self):
|
|
635
|
+
cli = runpy.run_path(self.SCRIPT)
|
|
636
|
+
stdin = io.TextIOWrapper(io.BytesIO(b'/\xff'), encoding='utf-8')
|
|
637
|
+
self.addCleanup(stdin.close)
|
|
638
|
+
args = [self.SCRIPT, '-f', '-', os.path.join(self.dir, 'a.json')]
|
|
639
|
+
with mock.patch.object(sys, 'argv', args), \
|
|
640
|
+
mock.patch.object(sys, 'stdin', stdin), \
|
|
641
|
+
mock.patch('builtins.open') as open_file, \
|
|
642
|
+
redirect_stderr(io.StringIO()):
|
|
643
|
+
with self.assertRaises(SystemExit) as caught:
|
|
644
|
+
cli['resolve_files']()
|
|
645
|
+
self.assertEqual(caught.exception.code, 2)
|
|
646
|
+
open_file.assert_not_called()
|
|
647
|
+
self.assertFalse(stdin.closed)
|
|
648
|
+
|
|
649
|
+
def test_closes_inputs_after_json_errors(self):
|
|
650
|
+
self._write('invalid.json', '{"a": }')
|
|
651
|
+
with self.assertRaises(json.JSONDecodeError):
|
|
652
|
+
self._run_with_retained_files('/a', os.path.join(self.dir, 'invalid.json'))
|
|
653
|
+
self.assertFalse(self._run_with_retained_files(
|
|
654
|
+
'--continue-on-error', '/a', os.path.join(self.dir, 'invalid.json'),
|
|
655
|
+
os.path.join(self.dir, 'a.json')))
|
|
656
|
+
|
|
657
|
+
def test_keeps_stdin_open(self):
|
|
658
|
+
cli = runpy.run_path(self.SCRIPT)
|
|
659
|
+
for args, content in ((['/a', '-'], '{"a": 1}'),
|
|
660
|
+
(['-f', '-', os.path.join(self.dir, 'a.json')], '/a\n')):
|
|
661
|
+
with self.subTest(args=args):
|
|
662
|
+
stdin = io.StringIO(content)
|
|
663
|
+
self.addCleanup(stdin.close)
|
|
664
|
+
with mock.patch.object(sys, 'argv', [self.SCRIPT] + args), \
|
|
665
|
+
mock.patch.object(sys, 'stdin', stdin), \
|
|
666
|
+
redirect_stdout(io.StringIO()):
|
|
667
|
+
self.assertTrue(cli['resolve_files']())
|
|
668
|
+
self.assertFalse(stdin.closed)
|
|
669
|
+
|
|
583
670
|
def test_utf8_json_file_without_utf8_mode(self):
|
|
584
671
|
self._write('unicode.json', '{"value": "🚀"}')
|
|
585
672
|
proc = self._run('/value', 'unicode.json', utf8=False)
|
|
@@ -648,6 +735,19 @@ class CommandLineTests(unittest.TestCase):
|
|
|
648
735
|
self.assertEqual(proc.returncode, 2)
|
|
649
736
|
self.assertIn("invalid JSON pointer 'a'", proc.stderr)
|
|
650
737
|
|
|
738
|
+
def test_invalid_pointer_file_encoding(self):
|
|
739
|
+
with open(os.path.join(self.dir, 'bad.txt'), 'wb') as f:
|
|
740
|
+
f.write(b'/\xff')
|
|
741
|
+
for options in ([], ['--continue-on-error']):
|
|
742
|
+
with self.subTest(options=options):
|
|
743
|
+
proc = self._run(*options, '-f', 'bad.txt',
|
|
744
|
+
'a.json', 'b.json')
|
|
745
|
+
self.assertEqual(proc.returncode, 2, proc.stderr)
|
|
746
|
+
self.assertEqual(proc.stdout, '')
|
|
747
|
+
self.assertIn('could not decode pointer file:', proc.stderr)
|
|
748
|
+
self.assertIn('decode', proc.stderr)
|
|
749
|
+
self.assertNotIn('Traceback', proc.stderr)
|
|
750
|
+
|
|
651
751
|
def test_empty_pointer(self):
|
|
652
752
|
proc = self._run('', 'a.json')
|
|
653
753
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|