| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,19 +18,21 @@ def main(): | |||
| 18 | 18 | infile = sys.stdin | |
| 19 | 19 | outfile = sys.stdout | |
| 20 | 20 | elif len(sys.argv) == 2: | |
| 21 | - infile = open(sys.argv[1], 'rb') | ||
| 21 | + infile = open(sys.argv[1], 'r') | ||
| 22 | 22 | outfile = sys.stdout | |
| 23 | 23 | 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') | ||
| 26 | 26 | else: | |
| 27 | 27 | 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') | ||
| 34 | 36 | ||
| 35 | 37 | ||
| 36 | 38 | if __name__ == '__main__': | |
| Original file line number | Diff line number | Diff 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'') | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -807,6 +807,7 @@ Alexandre Parenteau | |||
| 807 | 807 | Dan Parisien | |
| 808 | 808 | William Park | |
| 809 | 809 | Harri Pasanen | |
| 810 | + Berker Peksag | ||
| 810 | 811 | Bo Peng | |
| 811 | 812 | Joe Peterson | |
| 812 | 813 | Randy Pausch | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -167,6 +167,9 @@ Core and Builtins | |||
| 167 | 167 | Library | |
| 168 | 168 | ------- | |
| 169 | 169 | ||
| 170 | + - Issue #16549: Make json.tool work again on Python 3 and add tests. | ||
| 171 | + Initial patch by Berker Peksag and Serhiy Storchaka. | ||
| 172 | + | ||
| 170 | 173 | - Issue #12848: The pure Python pickle implementation now treats object | |
| 171 | 174 | lengths as unsigned 32-bit integers, like the C implementation does. | |
| 172 | 175 | Patch by Serhiy Storchaka. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments