Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 322f79f

Browse filesBrowse files
[3.12] Synchronize test_contextlib with test_contextlib_async (GH-111000) (GH-111114)
(cherry picked from commit ff4e53c) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent f6cde99 commit 322f79f
Copy full SHA for 322f79f

File tree

Expand file treeCollapse file tree

1 file changed

+46
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+46
-0
lines changed

‎Lib/test/test_contextlib.py

Copy file name to clipboardExpand all lines: Lib/test/test_contextlib.py
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ def whoo():
163163
# The "gen" attribute is an implementation detail.
164164
self.assertFalse(ctx.gen.gi_suspended)
165165

166+
def test_contextmanager_trap_no_yield(self):
167+
@contextmanager
168+
def whoo():
169+
if False:
170+
yield
171+
ctx = whoo()
172+
with self.assertRaises(RuntimeError):
173+
ctx.__enter__()
174+
166175
def test_contextmanager_trap_second_yield(self):
167176
@contextmanager
168177
def whoo():
@@ -176,6 +185,19 @@ def whoo():
176185
# The "gen" attribute is an implementation detail.
177186
self.assertFalse(ctx.gen.gi_suspended)
178187

188+
def test_contextmanager_non_normalised(self):
189+
@contextmanager
190+
def whoo():
191+
try:
192+
yield
193+
except RuntimeError:
194+
raise SyntaxError
195+
196+
ctx = whoo()
197+
ctx.__enter__()
198+
with self.assertRaises(SyntaxError):
199+
ctx.__exit__(RuntimeError, None, None)
200+
179201
def test_contextmanager_except(self):
180202
state = []
181203
@contextmanager
@@ -255,6 +277,25 @@ def test_issue29692():
255277
self.assertEqual(ex.args[0], 'issue29692:Unchained')
256278
self.assertIsNone(ex.__cause__)
257279

280+
def test_contextmanager_wrap_runtimeerror(self):
281+
@contextmanager
282+
def woohoo():
283+
try:
284+
yield
285+
except Exception as exc:
286+
raise RuntimeError(f'caught {exc}') from exc
287+
288+
with self.assertRaises(RuntimeError):
289+
with woohoo():
290+
1 / 0
291+
292+
# If the context manager wrapped StopIteration in a RuntimeError,
293+
# we also unwrap it, because we can't tell whether the wrapping was
294+
# done by the generator machinery or by the generator itself.
295+
with self.assertRaises(StopIteration):
296+
with woohoo():
297+
raise StopIteration
298+
258299
def _create_contextmanager_attribs(self):
259300
def attribs(**kw):
260301
def decorate(func):
@@ -266,6 +307,7 @@ def decorate(func):
266307
@attribs(foo='bar')
267308
def baz(spam):
268309
"""Whee!"""
310+
yield
269311
return baz
270312

271313
def test_contextmanager_attribs(self):
@@ -322,8 +364,11 @@ def woohoo(a, *, b):
322364

323365
def test_recursive(self):
324366
depth = 0
367+
ncols = 0
325368
@contextmanager
326369
def woohoo():
370+
nonlocal ncols
371+
ncols += 1
327372
nonlocal depth
328373
before = depth
329374
depth += 1
@@ -337,6 +382,7 @@ def recursive():
337382
recursive()
338383

339384
recursive()
385+
self.assertEqual(ncols, 10)
340386
self.assertEqual(depth, 0)
341387

342388

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.