jsonpointer 3.5__tar.gz → 3.6__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.5
3
+ Version: 3.6
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
@@ -58,7 +58,11 @@ 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
- ptr = args.pointer_file.read().rstrip("\r\n")
61
+ try:
62
+ ptr = args.pointer_file.read().rstrip("\r\n")
63
+ finally:
64
+ if args.pointer_file is not sys.stdin:
65
+ args.pointer_file.close()
62
66
 
63
67
  elif args.POINTER is not None:
64
68
  ptr = args.POINTER
@@ -101,6 +105,9 @@ def resolve_files():
101
105
  file=sys.stderr)
102
106
  ok = False
103
107
  continue
108
+ finally:
109
+ if f is not sys.stdin:
110
+ f.close()
104
111
 
105
112
  try:
106
113
  result = jsonpointer.resolve_pointer(doc, ptr)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: jsonpointer
3
- Version: 3.5
3
+ Version: 3.6
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.5'
35
+ __version__ = '3.6'
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
- """ Returns True if self contains the given ptr """
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
- """ Returns True if self contains the given ptr """
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,65 @@ 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_inputs_after_json_errors(self):
626
+ self._write('invalid.json', '{"a": }')
627
+ with self.assertRaises(json.JSONDecodeError):
628
+ self._run_with_retained_files('/a', os.path.join(self.dir, 'invalid.json'))
629
+ self.assertFalse(self._run_with_retained_files(
630
+ '--continue-on-error', '/a', os.path.join(self.dir, 'invalid.json'),
631
+ os.path.join(self.dir, 'a.json')))
632
+
633
+ def test_keeps_stdin_open(self):
634
+ cli = runpy.run_path(self.SCRIPT)
635
+ for args, content in ((['/a', '-'], '{"a": 1}'),
636
+ (['-f', '-', os.path.join(self.dir, 'a.json')], '/a\n')):
637
+ with self.subTest(args=args):
638
+ stdin = io.StringIO(content)
639
+ self.addCleanup(stdin.close)
640
+ with mock.patch.object(sys, 'argv', [self.SCRIPT] + args), \
641
+ mock.patch.object(sys, 'stdin', stdin), \
642
+ redirect_stdout(io.StringIO()):
643
+ self.assertTrue(cli['resolve_files']())
644
+ self.assertFalse(stdin.closed)
645
+
583
646
  def test_utf8_json_file_without_utf8_mode(self):
584
647
  self._write('unicode.json', '{"value": "🚀"}')
585
648
  proc = self._run('/value', 'unicode.json', utf8=False)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes