annotate Lib/test/test_contextlib.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 87d49e2cdd34
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
86195
63a1ee94b3ed Issue #15805: Add contextlib.redirect_stdout()
Raymond Hettinger <python@rcn.com>
parents: 85912
diff changeset
3 import io
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
4 import sys
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import tempfile
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import unittest
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 from contextlib import * # Tests __all__
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46664
diff changeset
8 from test import support
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60174
diff changeset
9 try:
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60174
diff changeset
10 import threading
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60174
diff changeset
11 except ImportError:
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60174
diff changeset
12 threading = None
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
14
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 class ContextManagerTestCase(unittest.TestCase):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 def test_contextmanager_plain(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 state = []
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 @contextmanager
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 def woohoo():
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 state.append(1)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 yield 42
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 state.append(999)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 with woohoo() as x:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 self.assertEqual(state, [1])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 self.assertEqual(x, 42)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 state.append(x)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 self.assertEqual(state, [1, 42, 999])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def test_contextmanager_finally(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 state = []
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 @contextmanager
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def woohoo():
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 state.append(1)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 try:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 yield 42
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 finally:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 state.append(999)
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
39 with self.assertRaises(ZeroDivisionError):
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 with woohoo() as x:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 self.assertEqual(state, [1])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 self.assertEqual(x, 42)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 state.append(x)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 raise ZeroDivisionError()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 self.assertEqual(state, [1, 42, 999])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
47 def test_contextmanager_no_reraise(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
48 @contextmanager
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
49 def whee():
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
50 yield
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
51 ctx = whee()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
52 ctx.__enter__()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
53 # Calling __exit__ should not result in an exception
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54406
diff changeset
54 self.assertFalse(ctx.__exit__(TypeError, TypeError("foo"), None))
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
55
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
56 def test_contextmanager_trap_yield_after_throw(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
57 @contextmanager
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
58 def whoo():
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
59 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
60 yield
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
61 except:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
62 yield
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
63 ctx = whoo()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
64 ctx.__enter__()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
65 self.assertRaises(
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
66 RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
67 )
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
68
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 def test_contextmanager_except(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 state = []
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 @contextmanager
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 def woohoo():
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 state.append(1)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 try:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 yield 42
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
76 except ZeroDivisionError as e:
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 state.append(e.args[0])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 self.assertEqual(state, [1, 42, 999])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 with woohoo() as x:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 self.assertEqual(state, [1])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 self.assertEqual(x, 42)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 state.append(x)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 raise ZeroDivisionError(999)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 self.assertEqual(state, [1, 42, 999])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
86 def _create_contextmanager_attribs(self):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
87 def attribs(**kw):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
88 def decorate(func):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
89 for k,v in kw.items():
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
90 setattr(func,k,v)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
91 return func
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
92 return decorate
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
93 @contextmanager
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
94 @attribs(foo='bar')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
95 def baz(spam):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
96 """Whee!"""
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
97 return baz
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
98
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
99 def test_contextmanager_attribs(self):
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
100 baz = self._create_contextmanager_attribs()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
101 self.assertEqual(baz.__name__,'baz')
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
102 self.assertEqual(baz.foo, 'bar')
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
103
86656
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
104 @support.requires_docstrings
59091
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
105 def test_contextmanager_doc_attrib(self):
8d3ff2fbbe4b Merged revisions 78351 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 55021
diff changeset
106 baz = self._create_contextmanager_attribs()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
107 self.assertEqual(baz.__doc__, "Whee!")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
108
86656
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
109 @support.requires_docstrings
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
110 def test_instance_docstring_given_cm_docstring(self):
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
111 baz = self._create_contextmanager_attribs()(None)
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
112 self.assertEqual(baz.__doc__, "Whee!")
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
113
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
114
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 class ClosingTestCase(unittest.TestCase):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
86656
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
117 @support.requires_docstrings
86650
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
118 def test_instance_docs(self):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
119 # Issue 19330: ensure context manager instances have good docstrings
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
120 cm_docstring = closing.__doc__
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
121 obj = closing(None)
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
122 self.assertEqual(obj.__doc__, cm_docstring)
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 def test_closing(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 state = []
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 class C:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 def close(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 state.append(1)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 x = C()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 self.assertEqual(state, [])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 with closing(x) as y:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 self.assertEqual(x, y)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 self.assertEqual(state, [1])
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 def test_closing_error(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 state = []
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 class C:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 def close(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 state.append(1)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 x = C()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 self.assertEqual(state, [])
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
142 with self.assertRaises(ZeroDivisionError):
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 with closing(x) as y:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 self.assertEqual(x, y)
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
145 1 / 0
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
146 self.assertEqual(state, [1])
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 class FileContextTestCase(unittest.TestCase):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 def testWithOpen(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 tfn = tempfile.mktemp()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 try:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 f = None
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 with open(tfn, "w") as f:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54406
diff changeset
155 self.assertFalse(f.closed)
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 f.write("Booh\n")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54406
diff changeset
157 self.assertTrue(f.closed)
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 f = None
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
159 with self.assertRaises(ZeroDivisionError):
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 with open(tfn, "r") as f:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54406
diff changeset
161 self.assertFalse(f.closed)
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 self.assertEqual(f.read(), "Booh\n")
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
163 1 / 0
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
164 self.assertTrue(f.closed)
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 finally:
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
166 support.unlink(tfn)
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60174
diff changeset
168 @unittest.skipUnless(threading, 'Threading required for this test.')
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 class LockContextTestCase(unittest.TestCase):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 def boilerPlate(self, lock, locked):
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54406
diff changeset
172 self.assertFalse(locked())
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 with lock:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54406
diff changeset
174 self.assertTrue(locked())
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54406
diff changeset
175 self.assertFalse(locked())
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
176 with self.assertRaises(ZeroDivisionError):
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 with lock:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54406
diff changeset
178 self.assertTrue(locked())
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
179 1 / 0
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59615
diff changeset
180 self.assertFalse(locked())
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 def testWithLock(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 lock = threading.Lock()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 self.boilerPlate(lock, lock.locked)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 def testWithRLock(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 lock = threading.RLock()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 self.boilerPlate(lock, lock._is_owned)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 def testWithCondition(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 lock = threading.Condition()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 def locked():
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 return lock._is_owned()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 self.boilerPlate(lock, locked)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
195
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 def testWithSemaphore(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 lock = threading.Semaphore()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 def locked():
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 if lock.acquire(False):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 lock.release()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 return False
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 else:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 return True
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 self.boilerPlate(lock, locked)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 def testWithBoundedSemaphore(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 lock = threading.BoundedSemaphore()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 def locked():
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 if lock.acquire(False):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 lock.release()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 return False
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 else:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 return True
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 self.boilerPlate(lock, locked)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
62652
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
216
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
217 class mycontext(ContextDecorator):
86650
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
218 """Example decoration-compatible context manager for testing"""
62652
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
219 started = False
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
220 exc = None
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
221 catch = False
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
222
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
223 def __enter__(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
224 self.started = True
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
225 return self
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
226
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
227 def __exit__(self, *exc):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
228 self.exc = exc
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
229 return self.catch
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
230
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
231
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
232 class TestContextDecorator(unittest.TestCase):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
233
86656
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
234 @support.requires_docstrings
86650
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
235 def test_instance_docs(self):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
236 # Issue 19330: ensure context manager instances have good docstrings
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
237 cm_docstring = mycontext.__doc__
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
238 obj = mycontext()
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
239 self.assertEqual(obj.__doc__, cm_docstring)
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
240
62652
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
241 def test_contextdecorator(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
242 context = mycontext()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
243 with context as result:
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
244 self.assertIs(result, context)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
245 self.assertTrue(context.started)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
246
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
247 self.assertEqual(context.exc, (None, None, None))
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
248
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
249
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
250 def test_contextdecorator_with_exception(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
251 context = mycontext()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
252
66581
38bec90fd8ab #10273: Rename assertRegexpMatches and assertRaisesRegexp to assertRegex and assertRaisesRegex.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62652
diff changeset
253 with self.assertRaisesRegex(NameError, 'foo'):
62652
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
254 with context:
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
255 raise NameError('foo')
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
256 self.assertIsNotNone(context.exc)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
257 self.assertIs(context.exc[0], NameError)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
258
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
259 context = mycontext()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
260 context.catch = True
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
261 with context:
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
262 raise NameError('foo')
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
263 self.assertIsNotNone(context.exc)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
264 self.assertIs(context.exc[0], NameError)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
265
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
266
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
267 def test_decorator(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
268 context = mycontext()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
269
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
270 @context
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
271 def test():
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
272 self.assertIsNone(context.exc)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
273 self.assertTrue(context.started)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
274 test()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
275 self.assertEqual(context.exc, (None, None, None))
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
276
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
277
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
278 def test_decorator_with_exception(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
279 context = mycontext()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
280
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
281 @context
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
282 def test():
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
283 self.assertIsNone(context.exc)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
284 self.assertTrue(context.started)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
285 raise NameError('foo')
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
286
66581
38bec90fd8ab #10273: Rename assertRegexpMatches and assertRaisesRegexp to assertRegex and assertRaisesRegex.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62652
diff changeset
287 with self.assertRaisesRegex(NameError, 'foo'):
62652
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
288 test()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
289 self.assertIsNotNone(context.exc)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
290 self.assertIs(context.exc[0], NameError)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
291
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
292
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
293 def test_decorating_method(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
294 context = mycontext()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
295
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
296 class Test(object):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
297
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
298 @context
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
299 def method(self, a, b, c=None):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
300 self.a = a
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
301 self.b = b
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
302 self.c = c
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
303
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
304 # these tests are for argument passing when used as a decorator
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
305 test = Test()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
306 test.method(1, 2)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
307 self.assertEqual(test.a, 1)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
308 self.assertEqual(test.b, 2)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
309 self.assertEqual(test.c, None)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
310
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
311 test = Test()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
312 test.method('a', 'b', 'c')
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
313 self.assertEqual(test.a, 'a')
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
314 self.assertEqual(test.b, 'b')
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
315 self.assertEqual(test.c, 'c')
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
316
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
317 test = Test()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
318 test.method(a=1, b=2)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
319 self.assertEqual(test.a, 1)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
320 self.assertEqual(test.b, 2)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
321
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
322
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
323 def test_typo_enter(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
324 class mycontext(ContextDecorator):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
325 def __unter__(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
326 pass
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
327 def __exit__(self, *exc):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
328 pass
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
329
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
330 with self.assertRaises(AttributeError):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
331 with mycontext():
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
332 pass
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
333
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
334
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
335 def test_typo_exit(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
336 class mycontext(ContextDecorator):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
337 def __enter__(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
338 pass
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
339 def __uxit__(self, *exc):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
340 pass
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
341
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
342 with self.assertRaises(AttributeError):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
343 with mycontext():
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
344 pass
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
345
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
346
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
347 def test_contextdecorator_as_mixin(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
348 class somecontext(object):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
349 started = False
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
350 exc = None
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
351
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
352 def __enter__(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
353 self.started = True
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
354 return self
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
355
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
356 def __exit__(self, *exc):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
357 self.exc = exc
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
358
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
359 class mycontext(somecontext, ContextDecorator):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
360 pass
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
361
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
362 context = mycontext()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
363 @context
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
364 def test():
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
365 self.assertIsNone(context.exc)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
366 self.assertTrue(context.started)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
367 test()
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
368 self.assertEqual(context.exc, (None, None, None))
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
369
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
370
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
371 def test_contextmanager_as_decorator(self):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
372 @contextmanager
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
373 def woohoo(y):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
374 state.append(y)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
375 yield
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
376 state.append(999)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
377
69849
e4ba097123f6 Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66581
diff changeset
378 state = []
62652
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
379 @woohoo(1)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
380 def test(x):
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
381 self.assertEqual(state, [1])
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
382 state.append(x)
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
383 test('something')
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
384 self.assertEqual(state, [1, 'something', 999])
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
385
69849
e4ba097123f6 Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66581
diff changeset
386 # Issue #11647: Ensure the decorated function is 'reusable'
e4ba097123f6 Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66581
diff changeset
387 state = []
e4ba097123f6 Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66581
diff changeset
388 test('something else')
e4ba097123f6 Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66581
diff changeset
389 self.assertEqual(state, [1, 'something else', 999])
e4ba097123f6 Issue #11647: allow contextmanager objects to be used as decorators as described in the docs. Initial patch by Ysj Ray.
Nick Coghlan <ncoghlan@gmail.com>
parents: 66581
diff changeset
390
62652
30e1d8d08105 Issue 9110. Adding ContextDecorator to contextlib. This enables the creation of APIs that act as decorators as well as context managers. contextlib.contextmanager changed to use ContextDecorator.
Michael Foord <fuzzyman@voidspace.org.uk>
parents: 61073
diff changeset
391
77095
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
392 class TestExitStack(unittest.TestCase):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
393
86656
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
394 @support.requires_docstrings
86650
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
395 def test_instance_docs(self):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
396 # Issue 19330: ensure context manager instances have good docstrings
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
397 cm_docstring = ExitStack.__doc__
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
398 obj = ExitStack()
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
399 self.assertEqual(obj.__doc__, cm_docstring)
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
400
77095
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
401 def test_no_resources(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
402 with ExitStack():
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
403 pass
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
404
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
405 def test_callback(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
406 expected = [
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
407 ((), {}),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
408 ((1,), {}),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
409 ((1,2), {}),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
410 ((), dict(example=1)),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
411 ((1,), dict(example=1)),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
412 ((1,2), dict(example=1)),
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
413 ]
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
414 result = []
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
415 def _exit(*args, **kwds):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
416 """Test metadata propagation"""
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
417 result.append((args, kwds))
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
418 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
419 for args, kwds in reversed(expected):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
420 if args and kwds:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
421 f = stack.callback(_exit, *args, **kwds)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
422 elif args:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
423 f = stack.callback(_exit, *args)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
424 elif kwds:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
425 f = stack.callback(_exit, **kwds)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
426 else:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
427 f = stack.callback(_exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
428 self.assertIs(f, _exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
429 for wrapper in stack._exit_callbacks:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
430 self.assertIs(wrapper.__wrapped__, _exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
431 self.assertNotEqual(wrapper.__name__, _exit.__name__)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
432 self.assertIsNone(wrapper.__doc__, _exit.__doc__)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
433 self.assertEqual(result, expected)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
434
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
435 def test_push(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
436 exc_raised = ZeroDivisionError
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
437 def _expect_exc(exc_type, exc, exc_tb):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
438 self.assertIs(exc_type, exc_raised)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
439 def _suppress_exc(*exc_details):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
440 return True
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
441 def _expect_ok(exc_type, exc, exc_tb):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
442 self.assertIsNone(exc_type)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
443 self.assertIsNone(exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
444 self.assertIsNone(exc_tb)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
445 class ExitCM(object):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
446 def __init__(self, check_exc):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
447 self.check_exc = check_exc
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
448 def __enter__(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
449 self.fail("Should not be called!")
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
450 def __exit__(self, *exc_details):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
451 self.check_exc(*exc_details)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
452 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
453 stack.push(_expect_ok)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
454 self.assertIs(stack._exit_callbacks[-1], _expect_ok)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
455 cm = ExitCM(_expect_ok)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
456 stack.push(cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
457 self.assertIs(stack._exit_callbacks[-1].__self__, cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
458 stack.push(_suppress_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
459 self.assertIs(stack._exit_callbacks[-1], _suppress_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
460 cm = ExitCM(_expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
461 stack.push(cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
462 self.assertIs(stack._exit_callbacks[-1].__self__, cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
463 stack.push(_expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
464 self.assertIs(stack._exit_callbacks[-1], _expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
465 stack.push(_expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
466 self.assertIs(stack._exit_callbacks[-1], _expect_exc)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
467 1/0
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
468
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
469 def test_enter_context(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
470 class TestCM(object):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
471 def __enter__(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
472 result.append(1)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
473 def __exit__(self, *exc_details):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
474 result.append(3)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
475
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
476 result = []
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
477 cm = TestCM()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
478 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
479 @stack.callback # Registered first => cleaned up last
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
480 def _exit():
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
481 result.append(4)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
482 self.assertIsNotNone(_exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
483 stack.enter_context(cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
484 self.assertIs(stack._exit_callbacks[-1].__self__, cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
485 result.append(2)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
486 self.assertEqual(result, [1, 2, 3, 4])
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
487
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
488 def test_close(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
489 result = []
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
490 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
491 @stack.callback
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
492 def _exit():
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
493 result.append(1)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
494 self.assertIsNotNone(_exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
495 stack.close()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
496 result.append(2)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
497 self.assertEqual(result, [1, 2])
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
498
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
499 def test_pop_all(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
500 result = []
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
501 with ExitStack() as stack:
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
502 @stack.callback
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
503 def _exit():
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
504 result.append(3)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
505 self.assertIsNotNone(_exit)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
506 new_stack = stack.pop_all()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
507 result.append(1)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
508 result.append(2)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
509 new_stack.close()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
510 self.assertEqual(result, [1, 2, 3])
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
511
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
512 def test_exit_raise(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
513 with self.assertRaises(ZeroDivisionError):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
514 with ExitStack() as stack:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
515 stack.push(lambda *exc: False)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
516 1/0
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
517
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
518 def test_exit_suppress(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
519 with ExitStack() as stack:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
520 stack.push(lambda *exc: True)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
521 1/0
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
522
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
523 def test_exit_exception_chaining_reference(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
524 # Sanity check to make sure that ExitStack chaining matches
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
525 # actual nested with statements
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
526 class RaiseExc:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
527 def __init__(self, exc):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
528 self.exc = exc
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
529 def __enter__(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
530 return self
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
531 def __exit__(self, *exc_details):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
532 raise self.exc
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
533
77282
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
534 class RaiseExcWithContext:
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
535 def __init__(self, outer, inner):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
536 self.outer = outer
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
537 self.inner = inner
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
538 def __enter__(self):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
539 return self
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
540 def __exit__(self, *exc_details):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
541 try:
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
542 raise self.inner
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
543 except:
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
544 raise self.outer
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
545
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
546 class SuppressExc:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
547 def __enter__(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
548 return self
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
549 def __exit__(self, *exc_details):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
550 type(self).saved_details = exc_details
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
551 return True
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
552
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
553 try:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
554 with RaiseExc(IndexError):
77282
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
555 with RaiseExcWithContext(KeyError, AttributeError):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
556 with SuppressExc():
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
557 with RaiseExc(ValueError):
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
558 1 / 0
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
559 except IndexError as exc:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
560 self.assertIsInstance(exc.__context__, KeyError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
561 self.assertIsInstance(exc.__context__.__context__, AttributeError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
562 # Inner exceptions were suppressed
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
563 self.assertIsNone(exc.__context__.__context__.__context__)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
564 else:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
565 self.fail("Expected IndexError, but no exception was raised")
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
566 # Check the inner exceptions
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
567 inner_exc = SuppressExc.saved_details[1]
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
568 self.assertIsInstance(inner_exc, ValueError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
569 self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
570
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
571 def test_exit_exception_chaining(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
572 # Ensure exception chaining matches the reference behaviour
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
573 def raise_exc(exc):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
574 raise exc
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
575
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
576 saved_details = None
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
577 def suppress_exc(*exc_details):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
578 nonlocal saved_details
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
579 saved_details = exc_details
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
580 return True
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
581
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
582 try:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
583 with ExitStack() as stack:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
584 stack.callback(raise_exc, IndexError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
585 stack.callback(raise_exc, KeyError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
586 stack.callback(raise_exc, AttributeError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
587 stack.push(suppress_exc)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
588 stack.callback(raise_exc, ValueError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
589 1 / 0
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
590 except IndexError as exc:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
591 self.assertIsInstance(exc.__context__, KeyError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
592 self.assertIsInstance(exc.__context__.__context__, AttributeError)
77282
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
593 # Inner exceptions were suppressed
c108bc96aec6 Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Nick Coghlan <ncoghlan@gmail.com>
parents: 77260
diff changeset
594 self.assertIsNone(exc.__context__.__context__.__context__)
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
595 else:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
596 self.fail("Expected IndexError, but no exception was raised")
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
597 # Check the inner exceptions
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
598 inner_exc = saved_details[1]
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
599 self.assertIsInstance(inner_exc, ValueError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
600 self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
601
85911
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
602 def test_exit_exception_non_suppressing(self):
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
603 # http://bugs.python.org/issue19092
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
604 def raise_exc(exc):
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
605 raise exc
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
606
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
607 def suppress_exc(*exc_details):
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
608 return True
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
609
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
610 try:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
611 with ExitStack() as stack:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
612 stack.callback(lambda: None)
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
613 stack.callback(raise_exc, IndexError)
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
614 except Exception as exc:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
615 self.assertIsInstance(exc, IndexError)
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
616 else:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
617 self.fail("Expected IndexError, but no exception was raised")
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
618
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
619 try:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
620 with ExitStack() as stack:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
621 stack.callback(raise_exc, KeyError)
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
622 stack.push(suppress_exc)
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
623 stack.callback(raise_exc, IndexError)
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
624 except Exception as exc:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
625 self.assertIsInstance(exc, KeyError)
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
626 else:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
627 self.fail("Expected KeyError, but no exception was raised")
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
628
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
629 def test_body_exception_suppress(self):
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
630 def suppress_exc(*exc_details):
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
631 return True
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
632 try:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
633 with ExitStack() as stack:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
634 stack.push(suppress_exc)
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
635 1/0
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
636 except IndexError as exc:
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
637 self.fail("Expected no exception, got IndexError")
423736775f6b Close #19092: ExitStack now reraises exceptions from __exit__
Nick Coghlan <ncoghlan@gmail.com>
parents: 77282
diff changeset
638
77259
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
639 def test_exit_exception_chaining_suppress(self):
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
640 with ExitStack() as stack:
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
641 stack.push(lambda *exc: True)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
642 stack.push(lambda *exc: 1/0)
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
643 stack.push(lambda *exc: {}[1])
fc73e6ea9e73 Issue #14963: Added test cases for contextlib.ExitStack exception handling behaviour (Initial patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77095
diff changeset
644
77260
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
645 def test_excessive_nesting(self):
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
646 # The original implementation would die with RecursionError here
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
647 with ExitStack() as stack:
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
648 for i in range(10000):
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
649 stack.callback(int)
c0c7618762e5 Close #14963: Use an iterative algorithm in contextlib.ExitStack.__exit__ (Patch by Alon Horev)
Nick Coghlan <ncoghlan@gmail.com>
parents: 77259
diff changeset
650
77095
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
651 def test_instance_bypass(self):
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
652 class Example(object): pass
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
653 cm = Example()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
654 cm.__exit__ = object()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
655 stack = ExitStack()
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
656 self.assertRaises(AttributeError, stack.enter_context, cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
657 stack.push(cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
658 self.assertIs(stack._exit_callbacks[-1], cm)
8ef66c73b1e1 Close #13585: add contextlib.ExitStack to replace the ill-fated contextlib.nested API
Nick Coghlan <ncoghlan@gmail.com>
parents: 69849
diff changeset
659
86195
63a1ee94b3ed Issue #15805: Add contextlib.redirect_stdout()
Raymond Hettinger <python@rcn.com>
parents: 85912
diff changeset
660 class TestRedirectStdout(unittest.TestCase):
63a1ee94b3ed Issue #15805: Add contextlib.redirect_stdout()
Raymond Hettinger <python@rcn.com>
parents: 85912
diff changeset
661
86656
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
662 @support.requires_docstrings
86650
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
663 def test_instance_docs(self):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
664 # Issue 19330: ensure context manager instances have good docstrings
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
665 cm_docstring = redirect_stdout.__doc__
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
666 obj = redirect_stdout(None)
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
667 self.assertEqual(obj.__doc__, cm_docstring)
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
668
86869
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
669 def test_no_redirect_in_init(self):
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
670 orig_stdout = sys.stdout
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
671 redirect_stdout(None)
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
672 self.assertIs(sys.stdout, orig_stdout)
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
673
86195
63a1ee94b3ed Issue #15805: Add contextlib.redirect_stdout()
Raymond Hettinger <python@rcn.com>
parents: 85912
diff changeset
674 def test_redirect_to_string_io(self):
63a1ee94b3ed Issue #15805: Add contextlib.redirect_stdout()
Raymond Hettinger <python@rcn.com>
parents: 85912
diff changeset
675 f = io.StringIO()
86645
2b904290b3b9 Close #19396: make test_contextlib tolerate -S
Nick Coghlan <ncoghlan@gmail.com>
parents: 86470
diff changeset
676 msg = "Consider an API like help(), which prints directly to stdout"
86869
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
677 orig_stdout = sys.stdout
86195
63a1ee94b3ed Issue #15805: Add contextlib.redirect_stdout()
Raymond Hettinger <python@rcn.com>
parents: 85912
diff changeset
678 with redirect_stdout(f):
86645
2b904290b3b9 Close #19396: make test_contextlib tolerate -S
Nick Coghlan <ncoghlan@gmail.com>
parents: 86470
diff changeset
679 print(msg)
86869
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
680 self.assertIs(sys.stdout, orig_stdout)
86645
2b904290b3b9 Close #19396: make test_contextlib tolerate -S
Nick Coghlan <ncoghlan@gmail.com>
parents: 86470
diff changeset
681 s = f.getvalue().strip()
2b904290b3b9 Close #19396: make test_contextlib tolerate -S
Nick Coghlan <ncoghlan@gmail.com>
parents: 86470
diff changeset
682 self.assertEqual(s, msg)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36770
diff changeset
683
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
684 def test_enter_result_is_target(self):
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
685 f = io.StringIO()
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
686 with redirect_stdout(f) as enter_result:
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
687 self.assertIs(enter_result, f)
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
688
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
689 def test_cm_is_reusable(self):
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
690 f = io.StringIO()
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
691 write_to_f = redirect_stdout(f)
86869
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
692 orig_stdout = sys.stdout
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
693 with write_to_f:
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
694 print("Hello", end=" ")
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
695 with write_to_f:
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
696 print("World!")
86869
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
697 self.assertIs(sys.stdout, orig_stdout)
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
698 s = f.getvalue()
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
699 self.assertEqual(s, "Hello World!\n")
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
700
86869
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
701 def test_cm_is_reentrant(self):
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
702 f = io.StringIO()
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
703 write_to_f = redirect_stdout(f)
86869
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
704 orig_stdout = sys.stdout
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
705 with write_to_f:
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
706 print("Hello", end=" ")
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
707 with write_to_f:
86869
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
708 print("World!")
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
709 self.assertIs(sys.stdout, orig_stdout)
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
710 s = f.getvalue()
87d49e2cdd34 Close #19403: make contextlib.redirect_stdout reentrant
Nick Coghlan <ncoghlan@gmail.com>
parents: 86656
diff changeset
711 self.assertEqual(s, "Hello World!\n")
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
712
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
713
86403
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
714 class TestSuppress(unittest.TestCase):
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
715
86656
a9bbc2d0c1dc Issue #19330: Handle the no-docstrings case in tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 86650
diff changeset
716 @support.requires_docstrings
86650
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
717 def test_instance_docs(self):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
718 # Issue 19330: ensure context manager instances have good docstrings
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
719 cm_docstring = suppress.__doc__
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
720 obj = suppress()
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
721 self.assertEqual(obj.__doc__, cm_docstring)
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
722
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
723 def test_no_result_from_enter(self):
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
724 with suppress(ValueError) as enter_result:
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
725 self.assertIsNone(enter_result)
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
726
86403
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
727 def test_no_exception(self):
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
728 with suppress(ValueError):
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
729 self.assertEqual(pow(2, 5), 32)
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
730
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
731 def test_exact_exception(self):
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
732 with suppress(TypeError):
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
733 len(5)
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
734
86650
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
735 def test_exception_hierarchy(self):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
736 with suppress(LookupError):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
737 'Hello'[50]
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
738
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
739 def test_other_exception(self):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
740 with self.assertRaises(ZeroDivisionError):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
741 with suppress(TypeError):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
742 1/0
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
743
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
744 def test_no_args(self):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
745 with self.assertRaises(ZeroDivisionError):
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
746 with suppress():
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
747 1/0
09153a9a3bb9 Close #19330 by using public classes in contextlib
Nick Coghlan <ncoghlan@gmail.com>
parents: 86645
diff changeset
748
86403
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
749 def test_multiple_exception_args(self):
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
750 with suppress(ZeroDivisionError, TypeError):
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
751 1/0
86403
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
752 with suppress(ZeroDivisionError, TypeError):
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
753 len(5)
22247b7d17fa Close #19266: contextlib.ignore -> contextlib.suppress
Nick Coghlan <ncoghlan@gmail.com>
parents: 86209
diff changeset
754
86470
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
755 def test_cm_is_reentrant(self):
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
756 ignore_exceptions = suppress(Exception)
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
757 with ignore_exceptions:
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
758 pass
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
759 with ignore_exceptions:
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
760 len(5)
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
761 with ignore_exceptions:
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
762 1/0
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
763 with ignore_exceptions: # Check nested usage
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
764 len(5)
124a7ba1e911 contextlib doc updates and refactoring
Nick Coghlan <ncoghlan@gmail.com>
parents: 86403
diff changeset
765
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 if __name__ == "__main__":
84108
05f7883ee92d Update various test modules to use unittest.main() for test discovery
Brett Cannon <brett@python.org>
parents: 82598
diff changeset
767 unittest.main()