|
1 | | -"""Unit tests for the with statement specified in PEP 343.""" |
| 1 | +"""Unit tests for the 'with/async with' statements specified in PEP 343/492.""" |
2 | 2 |
|
3 | 3 |
|
4 | 4 | __author__ = "Mike Bland" |
5 | 5 | __email__ = "mbland at acm dot org" |
6 | 6 |
|
| 7 | +import re |
7 | 8 | import sys |
8 | 9 | import traceback |
9 | 10 | import unittest |
10 | 11 | from collections import deque |
11 | 12 | from contextlib import _GeneratorContextManager, contextmanager, nullcontext |
12 | 13 |
|
13 | 14 |
|
| 15 | +def do_with(obj): |
| 16 | + with obj: |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +async def do_async_with(obj): |
| 21 | + async with obj: |
| 22 | + pass |
| 23 | + |
| 24 | + |
14 | 25 | class MockContextManager(_GeneratorContextManager): |
15 | 26 | def __init__(self, *args): |
16 | 27 | super().__init__(*args) |
@@ -110,34 +121,77 @@ def fooNotDeclared(): |
110 | 121 | with foo: pass |
111 | 122 | self.assertRaises(NameError, fooNotDeclared) |
112 | 123 |
|
113 | | - def testEnterAttributeError1(self): |
114 | | - class LacksEnter(object): |
115 | | - def __exit__(self, type, value, traceback): |
116 | | - pass |
117 | | - |
118 | | - def fooLacksEnter(): |
119 | | - foo = LacksEnter() |
120 | | - with foo: pass |
121 | | - self.assertRaisesRegex(TypeError, 'the context manager', fooLacksEnter) |
122 | | - |
123 | | - def testEnterAttributeError2(self): |
124 | | - class LacksEnterAndExit(object): |
125 | | - pass |
| 124 | + def testEnterAttributeError(self): |
| 125 | + class LacksEnter: |
| 126 | + def __exit__(self, type, value, traceback): ... |
126 | 127 |
|
127 | | - def fooLacksEnterAndExit(): |
128 | | - foo = LacksEnterAndExit() |
129 | | - with foo: pass |
130 | | - self.assertRaisesRegex(TypeError, 'the context manager', fooLacksEnterAndExit) |
| 128 | + with self.assertRaisesRegex(TypeError, re.escape(( |
| 129 | + "object does not support the context manager protocol " |
| 130 | + "(missed __enter__ method)" |
| 131 | + ))): |
| 132 | + do_with(LacksEnter()) |
131 | 133 |
|
132 | 134 | def testExitAttributeError(self): |
133 | | - class LacksExit(object): |
134 | | - def __enter__(self): |
135 | | - pass |
136 | | - |
137 | | - def fooLacksExit(): |
138 | | - foo = LacksExit() |
139 | | - with foo: pass |
140 | | - self.assertRaisesRegex(TypeError, 'the context manager.*__exit__', fooLacksExit) |
| 135 | + class LacksExit: |
| 136 | + def __enter__(self): ... |
| 137 | + |
| 138 | + msg = re.escape(( |
| 139 | + "object does not support the context manager protocol " |
| 140 | + "(missed __exit__ method)" |
| 141 | + )) |
| 142 | + # a missing __exit__ is reported missing before a missing __enter__ |
| 143 | + with self.assertRaisesRegex(TypeError, msg): |
| 144 | + do_with(object()) |
| 145 | + with self.assertRaisesRegex(TypeError, msg): |
| 146 | + do_with(LacksExit()) |
| 147 | + |
| 148 | + def testWithForAsyncManager(self): |
| 149 | + class AsyncManager: |
| 150 | + async def __aenter__(self): ... |
| 151 | + async def __aexit__(self, type, value, traceback): ... |
| 152 | + |
| 153 | + with self.assertRaisesRegex(TypeError, re.escape(( |
| 154 | + "object does not support the context manager protocol " |
| 155 | + "(missed __exit__ method) but it supports the asynchronous " |
| 156 | + "context manager protocol. Did you mean to use 'async with'?" |
| 157 | + ))): |
| 158 | + do_with(AsyncManager()) |
| 159 | + |
| 160 | + def testAsyncEnterAttributeError(self): |
| 161 | + class LacksAsyncEnter: |
| 162 | + async def __aexit__(self, type, value, traceback): ... |
| 163 | + |
| 164 | + with self.assertRaisesRegex(TypeError, re.escape(( |
| 165 | + "object does not support the asynchronous context manager protocol " |
| 166 | + "(missed __aenter__ method)" |
| 167 | + ))): |
| 168 | + do_async_with(LacksAsyncEnter()).send(None) |
| 169 | + |
| 170 | + def testAsyncExitAttributeError(self): |
| 171 | + class LacksAsyncExit: |
| 172 | + async def __aenter__(self): ... |
| 173 | + |
| 174 | + msg = re.escape(( |
| 175 | + "object does not support the asynchronous context manager protocol " |
| 176 | + "(missed __aexit__ method)" |
| 177 | + )) |
| 178 | + # a missing __aexit__ is reported missing before a missing __aenter__ |
| 179 | + with self.assertRaisesRegex(TypeError, msg): |
| 180 | + do_async_with(object()).send(None) |
| 181 | + with self.assertRaisesRegex(TypeError, msg): |
| 182 | + do_async_with(LacksAsyncExit()).send(None) |
| 183 | + |
| 184 | + def testAsyncWithForSyncManager(self): |
| 185 | + class SyncManager: |
| 186 | + def __enter__(self): ... |
| 187 | + def __exit__(self, type, value, traceback): ... |
| 188 | + |
| 189 | + with self.assertRaisesRegex(TypeError, re.escape(( |
| 190 | + "object does not support the asynchronous context manager protocol " |
| 191 | + "(missed __aexit__ method) but it supports the context manager " |
| 192 | + "protocol. Did you mean to use 'with'?" |
| 193 | + ))): |
| 194 | + do_async_with(SyncManager()).send(None) |
141 | 195 |
|
142 | 196 | def assertRaisesSyntaxError(self, codestr): |
143 | 197 | def shouldRaiseSyntaxError(s): |
@@ -190,6 +244,7 @@ def shouldThrow(): |
190 | 244 | pass |
191 | 245 | self.assertRaises(RuntimeError, shouldThrow) |
192 | 246 |
|
| 247 | + |
193 | 248 | class ContextmanagerAssertionMixin(object): |
194 | 249 |
|
195 | 250 | def setUp(self): |
|
0 commit comments