Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_contextlib.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 68cfc51e4331 |
| children | 762d89faceaa 260c8ed3f5df |
| 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 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 class ContextManagerTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
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
|
15 def test_contextmanager_plain(self): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 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
|
17 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 state.append(999) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
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
|
28 def test_contextmanager_finally(self): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 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
|
30 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 state.append(999) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 raise ZeroDivisionError() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 except ZeroDivisionError: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 pass |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 self.fail("Expected ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 |
|
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
|
49 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
|
50 @contextmanager |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
51 def whee(): |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
52 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
|
53 ctx = whee() |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
54 ctx.__enter__() |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
55 # Calling __exit__ should not result in an exception |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
56 self.failIf(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
|
57 |
|
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
|
58 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
|
59 @contextmanager |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
60 def whoo(): |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
61 try: |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
62 yield |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
63 except: |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
64 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
|
65 ctx = whoo() |
|
37203
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
66 ctx.__enter__() |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
67 self.assertRaises( |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
68 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
|
69 ) |
|
b39c3d7a5ad4
Fix a problem with @contextmanager not detecting a broken generator
Phillip J. Eby <pje@telecommunity.com>
parents:
36770
diff
changeset
|
70 |
|
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
|
71 def test_contextmanager_except(self): |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 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
|
73 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 def woohoo(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 yield 42 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 except ZeroDivisionError, e: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 state.append(e.args[0]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 with woohoo() as x: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 self.assertEqual(x, 42) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 raise ZeroDivisionError(999) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 self.assertEqual(state, [1, 42, 999]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 |
|
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
|
88 def test_contextmanager_attribs(self): |
|
37257
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
89 def attribs(**kw): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
90 def decorate(func): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
91 for k,v in kw.items(): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
92 setattr(func,k,v) |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
93 return func |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
94 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
|
95 @contextmanager |
|
37257
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
96 @attribs(foo='bar') |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
97 def baz(spam): |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
98 """Whee!""" |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
99 self.assertEqual(baz.__name__,'baz') |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
100 self.assertEqual(baz.foo, 'bar') |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
101 self.assertEqual(baz.__doc__, "Whee!") |
|
034c0cf5007b
Fix contextlib not copying function attributes
Phillip J. Eby <pje@telecommunity.com>
parents:
37203
diff
changeset
|
102 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 class NestedTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 # XXX This needs more work |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 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
|
108 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 def a(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 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
|
111 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 def b(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 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
|
114 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 def c(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 yield 3 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 with nested(a(), b(), c()) as (x, y, z): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 self.assertEqual(x, 1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 self.assertEqual(y, 2) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 self.assertEqual(z, 3) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 def test_nested_cleanup(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 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
|
124 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 def a(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 yield 2 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 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
|
131 @contextmanager |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 def b(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 state.append(4) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 yield 5 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 state.append(6) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 with nested(a(), b()) as (x, y): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 state.append(x) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 state.append(y) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 1/0 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 except ZeroDivisionError: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 self.assertEqual(state, [1, 4, 2, 5, 6, 3]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 self.fail("Didn't raise ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 |
|
37955
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
148 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
|
149 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
|
150 @contextmanager |
|
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 a(): |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
152 yield 1 |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
153 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
|
154 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
|
155 return 2 |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
156 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
|
157 try: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
158 raise Exception() |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
159 except: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
160 pass |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
161 try: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
162 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
|
163 1/0 |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
164 except ZeroDivisionError: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
165 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
|
166 except Exception: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
167 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
|
168 else: |
|
adb10afda55b
Fix contextlib.nested to cope with exit methods raising and handling exceptions
Nick Coghlan <ncoghlan@gmail.com>
parents:
37567
diff
changeset
|
169 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
|
170 |
|
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
|
171 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
|
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 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
|
174 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
|
175 @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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 # 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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 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
|
187 |
|
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 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
|
189 @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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 |
|
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 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
|
201 @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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 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
|
211 |
|
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 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
|
213 @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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 class ClosingTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 # XXX This needs more work |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 def test_closing(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 class C: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 def close(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 x = C() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 self.assertEqual(state, []) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 with closing(x) as y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 self.assertEqual(x, y) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 def test_closing_error(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 state = [] |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 class C: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 def close(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 state.append(1) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 x = C() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 self.assertEqual(state, []) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 with closing(x) as y: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 self.assertEqual(x, y) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 1/0 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 except ZeroDivisionError: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 self.assertEqual(state, [1]) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 self.fail("Didn't raise ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 class FileContextTestCase(unittest.TestCase): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 def testWithOpen(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 tfn = tempfile.mktemp() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 f = None |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 with open(tfn, "w") as f: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 self.failIf(f.closed) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 f.write("Booh\n") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 self.failUnless(f.closed) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 f = None |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 with open(tfn, "r") as f: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 self.failIf(f.closed) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 self.assertEqual(f.read(), "Booh\n") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 1/0 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 except ZeroDivisionError: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 self.failUnless(f.closed) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 self.fail("Didn't raise ZeroDivisionError") |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 finally: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 os.remove(tfn) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 except os.error: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 pass |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 class LockContextTestCase(unittest.TestCase): |
|
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 boilerPlate(self, lock, locked): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 self.failIf(locked()) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 with lock: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 self.failUnless(locked()) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 self.failIf(locked()) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 try: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 with lock: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 self.failUnless(locked()) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 1/0 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 except ZeroDivisionError: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 self.failIf(locked()) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 self.fail("Didn't raise ZeroDivisionError") |
|
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 testWithLock(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 lock = threading.Lock() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 self.boilerPlate(lock, lock.locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 def testWithRLock(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 lock = threading.RLock() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 self.boilerPlate(lock, lock._is_owned) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 def testWithCondition(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 lock = threading.Condition() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 return lock._is_owned() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 def testWithSemaphore(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 lock = threading.Semaphore() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 if lock.acquire(False): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 lock.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 return False |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 return True |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 def testWithBoundedSemaphore(self): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 lock = threading.BoundedSemaphore() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 def locked(): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 if lock.acquire(False): |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 lock.release() |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 return False |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 else: |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 return True |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 self.boilerPlate(lock, locked) |
|
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 |
|
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
|
332 # 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
|
333 def test_main(): |
|
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
|
334 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
|
335 |
|
36737
fa92fe15251a
Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 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
|
337 test_main() |
