Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 057bcb4

Browse files
committed
#16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
1 parent d654ded commit 057bcb4

4 files changed

Lines changed: 84 additions & 9 deletions

File tree

Lib/json/tool.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ def main():
1818
infile = sys.stdin
1919
outfile = sys.stdout
2020
elif len(sys.argv) == 2:
21-
infile = open(sys.argv[1], 'rb')
21+
infile = open(sys.argv[1], 'r')
2222
outfile = sys.stdout
2323
elif len(sys.argv) == 3:
24-
infile = open(sys.argv[1], 'rb')
25-
outfile = open(sys.argv[2], 'wb')
24+
infile = open(sys.argv[1], 'r')
25+
outfile = open(sys.argv[2], 'w')
2626
else:
2727
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
28-
try:
29-
obj = json.load(infile)
30-
except ValueError as e:
31-
raise SystemExit(e)
32-
json.dump(obj, outfile, sort_keys=True, indent=4)
33-
outfile.write('\n')
28+
with infile:
29+
try:
30+
obj = json.load(infile)
31+
except ValueError as e:
32+
raise SystemExit(e)
33+
with outfile:
34+
json.dump(obj, outfile, sort_keys=True, indent=4)
35+
outfile.write('\n')
3436

3537

3638
if __name__ == '__main__':

Lib/test/json_tests/test_tool.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
import sys
3+
import textwrap
4+
import unittest
5+
import subprocess
6+
from test import support
7+
from test.script_helper import assert_python_ok
8+
9+
class TestTool(unittest.TestCase):
10+
data = """
11+
12+
[["blorpie"],[ "whoops" ] , [
13+
],\t"d-shtaeou",\r"d-nthiouh",
14+
"i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"
15+
:"yes"} ]
16+
"""
17+
18+
expect = textwrap.dedent("""\
19+
[
20+
[
21+
"blorpie"
22+
],
23+
[
24+
"whoops"
25+
],
26+
[],
27+
"d-shtaeou",
28+
"d-nthiouh",
29+
"i-vhbjkhnth",
30+
{
31+
"nifty": 87
32+
},
33+
{
34+
"field": "yes",
35+
"morefield": false
36+
}
37+
]
38+
""")
39+
40+
def test_stdin_stdout(self):
41+
with subprocess.Popen(
42+
(sys.executable, '-m', 'json.tool'),
43+
stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc:
44+
out, err = proc.communicate(self.data.encode())
45+
self.assertEqual(out, self.expect.encode())
46+
self.assertEqual(err, None)
47+
48+
def _create_infile(self):
49+
infile = support.TESTFN
50+
with open(infile, "w") as fp:
51+
self.addCleanup(os.remove, infile)
52+
fp.write(self.data)
53+
return infile
54+
55+
def test_infile_stdout(self):
56+
infile = self._create_infile()
57+
rc, out, err = assert_python_ok('-m', 'json.tool', infile)
58+
self.assertEqual(out, self.expect.encode())
59+
self.assertEqual(err, b'')
60+
61+
def test_infile_outfile(self):
62+
infile = self._create_infile()
63+
outfile = support.TESTFN + '.out'
64+
rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile)
65+
self.addCleanup(os.remove, outfile)
66+
with open(outfile, "r") as fp:
67+
self.assertEqual(fp.read(), self.expect)
68+
self.assertEqual(out, b'')
69+
self.assertEqual(err, b'')

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ Alexandre Parenteau
807807
Dan Parisien
808808
William Park
809809
Harri Pasanen
810+
Berker Peksag
810811
Bo Peng
811812
Joe Peterson
812813
Randy Pausch

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ Core and Builtins
167167
Library
168168
-------
169169

170+
- Issue #16549: Make json.tool work again on Python 3 and add tests.
171+
Initial patch by Berker Peksag and Serhiy Storchaka.
172+
170173
- Issue #12848: The pure Python pickle implementation now treats object
171174
lengths as unsigned 32-bit integers, like the C implementation does.
172175
Patch by Serhiy Storchaka.

0 commit comments

Comments
 (0)