From 9ec4f97b81d64cf43afe71e1f1b7676d7b830821 Mon Sep 17 00:00:00 2001 From: BooGu <144680841+boogu2005@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:33:04 +0800 Subject: [PATCH] Read piped CLI input as UTF-8 --- bin/jsonpointer | 11 +++++++++-- doc/commandline.rst | 2 ++ tests.py | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/bin/jsonpointer b/bin/jsonpointer index 9e0efb2..8765fe3 100755 --- a/bin/jsonpointer +++ b/bin/jsonpointer @@ -57,6 +57,13 @@ def open_input(name): parser.error("can't open '%s': %s" % (name, e)) +def read_input(stream): + """Decode redirected stdin using the same encoding as named files.""" + if stream is sys.stdin and not stream.isatty() and hasattr(stream, 'buffer'): + return stream.buffer.read().decode('utf-8') + return stream.read() + + def parse_pointer(args): """ Return the parsed pointer and the list of files to resolve it on @@ -75,7 +82,7 @@ def parse_pointer(args): # part of a member name. pointer_file = open_input(args.pointer_file) try: - ptr = pointer_file.read().rstrip("\r\n") + ptr = read_input(pointer_file).rstrip("\r\n") except UnicodeDecodeError as e: parser.error('could not decode pointer file: %s' % e) finally: @@ -112,7 +119,7 @@ def resolve_files(): f = open_input(name) try: - doc = json.load(f) + doc = json.loads(read_input(f)) except (json.JSONDecodeError, UnicodeDecodeError) as e: if not args.continue_on_error: raise diff --git a/doc/commandline.rst b/doc/commandline.rst index e170d02..5b18039 100644 --- a/doc/commandline.rst +++ b/doc/commandline.rst @@ -32,6 +32,8 @@ The pointer can be passed as the first positional argument, with ``-p``, or read from a file with ``-f``. JSON input files and pointer files are read as UTF-8. +Use ``-`` to read a JSON document or a pointer from standard input. +Piped and redirected standard input is also read as UTF-8. The exit status is 0 if the pointer could be resolved on all files, 1 if it could not be resolved on at least one file (the remaining files are still diff --git a/tests.py b/tests.py index a2fc2d3..bb587f3 100755 --- a/tests.py +++ b/tests.py @@ -816,7 +816,7 @@ def test_keyboard_interrupt_exit_status(self): cli = runpy.run_path(self.SCRIPT) args = [self.SCRIPT, '/a', os.path.join(self.dir, 'a.json')] with mock.patch.object(sys, 'argv', args), \ - mock.patch('json.load', side_effect=KeyboardInterrupt): + mock.patch('json.loads', side_effect=KeyboardInterrupt): with self.assertRaises(SystemExit) as caught: cli['main']() self.assertEqual(caught.exception.code, 130) @@ -836,6 +836,20 @@ def test_utf8_pointer_file_without_utf8_mode(self): self.assertEqual(json.loads(proc.stdout), 'expected') self.assertEqual(proc.stderr, '') + def test_utf8_piped_input_with_legacy_stdio_encoding(self): + self._write('unicode.json', json.dumps({'ä': 'expected', 'ä': 'wrong'})) + env = dict(os.environ, PYTHONPATH=self.ROOT, PYTHONUTF8='0', + PYTHONIOENCODING='cp1252') + for args, stdin in ((['/ä', '-'], '{"ä": "expected"}'), + (['-f', '-', 'unicode.json'], '/ä\n')): + with self.subTest(args=args): + proc = subprocess.run([sys.executable, self.SCRIPT] + args, + cwd=self.dir, env=env, capture_output=True, + input=stdin.encode('utf-8')) + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertEqual(json.loads(proc.stdout), 'expected') + self.assertEqual(proc.stderr, b'') + def test_positional_pointer(self): proc = self._run('/a', 'a.json', 'b.json') self.assertEqual(proc.returncode, 0, proc.stderr)