Skip to content

Commit 69dc81f

Browse files
committed
move tests
1 parent db1fcfc commit 69dc81f

File tree

2 files changed

+81
-54
lines changed

2 files changed

+81
-54
lines changed

Lib/test/test_compile.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,6 @@
2626
from test.support.os_helper import FakePath
2727

2828

29-
class DummyEnter:
30-
def __enter__(self, *args, **kwargs):
31-
pass
32-
33-
34-
class DummyExit:
35-
def __exit__(self, *args, **kwargs):
36-
pass
37-
38-
39-
class SyncDummy(DummyEnter, DummyExit):
40-
pass
41-
42-
43-
class AsyncDummyEnter:
44-
async def __aenter__(self, *args, **kwargs):
45-
pass
46-
47-
48-
class AsyncDummyExit:
49-
async def __aexit__(self, *args, **kwargs):
50-
pass
51-
52-
53-
class AsyncDummy(AsyncDummyEnter, AsyncDummyExit):
54-
pass
55-
56-
5729
class TestSpecifics(unittest.TestCase):
5830

5931
def compile_single(self, source):

Lib/test/test_with.py

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
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."""
22

33

44
__author__ = "Mike Bland"
55
__email__ = "mbland at acm dot org"
66

7+
import re
78
import sys
89
import traceback
910
import unittest
1011
from collections import deque
1112
from contextlib import _GeneratorContextManager, contextmanager, nullcontext
1213

1314

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+
1425
class MockContextManager(_GeneratorContextManager):
1526
def __init__(self, *args):
1627
super().__init__(*args)
@@ -110,34 +121,77 @@ def fooNotDeclared():
110121
with foo: pass
111122
self.assertRaises(NameError, fooNotDeclared)
112123

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): ...
126127

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())
131133

132134
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)
141195

142196
def assertRaisesSyntaxError(self, codestr):
143197
def shouldRaiseSyntaxError(s):
@@ -190,6 +244,7 @@ def shouldThrow():
190244
pass
191245
self.assertRaises(RuntimeError, shouldThrow)
192246

247+
193248
class ContextmanagerAssertionMixin(object):
194249

195250
def setUp(self):

0 commit comments

Comments
 (0)