]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-92118: fix traceback of exceptions propagated from inside a contextlib.contextmana...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Wed, 4 May 2022 18:40:47 +0000 (14:40 -0400)
committerGitHub <noreply@github.com>
Wed, 4 May 2022 18:40:47 +0000 (19:40 +0100)
Lib/contextlib.py
Lib/test/test_contextlib.py
Misc/NEWS.d/next/Library/2022-05-02-23-08-02.gh-issue-92118.9Mm9g4.rst [new file with mode: 0644]

index 4cff9c6a4611788ef799982848a9748a1ebd97d9..625bb33b12d5fd17638934215fbe8b7cd0bffe2d 100644 (file)
@@ -161,6 +161,7 @@ class _GeneratorContextManager(
             except RuntimeError as exc:
                 # Don't re-raise the passed in exception. (issue27122)
                 if exc is value:
+                    exc.__traceback__ = traceback
                     return False
                 # Avoid suppressing if a StopIteration exception
                 # was passed to throw() and later wrapped into a RuntimeError
@@ -172,6 +173,7 @@ class _GeneratorContextManager(
                     isinstance(value, StopIteration)
                     and exc.__cause__ is value
                 ):
+                    exc.__traceback__ = traceback
                     return False
                 raise
             except BaseException as exc:
@@ -183,6 +185,7 @@ class _GeneratorContextManager(
                 # and the __exit__() protocol.
                 if exc is not value:
                     raise
+                exc.__traceback__ = traceback
                 return False
             raise RuntimeError("generator didn't stop after throw()")
 
index e238548be9e2be8f965b8d12574a86858c65e23c..bfe81173ddc0b4e50ac5937985de2f45b4c254bc 100644 (file)
@@ -5,6 +5,7 @@ import os
 import sys
 import tempfile
 import threading
+import traceback
 import unittest
 from contextlib import *  # Tests __all__
 from test import support
@@ -87,6 +88,32 @@ class ContextManagerTestCase(unittest.TestCase):
                 raise ZeroDivisionError()
         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):
         @contextmanager
         def whee():
diff --git a/Misc/NEWS.d/next/Library/2022-05-02-23-08-02.gh-issue-92118.9Mm9g4.rst b/Misc/NEWS.d/next/Library/2022-05-02-23-08-02.gh-issue-92118.9Mm9g4.rst
new file mode 100644 (file)
index 0000000..b58ecdf
--- /dev/null
@@ -0,0 +1 @@
+Fix a 3.11 regression in :func:`~contextlib.contextmanager`, which caused it to propagate exceptions with incorrect tracebacks.