Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_contextlib.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | b36cb4602e21 |
| children |
| rev | line source |
|---|---|
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Unit tests for contextlib.py, and other context managers.""" |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
37566
33423b845554
test_contextlib wasn't actually being run by regrtest.py. Or more precisely,
Phillip J. Eby <pje@telecommunity.com>
parents:
37257
diff
changeset
|
3 import sys |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 import tempfile |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import unittest |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 from contextlib import * # Tests __all__ |
|
41534
6c4d7678af5e
Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents:
39708
diff
changeset
|
7 from test import test_support |
|
61046
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60116
diff
changeset
|
8 try: |
|
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60116
diff
changeset
|
9 import threading |
|
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60116
diff
changeset
|
10 except ImportError: |
|
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60116
diff
changeset
|
11 threading = None |
|
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:
59045
diff
changeset
|
12 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 class ContextManagerTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
16 def test_contextmanager_plain(self): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 state = [] |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
18 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 state.append(999) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
29 def test_contextmanager_finally(self): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 state = [] |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
31 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 state.append(999) |
|
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:
59045
diff
changeset
|
38 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 raise ZeroDivisionError() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
46 def test_contextmanager_no_reraise(self): |
|
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
47 @contextmanager |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
48 def whee(): |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
49 yield |
|
38084
a812d0fae74a
Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents:
37971
diff
changeset
|
50 ctx = whee() |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
51 ctx.__enter__() |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
52 # Calling __exit__ should not result in an exception |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
53 self.assertFalse(ctx.__exit__(TypeError, TypeError("foo"), None)) |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
54 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
55 def test_contextmanager_trap_yield_after_throw(self): |
|
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
56 @contextmanager |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
57 def whoo(): |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
58 try: |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
59 yield |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
60 except: |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
61 yield |
|
38084
a812d0fae74a
Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents:
37971
diff
changeset
|
62 ctx = whoo() |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
63 ctx.__enter__() |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
64 self.assertRaises( |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
65 RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
66 ) |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
67 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
68 def test_contextmanager_except(self): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 state = [] |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
70 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 except ZeroDivisionError, e: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 state.append(e.args[0]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 raise ZeroDivisionError(999) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
85 def _create_contextmanager_attribs(self): |
|
37257
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
86 def attribs(**kw): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
87 def decorate(func): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
88 for k,v in kw.items(): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
89 setattr(func,k,v) |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
90 return func |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
91 return decorate |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
92 @contextmanager |
|
37257
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
93 @attribs(foo='bar') |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
94 def baz(spam): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
95 """Whee!""" |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
96 return baz |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
97 |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
98 def test_contextmanager_attribs(self): |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
99 baz = self._create_contextmanager_attribs() |
|
37257
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
100 self.assertEqual(baz.__name__,'baz') |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
101 self.assertEqual(baz.foo, 'bar') |
|
59045
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
102 |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
103 @unittest.skipIf(sys.flags.optimize >= 2, |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
104 "Docstrings are omitted with -O2 and above") |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
105 def test_contextmanager_doc_attrib(self): |
|
a4c45f8e70aa
Issue 6292: for the moment at least, the test suite passes if run
R. David Murray <rdmurray@bitdance.com>
parents:
58817
diff
changeset
|
106 baz = self._create_contextmanager_attribs() |
|
37257
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
107 self.assertEqual(baz.__doc__, "Whee!") |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
108 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 class NestedTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 # XXX This needs more work |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 def test_nested(self): |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
114 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 def a(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 yield 1 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
117 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 def b(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 yield 2 |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
120 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 def c(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 yield 3 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 with nested(a(), b(), c()) as (x, y, z): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 self.assertEqual(x, 1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 self.assertEqual(y, 2) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 self.assertEqual(z, 3) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 def test_nested_cleanup(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 state = [] |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
130 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 def a(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 yield 2 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 state.append(3) |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
137 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 def b(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 state.append(4) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 yield 5 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 state.append(6) |
|
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:
59045
diff
changeset
|
144 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 with nested(a(), b()) as (x, y): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 state.append(y) |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46613
diff
changeset
|
148 1 // 0 |
|
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:
59045
diff
changeset
|
149 self.assertEqual(state, [1, 4, 2, 5, 6, 3]) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 |
|
37955
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
151 def test_nested_right_exception(self): |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
152 @contextmanager |
|
37955
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
153 def a(): |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
154 yield 1 |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
155 class b(object): |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
156 def __enter__(self): |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
157 return 2 |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
158 def __exit__(self, *exc_info): |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
159 try: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
160 raise Exception() |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
161 except: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
162 pass |
|
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:
59045
diff
changeset
|
163 with self.assertRaises(ZeroDivisionError): |
|
37955
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
164 with nested(a(), b()) as (x, y): |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46613
diff
changeset
|
165 1 // 0 |
|
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:
59045
diff
changeset
|
166 self.assertEqual((x, y), (1, 2)) |
|
37955
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
167 |
|
36770
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
168 def test_nested_b_swallows(self): |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
169 @contextmanager |
|
36770
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
170 def a(): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
171 yield |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
172 @contextmanager |
|
36770
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
173 def b(): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
174 try: |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
175 yield |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
176 except: |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
177 # Swallow the exception |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
178 pass |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
179 try: |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
180 with nested(a(), b()): |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46613
diff
changeset
|
181 1 // 0 |
|
36770
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
182 except ZeroDivisionError: |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
183 self.fail("Didn't swallow ZeroDivisionError") |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
184 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
185 def test_nested_break(self): |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
186 @contextmanager |
|
36770
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
187 def a(): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
188 yield |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
189 state = 0 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
190 while True: |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
191 state += 1 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
192 with nested(a(), a()): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
193 break |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
194 state += 10 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
195 self.assertEqual(state, 1) |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
196 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
197 def test_nested_continue(self): |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
198 @contextmanager |
|
36770
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
199 def a(): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
200 yield |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
201 state = 0 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
202 while state < 3: |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
203 state += 1 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
204 with nested(a(), a()): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
205 continue |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
206 state += 10 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
207 self.assertEqual(state, 3) |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
208 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
209 def test_nested_return(self): |
|
38101
6bc1e83a97a6
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents:
38084
diff
changeset
|
210 @contextmanager |
|
36770
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
211 def a(): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
212 try: |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
213 yield |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
214 except: |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
215 pass |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
216 def foo(): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
217 with nested(a(), a()): |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
218 return 1 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
219 return 10 |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
220 self.assertEqual(foo(), 1) |
|
623763457a4e
Fix a bug in nested() - if one of the sub-context-managers swallows the
Guido van Rossum <guido@python.org>
parents:
36737
diff
changeset
|
221 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 class ClosingTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 # XXX This needs more work |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 def test_closing(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 class C: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 def close(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 x = C() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 self.assertEqual(state, []) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 with closing(x) as y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 self.assertEqual(x, y) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 def test_closing_error(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 class C: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 def close(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 x = C() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 self.assertEqual(state, []) |
|
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:
59045
diff
changeset
|
244 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 with closing(x) as y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 self.assertEqual(x, y) |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46613
diff
changeset
|
247 1 // 0 |
|
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:
59045
diff
changeset
|
248 self.assertEqual(state, [1]) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 class FileContextTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 def testWithOpen(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 tfn = tempfile.mktemp() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 f = None |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 with open(tfn, "w") as f: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
257 self.assertFalse(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 f.write("Booh\n") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
259 self.assertTrue(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 f = None |
|
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:
59045
diff
changeset
|
261 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 with open(tfn, "r") as f: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
263 self.assertFalse(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 self.assertEqual(f.read(), "Booh\n") |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46613
diff
changeset
|
265 1 // 0 |
|
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:
59045
diff
changeset
|
266 self.assertTrue(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 finally: |
|
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:
59045
diff
changeset
|
268 test_support.unlink(tfn) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 |
|
61046
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60116
diff
changeset
|
270 @unittest.skipUnless(threading, 'Threading required for this test.') |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 class LockContextTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 def boilerPlate(self, lock, locked): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
274 self.assertFalse(locked()) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 with lock: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
276 self.assertTrue(locked()) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
277 self.assertFalse(locked()) |
|
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:
59045
diff
changeset
|
278 with self.assertRaises(ZeroDivisionError): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 with lock: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
280 self.assertTrue(locked()) |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46613
diff
changeset
|
281 1 // 0 |
|
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:
59045
diff
changeset
|
282 self.assertFalse(locked()) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 def testWithLock(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 lock = threading.Lock() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 self.boilerPlate(lock, lock.locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 def testWithRLock(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 lock = threading.RLock() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 self.boilerPlate(lock, lock._is_owned) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 def testWithCondition(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 lock = threading.Condition() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 return lock._is_owned() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 def testWithSemaphore(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 lock = threading.Semaphore() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 if lock.acquire(False): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 lock.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 return False |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 return True |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 def testWithBoundedSemaphore(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 lock = threading.BoundedSemaphore() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 if lock.acquire(False): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 lock.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 return False |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 return True |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 |
|
37566
33423b845554
test_contextlib wasn't actually being run by regrtest.py. Or more precisely,
Phillip J. Eby <pje@telecommunity.com>
parents:
37257
diff
changeset
|
318 # This is needed to make the test actually run under regrtest.py! |
|
33423b845554
test_contextlib wasn't actually being run by regrtest.py. Or more precisely,
Phillip J. Eby <pje@telecommunity.com>
parents:
37257
diff
changeset
|
319 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:
59045
diff
changeset
|
320 with test_support.check_warnings(("With-statements now directly support " |
|
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:
59045
diff
changeset
|
321 "multiple context managers", |
|
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:
59045
diff
changeset
|
322 DeprecationWarning)): |
|
54411
260c8ed3f5df
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Raymond Hettinger <python@rcn.com>
parents:
46613
diff
changeset
|
323 test_support.run_unittest(__name__) |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46613
diff
changeset
|
324 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 if __name__ == "__main__": |
|
37566
33423b845554
test_contextlib wasn't actually being run by regrtest.py. Or more precisely,
Phillip J. Eby <pje@telecommunity.com>
parents:
37257
diff
changeset
|
326 test_main() |
