Skip to content

Commit 72e81d8

Browse files
authored
Merge pull request #133 from boogu2005/fix/cli-utf8-piped-input
2 parents e1fa1ba + 9ec4f97 commit 72e81d8

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎bin/jsonpointer‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ def open_input(name):
5757
parser.error("can't open '%s': %s" % (name, e))
5858

5959

60+
def read_input(stream):
61+
"""Decode redirected stdin using the same encoding as named files."""
62+
if stream is sys.stdin and not stream.isatty() and hasattr(stream, 'buffer'):
63+
return stream.buffer.read().decode('utf-8')
64+
return stream.read()
65+
66+
6067
def parse_pointer(args):
6168
""" Return the parsed pointer and the list of files to resolve it on
6269
@@ -75,7 +82,7 @@ def parse_pointer(args):
7582
# part of a member name.
7683
pointer_file = open_input(args.pointer_file)
7784
try:
78-
ptr = pointer_file.read().rstrip("\r\n")
85+
ptr = read_input(pointer_file).rstrip("\r\n")
7986
except UnicodeDecodeError as e:
8087
parser.error('could not decode pointer file: %s' % e)
8188
finally:
@@ -112,7 +119,7 @@ def resolve_files():
112119
f = open_input(name)
113120

114121
try:
115-
doc = json.load(f)
122+
doc = json.loads(read_input(f))
116123
except (json.JSONDecodeError, UnicodeDecodeError) as e:
117124
if not args.continue_on_error:
118125
raise

‎doc/commandline.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ The pointer can be passed as the first positional argument, with ``-p``, or
3232
read from a file with ``-f``.
3333

3434
JSON input files and pointer files are read as UTF-8.
35+
Use ``-`` to read a JSON document or a pointer from standard input.
36+
Piped and redirected standard input is also read as UTF-8.
3537

3638
The exit status is 0 if the pointer could be resolved on all files, 1 if it
3739
could not be resolved on at least one file (the remaining files are still

‎tests.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ def test_keyboard_interrupt_exit_status(self):
816816
cli = runpy.run_path(self.SCRIPT)
817817
args = [self.SCRIPT, '/a', os.path.join(self.dir, 'a.json')]
818818
with mock.patch.object(sys, 'argv', args), \
819-
mock.patch('json.load', side_effect=KeyboardInterrupt):
819+
mock.patch('json.loads', side_effect=KeyboardInterrupt):
820820
with self.assertRaises(SystemExit) as caught:
821821
cli['main']()
822822
self.assertEqual(caught.exception.code, 130)
@@ -836,6 +836,20 @@ def test_utf8_pointer_file_without_utf8_mode(self):
836836
self.assertEqual(json.loads(proc.stdout), 'expected')
837837
self.assertEqual(proc.stderr, '')
838838

839+
def test_utf8_piped_input_with_legacy_stdio_encoding(self):
840+
self._write('unicode.json', json.dumps({'ä': 'expected', 'ä': 'wrong'}))
841+
env = dict(os.environ, PYTHONPATH=self.ROOT, PYTHONUTF8='0',
842+
PYTHONIOENCODING='cp1252')
843+
for args, stdin in ((['/ä', '-'], '{"ä": "expected"}'),
844+
(['-f', '-', 'unicode.json'], '/ä\n')):
845+
with self.subTest(args=args):
846+
proc = subprocess.run([sys.executable, self.SCRIPT] + args,
847+
cwd=self.dir, env=env, capture_output=True,
848+
input=stdin.encode('utf-8'))
849+
self.assertEqual(proc.returncode, 0, proc.stderr)
850+
self.assertEqual(json.loads(proc.stdout), 'expected')
851+
self.assertEqual(proc.stderr, b'')
852+
839853
def test_positional_pointer(self):
840854
proc = self._run('/a', 'a.json', 'b.json')
841855
self.assertEqual(proc.returncode, 0, proc.stderr)

0 commit comments

Comments
 (0)