[Python-checkins] bpo-33684: json.tool: Use utf-8 for infile and outfile. (GH-17460)

Inada Naoki webhook-mailer at python.org
Wed Dec 4 04:39:37 EST 2019


https://github.com/python/cpython/commit/808769f3a4cbdc47cf1a5708dd61b1787bb192d4
commit: 808769f3a4cbdc47cf1a5708dd61b1787bb192d4
branch: master
author: Inada Naoki <songofacandy at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-12-04T18:39:31+09:00
summary:

bpo-33684: json.tool: Use utf-8 for infile and outfile. (GH-17460)

files:
A Misc/NEWS.d/next/Library/2019-12-04-15-28-40.bpo-33684.QeSmQP.rst
M Lib/json/tool.py
M Lib/test/test_json/test_tool.py

diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index 6c687d77c511c..2a404a4441796 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -20,10 +20,12 @@ def main():
     description = ('A simple command line interface for json module '
                    'to validate and pretty-print JSON objects.')
     parser = argparse.ArgumentParser(prog=prog, description=description)
-    parser.add_argument('infile', nargs='?', type=argparse.FileType(),
+    parser.add_argument('infile', nargs='?',
+                        type=argparse.FileType(encoding="utf-8"),
                         help='a JSON file to be validated or pretty-printed',
                         default=sys.stdin)
-    parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
+    parser.add_argument('outfile', nargs='?',
+                        type=argparse.FileType('w', encoding="utf-8"),
                         help='write the output of infile to outfile',
                         default=sys.stdout)
     parser.add_argument('--sort-keys', action='store_true', default=False,
diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py
index 81d179c6d10b0..953a5696e7c22 100644
--- a/Lib/test/test_json/test_tool.py
+++ b/Lib/test/test_json/test_tool.py
@@ -89,11 +89,11 @@ def test_stdin_stdout(self):
         self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
         self.assertEqual(err, b'')
 
-    def _create_infile(self):
+    def _create_infile(self, data=None):
         infile = support.TESTFN
-        with open(infile, "w") as fp:
+        with open(infile, "w", encoding="utf-8") as fp:
             self.addCleanup(os.remove, infile)
-            fp.write(self.data)
+            fp.write(data or self.data)
         return infile
 
     def test_infile_stdout(self):
@@ -103,6 +103,21 @@ def test_infile_stdout(self):
         self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
         self.assertEqual(err, b'')
 
+    def test_non_ascii_infile(self):
+        data = '{"msg": "\u3053\u3093\u306b\u3061\u306f"}'
+        expect = textwrap.dedent('''\
+        {
+            "msg": "\\u3053\\u3093\\u306b\\u3061\\u306f"
+        }
+        ''').encode()
+
+        infile = self._create_infile(data)
+        rc, out, err = assert_python_ok('-m', 'json.tool', infile)
+
+        self.assertEqual(rc, 0)
+        self.assertEqual(out.splitlines(), expect.splitlines())
+        self.assertEqual(err, b'')
+
     def test_infile_outfile(self):
         infile = self._create_infile()
         outfile = support.TESTFN + '.out'
diff --git a/Misc/NEWS.d/next/Library/2019-12-04-15-28-40.bpo-33684.QeSmQP.rst b/Misc/NEWS.d/next/Library/2019-12-04-15-28-40.bpo-33684.QeSmQP.rst
new file mode 100644
index 0000000000000..107f9bb008330
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-04-15-28-40.bpo-33684.QeSmQP.rst
@@ -0,0 +1,2 @@
+Fix ``json.tool`` failed to read a JSON file with non-ASCII characters when
+locale encoding is not UTF-8.



More information about the Python-checkins mailing list