Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_contextlib.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 6768b2350c57 |
| children | 70e115c34f97 |
| 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 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
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
|
4 import sys |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 import os |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 import decimal |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 import tempfile |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 import unittest |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 import threading |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 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
|
11 from test import test_support |
|
54411
260c8ed3f5df
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Raymond Hettinger <python@rcn.com>
parents:
46613
diff
changeset
|
12 import warnings |
|
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) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 try: |
|
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 except ZeroDivisionError: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 pass |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 self.fail("Expected ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 |
|
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
|
50 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
|
51 @contextmanager |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
52 def whee(): |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
53 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
|
54 ctx = whee() |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
55 ctx.__enter__() |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
56 # 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
|
57 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
|
58 |
|
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
|
59 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
|
60 @contextmanager |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
61 def whoo(): |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
62 try: |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
63 yield |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
64 except: |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
65 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
|
66 ctx = whoo() |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
67 ctx.__enter__() |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
68 self.assertRaises( |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
69 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
|
70 ) |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
71 |
|
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
|
72 def test_contextmanager_except(self): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 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
|
74 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 except ZeroDivisionError, e: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 state.append(e.args[0]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 raise ZeroDivisionError(999) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 |
|
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
|
89 def test_contextmanager_attribs(self): |
|
37257
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
90 def attribs(**kw): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
91 def decorate(func): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
92 for k,v in kw.items(): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
93 setattr(func,k,v) |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
94 return func |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
95 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
|
96 @contextmanager |
|
37257
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
97 @attribs(foo='bar') |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
98 def baz(spam): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
99 """Whee!""" |
|
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') |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
102 self.assertEqual(baz.__doc__, "Whee!") |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
103 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 class NestedTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 # XXX This needs more work |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 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
|
109 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 def a(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 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
|
112 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 def b(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 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
|
115 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 def c(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 yield 3 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 with nested(a(), b(), c()) as (x, y, z): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 self.assertEqual(x, 1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 self.assertEqual(y, 2) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 self.assertEqual(z, 3) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 def test_nested_cleanup(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 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
|
125 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 def a(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 yield 2 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 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
|
132 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 def b(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 state.append(4) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 yield 5 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 state.append(6) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 with nested(a(), b()) as (x, y): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 state.append(y) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 1/0 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 except ZeroDivisionError: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 self.assertEqual(state, [1, 4, 2, 5, 6, 3]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 self.fail("Didn't raise ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
37955
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
149 def test_nested_right_exception(self): |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
150 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
|
151 @contextmanager |
|
37955
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
152 def a(): |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
153 yield 1 |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
154 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
|
155 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
|
156 return 2 |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
157 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
|
158 try: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
159 raise Exception() |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
160 except: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
161 pass |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
162 try: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
163 with nested(a(), b()) as (x, y): |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
164 1/0 |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
165 except ZeroDivisionError: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
166 self.assertEqual((x, y), (1, 2)) |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
167 except Exception: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
168 self.fail("Reraised wrong exception") |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
169 else: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
170 self.fail("Didn't raise ZeroDivisionError") |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
171 |
|
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
|
172 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
|
173 @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
|
174 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
|
175 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
|
176 @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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 # 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
|
182 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
|
183 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
|
184 with nested(a(), 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
|
185 1/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
|
186 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
|
187 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
|
188 |
|
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 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
|
190 @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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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 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
|
197 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
|
198 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
|
199 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
|
200 |
|
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 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
|
202 @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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 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
|
212 |
|
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 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
|
214 @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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 class ClosingTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 # XXX This needs more work |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 def test_closing(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 class C: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 def close(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 x = C() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 self.assertEqual(state, []) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 with closing(x) as y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 self.assertEqual(x, y) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 def test_closing_error(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 class C: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 def close(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 x = C() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 self.assertEqual(state, []) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 with closing(x) as y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 self.assertEqual(x, y) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 1/0 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 except ZeroDivisionError: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 self.fail("Didn't raise ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 class FileContextTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 def testWithOpen(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 tfn = tempfile.mktemp() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 f = None |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 with open(tfn, "w") as f: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
264 self.assertFalse(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 f.write("Booh\n") |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
266 self.assertTrue(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 f = None |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 with open(tfn, "r") as f: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
270 self.assertFalse(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 self.assertEqual(f.read(), "Booh\n") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 1/0 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 except ZeroDivisionError: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
274 self.assertTrue(f.closed) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 self.fail("Didn't raise ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 os.remove(tfn) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 except os.error: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 pass |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 class LockContextTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 def boilerPlate(self, lock, locked): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
286 self.assertFalse(locked()) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 with lock: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
288 self.assertTrue(locked()) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
289 self.assertFalse(locked()) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 with lock: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
292 self.assertTrue(locked()) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 1/0 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 except ZeroDivisionError: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54411
diff
changeset
|
295 self.assertFalse(locked()) |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 self.fail("Didn't raise ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 def testWithLock(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 lock = threading.Lock() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 self.boilerPlate(lock, lock.locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 def testWithRLock(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 lock = threading.RLock() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 self.boilerPlate(lock, lock._is_owned) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 def testWithCondition(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 lock = threading.Condition() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 return lock._is_owned() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 def testWithSemaphore(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 lock = threading.Semaphore() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 if lock.acquire(False): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 lock.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 return False |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 return True |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 def testWithBoundedSemaphore(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 lock = threading.BoundedSemaphore() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 if lock.acquire(False): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 lock.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 return False |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 return True |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 |
|
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
|
333 # 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
|
334 def test_main(): |
|
54411
260c8ed3f5df
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Raymond Hettinger <python@rcn.com>
parents:
46613
diff
changeset
|
335 with warnings.catch_warnings(): |
|
260c8ed3f5df
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Raymond Hettinger <python@rcn.com>
parents:
46613
diff
changeset
|
336 warnings.simplefilter('ignore') |
|
260c8ed3f5df
Deprecate contextlib.nested(). The with-statement now provides this functionality directly.
Raymond Hettinger <python@rcn.com>
parents:
46613
diff
changeset
|
337 test_support.run_unittest(__name__) |
|
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
|
338 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 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
|
340 test_main() |
