From fca6e8ef05fa14d8132f447387b95905cddc10c7 Mon Sep 17 00:00:00 2001 From: iritkatriel Date: Mon, 2 May 2022 23:44:43 +0100 Subject: [PATCH 1/4] gh-92118: fix traceback of exceptions propagated from inside a contextlib.contextmanager --- Lib/contextlib.py | 1 + Lib/test/test_contextlib.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 4cff9c6a461178..5ef81248b7389f 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -183,6 +183,7 @@ def __exit__(self, typ, value, traceback): # and the __exit__() protocol. if exc is not value: raise + exc.__traceback__ = traceback return False raise RuntimeError("generator didn't stop after throw()") diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index e238548be9e2be..3445f5d60e9d37 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -5,6 +5,7 @@ import sys import tempfile import threading +import traceback import unittest from contextlib import * # Tests __all__ from test import support @@ -87,6 +88,24 @@ def woohoo(): raise ZeroDivisionError() self.assertEqual(state, [1, 42, 999]) + def test_contextmanager_traceback(self): + @contextmanager + def f(): + try: + yield + finally: + pass + + 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') + def test_contextmanager_no_reraise(self): @contextmanager def whee(): From c67d0f251cda5373d2f0f2ed6b87f933cd315379 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 2 May 2022 23:08:04 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-05-02-23-08-02.gh-issue-92118.9Mm9g4.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-05-02-23-08-02.gh-issue-92118.9Mm9g4.rst 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 index 00000000000000..b58ecdf40daf34 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-02-23-08-02.gh-issue-92118.9Mm9g4.rst @@ -0,0 +1 @@ +Fix a 3.11 regression in :func:`~contextlib.contextmanager`, which caused it to propagate exceptions with incorrect tracebacks. From b7b47f3283b0da5eb275b22c5eb686814754f131 Mon Sep 17 00:00:00 2001 From: iritkatriel Date: Tue, 3 May 2022 00:35:12 +0100 Subject: [PATCH 3/4] handle the RuntimeError case in the same way --- Lib/contextlib.py | 2 ++ Lib/test/test_contextlib.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 5ef81248b7389f..625bb33b12d5fd 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -161,6 +161,7 @@ def __exit__(self, typ, value, traceback): 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 @@ def __exit__(self, typ, value, traceback): isinstance(value, StopIteration) and exc.__cause__ is value ): + exc.__traceback__ = traceback return False raise except BaseException as exc: diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 3445f5d60e9d37..4edeea09d9c5a0 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -106,6 +106,16 @@ def f(): self.assertEqual(frames[0].name, 'test_contextmanager_traceback') self.assertEqual(frames[0].line, '1/0') + 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(): From ed5887cbd4d2057bcd91975b4fc4346844b5dee4 Mon Sep 17 00:00:00 2001 From: iritkatriel Date: Tue, 3 May 2022 16:20:44 +0100 Subject: [PATCH 4/4] update test re serhiy's review comments --- Lib/test/test_contextlib.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 4edeea09d9c5a0..bfe81173ddc0b4 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -91,10 +91,7 @@ def woohoo(): def test_contextmanager_traceback(self): @contextmanager def f(): - try: - yield - finally: - pass + yield try: with f(): @@ -106,6 +103,7 @@ def f(): 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)