mirror of
https://github.com/python/cpython.git
synced 2025-10-08 16:11:51 +00:00
gh-92118: fix traceback of exceptions propagated from inside a contextlib.contextmanager (GH-92202)
This commit is contained in:
parent
f8a2fab212
commit
e61330b44f
3 changed files with 31 additions and 0 deletions
|
@ -161,6 +161,7 @@ class _GeneratorContextManager(
|
||||||
except RuntimeError as exc:
|
except RuntimeError as exc:
|
||||||
# Don't re-raise the passed in exception. (issue27122)
|
# Don't re-raise the passed in exception. (issue27122)
|
||||||
if exc is value:
|
if exc is value:
|
||||||
|
exc.__traceback__ = traceback
|
||||||
return False
|
return False
|
||||||
# Avoid suppressing if a StopIteration exception
|
# Avoid suppressing if a StopIteration exception
|
||||||
# was passed to throw() and later wrapped into a RuntimeError
|
# was passed to throw() and later wrapped into a RuntimeError
|
||||||
|
@ -172,6 +173,7 @@ class _GeneratorContextManager(
|
||||||
isinstance(value, StopIteration)
|
isinstance(value, StopIteration)
|
||||||
and exc.__cause__ is value
|
and exc.__cause__ is value
|
||||||
):
|
):
|
||||||
|
exc.__traceback__ = traceback
|
||||||
return False
|
return False
|
||||||
raise
|
raise
|
||||||
except BaseException as exc:
|
except BaseException as exc:
|
||||||
|
@ -183,6 +185,7 @@ class _GeneratorContextManager(
|
||||||
# and the __exit__() protocol.
|
# and the __exit__() protocol.
|
||||||
if exc is not value:
|
if exc is not value:
|
||||||
raise
|
raise
|
||||||
|
exc.__traceback__ = traceback
|
||||||
return False
|
return False
|
||||||
raise RuntimeError("generator didn't stop after throw()")
|
raise RuntimeError("generator didn't stop after throw()")
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import threading
|
import threading
|
||||||
|
import traceback
|
||||||
import unittest
|
import unittest
|
||||||
from contextlib import * # Tests __all__
|
from contextlib import * # Tests __all__
|
||||||
from test import support
|
from test import support
|
||||||
|
@ -87,6 +88,32 @@ class ContextManagerTestCase(unittest.TestCase):
|
||||||
raise ZeroDivisionError()
|
raise ZeroDivisionError()
|
||||||
self.assertEqual(state, [1, 42, 999])
|
self.assertEqual(state, [1, 42, 999])
|
||||||
|
|
||||||
|
def test_contextmanager_traceback(self):
|
||||||
|
@contextmanager
|
||||||
|
def f():
|
||||||
|
yield
|
||||||
|
|
||||||
|
try:
|
||||||
|
with f():
|
||||||
|
1/0
|
||||||
|
except ZeroDivisionError as e:
|
||||||
|
frames = traceback.extract_tb(e.__traceback__)
|
||||||
|
|
||||||
|
self.assertEqual(len(frames), 1)
|
||||||
|
self.assertEqual(frames[0].name, 'test_contextmanager_traceback')
|
||||||
|
self.assertEqual(frames[0].line, '1/0')
|
||||||
|
|
||||||
|
# Repeat with RuntimeError (which goes through a different code path)
|
||||||
|
try:
|
||||||
|
with f():
|
||||||
|
raise NotImplementedError(42)
|
||||||
|
except NotImplementedError as e:
|
||||||
|
frames = traceback.extract_tb(e.__traceback__)
|
||||||
|
|
||||||
|
self.assertEqual(len(frames), 1)
|
||||||
|
self.assertEqual(frames[0].name, 'test_contextmanager_traceback')
|
||||||
|
self.assertEqual(frames[0].line, 'raise NotImplementedError(42)')
|
||||||
|
|
||||||
def test_contextmanager_no_reraise(self):
|
def test_contextmanager_no_reraise(self):
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def whee():
|
def whee():
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix a 3.11 regression in :func:`~contextlib.contextmanager`, which caused it to propagate exceptions with incorrect tracebacks.
|
Loading…
Add table
Add a link
Reference in a new issue