annotate Lib/test/test_json/test_tool.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 95cf8640b271
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
80636
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
1 import os
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
2 import sys
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
3 import textwrap
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
4 import unittest
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
5 import subprocess
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
6 from test import support
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
7 from test.script_helper import assert_python_ok
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
8
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
9 class TestTool(unittest.TestCase):
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
10 data = """
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
11
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
12 [["blorpie"],[ "whoops" ] , [
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
13 ],\t"d-shtaeou",\r"d-nthiouh",
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
14 "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
15 :"yes"} ]
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
16 """
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
17
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
18 expect = textwrap.dedent("""\
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
19 [
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
20 [
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
21 "blorpie"
80640
6996a53f13ce #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80636
diff changeset
22 ],
80636
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
23 [
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
24 "whoops"
80640
6996a53f13ce #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80636
diff changeset
25 ],
6996a53f13ce #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80636
diff changeset
26 [],
6996a53f13ce #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80636
diff changeset
27 "d-shtaeou",
6996a53f13ce #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80636
diff changeset
28 "d-nthiouh",
6996a53f13ce #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80636
diff changeset
29 "i-vhbjkhnth",
80636
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
30 {
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
31 "nifty": 87
80640
6996a53f13ce #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80636
diff changeset
32 },
80636
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
33 {
80640
6996a53f13ce #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80636
diff changeset
34 "field": "yes",
80636
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
35 "morefield": false
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
36 }
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
37 ]
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
38 """)
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
39
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
40 def test_stdin_stdout(self):
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
41 with subprocess.Popen(
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
42 (sys.executable, '-m', 'json.tool'),
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
43 stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc:
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
44 out, err = proc.communicate(self.data.encode())
80645
4ec9f0b0f8f1 #16549: fix test failures on Windows.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80640
diff changeset
45 self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
80636
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
46 self.assertEqual(err, None)
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
47
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
48 def _create_infile(self):
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
49 infile = support.TESTFN
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
50 with open(infile, "w") as fp:
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
51 self.addCleanup(os.remove, infile)
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
52 fp.write(self.data)
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
53 return infile
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
54
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
55 def test_infile_stdout(self):
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
56 infile = self._create_infile()
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
57 rc, out, err = assert_python_ok('-m', 'json.tool', infile)
80645
4ec9f0b0f8f1 #16549: fix test failures on Windows.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80640
diff changeset
58 self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
80636
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
59 self.assertEqual(err, b'')
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
60
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
61 def test_infile_outfile(self):
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
62 infile = self._create_infile()
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
63 outfile = support.TESTFN + '.out'
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
64 rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile)
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
65 self.addCleanup(os.remove, outfile)
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
66 with open(outfile, "r") as fp:
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
67 self.assertEqual(fp.read(), self.expect)
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
68 self.assertEqual(out, b'')
44c544809e6c #16549: Make json.tool work again on Python 3 and add tests. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
69 self.assertEqual(err, b'')