Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_codeop.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | e3184e46303b |
| children | 693155cdae03 aa0d2d51fbde |
| rev | line source |
|---|---|
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
1 """ |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
2 Test cases for codeop.py |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
3 Nick Mathewson |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
4 """ |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
5 import unittest |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
6 from test_support import run_unittest |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
7 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
8 from codeop import compile_command |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
9 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
10 class CodeopTests(unittest.TestCase): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
11 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
12 def assertValid(self, str, symbol='single'): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
13 '''succeed iff str is a valid piece of code''' |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
14 expected = compile(str, "<input>", symbol) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
15 self.assertEquals( compile_command(str, "<input>", symbol), expected) |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18878
diff
changeset
|
16 |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
17 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
18 def assertIncomplete(self, str, symbol='single'): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
19 '''succeed iff str is the start of a valid piece of code''' |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
20 self.assertEquals( compile_command(str, symbol=symbol), None) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
21 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
22 def assertInvalid(self, str, symbol='single', is_syntax=1): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
23 '''succeed iff str is the start of an invalid piece of code''' |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
24 try: |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
25 compile_command(str,symbol=symbol) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
26 self.fail("No exception thrown for invalid code") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
27 except SyntaxError: |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
28 self.assert_(is_syntax) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
29 except OverflowError: |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
30 self.assert_(not is_syntax) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
31 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
32 def test_valid(self): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
33 av = self.assertValid |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
34 av("a = 1\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
35 av("def x():\n pass\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
36 av("pass\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
37 av("3**3\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
38 av("if 9==3:\n pass\nelse:\n pass\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
39 av("#a\n#b\na = 3\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
40 av("#a\n\n \na=3\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
41 av("a=3\n\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
42 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
43 # special case |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18878
diff
changeset
|
44 self.assertEquals(compile_command(""), |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
45 compile("pass", "<input>", 'single')) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
46 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
47 av("3**3","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
48 av("(lambda z: \n z**3)","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
49 av("#a\n#b\na**3","eval") |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18878
diff
changeset
|
50 |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
51 def test_incomplete(self): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
52 ai = self.assertIncomplete |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
53 ai("(a **") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
54 ai("def x():\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
55 ai("(a,b,") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
56 ai("(a,b,(") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
57 ai("(a,b,(") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
58 ai("if 9==3:\n pass\nelse:\n") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
59 ai("if 9==3:\n pass\nelse:\n pass") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
60 ai("a = (") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
61 ai("a = 9+ \\") |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18878
diff
changeset
|
62 |
|
18878
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
63 ai("(","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
64 ai("(\n\n\n","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
65 ai("(9+","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
66 ai("9+ \\","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
67 ai("lambda z: \\","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
68 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
69 def test_invalid(self): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
70 ai = self.assertInvalid |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
71 ai("a b") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
72 ai("a = ") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
73 ai("a = 9 +") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
74 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
75 ai("a = 1","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
76 ai("a = (","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
77 ai("]","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
78 ai("())","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
79 ai("[}","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
80 ai("9+","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
81 ai("lambda z:","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
82 ai("a b","eval") |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
83 |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
84 def test_filename(self): |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
85 self.assertEquals(compile_command("a = 1\n", "abc").co_filename, |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
86 compile("a = 1\n", "abc", 'single').co_filename) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
87 self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename, |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
88 compile("a = 1\n", "def", 'single').co_filename) |
|
4d9ebeedbd04
Patch #442866: Tests for codeop.py.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff
changeset
|
89 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
90 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
91 def test_main(): |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
92 run_unittest(CodeopTests) |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
93 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
94 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
95 if __name__ == "__main__": |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
19119
diff
changeset
|
96 test_main() |
