diff --git a/CHANGES.txt b/CHANGES.txt index 4d9b1fc3c56a8dda8b8a7d9d67b6f3159acdd98b_Q0hBTkdFUy50eHQ=..02f006dbc070f4be9327f43a25b8b2d838c87161_Q0hBTkdFUy50eHQ= 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,7 @@ Version 3.1.0 released XXXX-XX-XX +* simplejson.tool tests and bugfix for Python 3.x + http://bugs.python.org/issue16549 * Improve error messages for certain kinds of truncated input http://bugs.python.org/issue16009 * Moved JSONDecodeError to json.scanner (still available for import diff --git a/simplejson/tests/__init__.py b/simplejson/tests/__init__.py index 4d9b1fc3c56a8dda8b8a7d9d67b6f3159acdd98b_c2ltcGxlanNvbi90ZXN0cy9fX2luaXRfXy5weQ==..02f006dbc070f4be9327f43a25b8b2d838c87161_c2ltcGxlanNvbi90ZXN0cy9fX2luaXRfXy5weQ== 100644 --- a/simplejson/tests/__init__.py +++ b/simplejson/tests/__init__.py @@ -59,6 +59,7 @@ 'simplejson.tests.test_decimal', 'simplejson.tests.test_tuple', 'simplejson.tests.test_namedtuple', + 'simplejson.tests.test_tool', ]) suite = additional_tests(suite) return OptionalExtensionTestSuite([suite]) diff --git a/simplejson/tests/test_tool.py b/simplejson/tests/test_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..02f006dbc070f4be9327f43a25b8b2d838c87161_c2ltcGxlanNvbi90ZXN0cy90ZXN0X3Rvb2wucHk= --- /dev/null +++ b/simplejson/tests/test_tool.py @@ -0,0 +1,80 @@ +import os +import sys +import textwrap +import unittest +import subprocess +import tempfile + +class TestTool(unittest.TestCase): + data = """ + + [["blorpie"],[ "whoops" ] , [ + ],\t"d-shtaeou",\r"d-nthiouh", + "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field" + :"yes"} ] + """ + + expect = textwrap.dedent("""\ + [ + [ + "blorpie" + ], + [ + "whoops" + ], + [], + "d-shtaeou", + "d-nthiouh", + "i-vhbjkhnth", + { + "nifty": 87 + }, + { + "field": "yes", + "morefield": false + } + ] + """) + + def runTool(self, args=None, data=None): + argv = [sys.executable, '-m', 'simplejson.tool'] + if args: + argv.extend(args) + proc = subprocess.Popen(argv, + stdin=subprocess.PIPE, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE) + out, err = proc.communicate(data) + self.assertEqual(len(err), 0) + self.assertEqual(proc.returncode, 0) + return out + + def test_stdin_stdout(self): + self.assertEqual( + self.runTool(data=self.data.encode()), + self.expect.encode()) + + def test_infile_stdout(self): + with tempfile.NamedTemporaryFile() as infile: + infile.write(self.data.encode()) + infile.flush() + self.assertEqual( + self.runTool(args=[infile.name]), + self.expect.encode()) + + def test_infile_outfile(self): + with tempfile.NamedTemporaryFile() as infile: + infile.write(self.data.encode()) + infile.flush() + # outfile will get overwritten by tool, so the delete + # may not work on some platforms. Do it manually. + outfile = tempfile.NamedTemporaryFile(delete=0) + try: + self.assertEqual( + self.runTool(args=[infile.name, outfile.name]), + ''.encode()) + with open(outfile.name, 'rb') as f: + self.assertEqual(f.read(), self.expect.encode()) + finally: + outfile.close() + os.unlink(outfile.name) diff --git a/simplejson/tool.py b/simplejson/tool.py index 4d9b1fc3c56a8dda8b8a7d9d67b6f3159acdd98b_c2ltcGxlanNvbi90b29sLnB5..02f006dbc070f4be9327f43a25b8b2d838c87161_c2ltcGxlanNvbi90b29sLnB5 100644 --- a/simplejson/tool.py +++ b/simplejson/tool.py @@ -18,6 +18,6 @@ infile = sys.stdin outfile = sys.stdout elif len(sys.argv) == 2: - infile = open(sys.argv[1], 'rb') + infile = open(sys.argv[1], 'r') outfile = sys.stdout elif len(sys.argv) == 3: @@ -22,6 +22,6 @@ outfile = sys.stdout elif len(sys.argv) == 3: - infile = open(sys.argv[1], 'rb') - outfile = open(sys.argv[2], 'wb') + infile = open(sys.argv[1], 'r') + outfile = open(sys.argv[2], 'w') else: raise SystemExit(sys.argv[0] + " [infile [outfile]]") @@ -26,13 +26,15 @@ else: raise SystemExit(sys.argv[0] + " [infile [outfile]]") - try: - obj = json.load(infile, - object_pairs_hook=json.OrderedDict, - use_decimal=True) - except ValueError: - raise SystemExit(sys.exc_info()[1]) - json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True) - outfile.write('\n') + with infile: + try: + obj = json.load(infile, + object_pairs_hook=json.OrderedDict, + use_decimal=True) + except ValueError: + raise SystemExit(sys.exc_info()[1]) + with outfile: + json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True) + outfile.write('\n') if __name__ == '__main__':