Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_global.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 3748bca4d869 |
| children |
| rev | line source |
|---|---|
|
16925
faa7aaeb0210
test_global was broken by some recent checkin. Repairing.
Tim Peters <tim.peters@gmail.com>
parents:
16874
diff
changeset
|
1 """Verify that warnings are issued for global statements following use.""" |
|
16822
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
2 |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
3 from test.test_support import run_unittest, check_syntax_error |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
4 import unittest |
|
60116
3748bca4d869
Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
40217
diff
changeset
|
5 import warnings |
|
16822
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
6 |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
7 |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
8 class GlobalTests(unittest.TestCase): |
|
16822
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
9 |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
10 def test1(self): |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
11 prog_text_1 = """\ |
|
16822
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
12 def wrong1(): |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
13 a = 1 |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
14 b = 2 |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
15 global a |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
16 global b |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
17 """ |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
18 check_syntax_error(self, prog_text_1) |
|
16822
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
19 |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
20 def test2(self): |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
21 prog_text_2 = """\ |
|
16822
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
22 def wrong2(): |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
23 print x |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
24 global x |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
25 """ |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
26 check_syntax_error(self, prog_text_2) |
|
16822
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
27 |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
28 def test3(self): |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
29 prog_text_3 = """\ |
|
16822
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
30 def wrong3(): |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
31 print x |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
32 x = 2 |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
33 global x |
|
0f0efc2d12f1
verify that warnings are issued for bogus uses of global
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
34 """ |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
35 check_syntax_error(self, prog_text_3) |
|
16874
79bbf7ad0fc1
Add test case for global stmt at module level.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16861
diff
changeset
|
36 |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
37 def test4(self): |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
38 prog_text_4 = """\ |
|
16874
79bbf7ad0fc1
Add test case for global stmt at module level.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16861
diff
changeset
|
39 global x |
|
79bbf7ad0fc1
Add test case for global stmt at module level.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16861
diff
changeset
|
40 x = 2 |
|
79bbf7ad0fc1
Add test case for global stmt at module level.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16861
diff
changeset
|
41 """ |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
42 # this should work |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
43 compile(prog_text_4, "<test string>", "exec") |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
44 |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
45 |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
46 def test_main(): |
|
60116
3748bca4d869
Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
40217
diff
changeset
|
47 with warnings.catch_warnings(): |
|
3748bca4d869
Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
40217
diff
changeset
|
48 warnings.filterwarnings("error", module="<test string>") |
|
3748bca4d869
Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
40217
diff
changeset
|
49 run_unittest(GlobalTests) |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
50 |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
51 if __name__ == "__main__": |
|
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
24442
diff
changeset
|
52 test_main() |
