From 8110f2859787cbf611ea7bc508d5410374a4dbbb Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 10:55:35 +0100 Subject: [PATCH 01/10] gh-95882 fix traceback of exceptions propagated from inside a contextlib.asynccontextmanager --- Lib/contextlib.py | 3 +++ Lib/test/test_contextlib_async.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 625bb33b12d5fd1..c878afa341ef5e2 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -228,6 +228,7 @@ async def __aexit__(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 Stop(Async)Iteration exception # was passed to athrow() and later wrapped into a RuntimeError @@ -239,6 +240,7 @@ async def __aexit__(self, typ, value, traceback): isinstance(value, (StopIteration, StopAsyncIteration)) and exc.__cause__ is value ): + exc.__traceback__ = traceback return False raise except BaseException as exc: @@ -250,6 +252,7 @@ async def __aexit__(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 athrow()") diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index b64673d2c31e056..ac1c92639505f32 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -125,6 +125,33 @@ async def woohoo(): raise ZeroDivisionError() self.assertEqual(state, [1, 42, 999]) + @_async_test + async def test_contextmanager_traceback(self): + @asynccontextmanager + async def f(): + yield + + try: + async 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: + async 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)') + @_async_test async def test_contextmanager_no_reraise(self): @asynccontextmanager From 7f448d8b5fc2ee04a79fab21b0772bc9dbcbc745 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 11 Aug 2022 10:02:22 +0000 Subject: [PATCH 02/10] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst diff --git a/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst b/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst new file mode 100644 index 000000000000000..636648b55b6c2dc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst @@ -0,0 +1 @@ +Fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks. From 25d3ee0815c2ffd0a91d722b57ceb950f02a1fcf Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 12:40:40 +0100 Subject: [PATCH 03/10] Update test_contextlib_async.py --- Lib/test/test_contextlib_async.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index ac1c92639505f32..e024835b6460f3b 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -5,6 +5,7 @@ import functools from test import support import unittest +import traceback from test.test_contextlib import TestBaseExitStack From adf2bf3cc554b7c841edce47bb023bbc89d50908 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 14:52:52 +0100 Subject: [PATCH 04/10] test for the Stop(Async)Iteration case --- Lib/test/test_contextlib.py | 32 +++++++++++++++++++++++--- Lib/test/test_contextlib_async.py | 37 ++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 31f5c74572b630c..a90be5b48ab2bc0 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -104,15 +104,41 @@ def f(): self.assertEqual(frames[0].line, '1/0') # Repeat with RuntimeError (which goes through a different code path) + class RuntimeErrorSubclass(RuntimeError): + pass + try: with f(): - raise NotImplementedError(42) - except NotImplementedError as e: + raise RuntimeErrorSubclass(42) + except RuntimeErrorSubclass 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)') + self.assertEqual(frames[0].line, 'raise RuntimeErrorSubclass(42)') + + class StopIterationSubclass(StopIteration): + pass + + for stop_exc in ( + StopIteration('spam'), + StopIterationSubclass('spam'), + ): + with self.subTest(type=type(stop_exc)): + try: + with f(): + raise stop_exc + except type(stop_exc) as e: + self.assertIs(e, stop_exc) + frames = traceback.extract_tb(e.__traceback__) + else: + self.fail(f'{stop_exc} was suppressed') + + self.assertEqual(len(frames), 2) + self.assertEqual(frames[0].name, 'f') + self.assertEqual(frames[0].line, 'yield') + self.assertEqual(frames[1].name, 'test_contextmanager_traceback') + self.assertEqual(frames[1].line, 'raise stop_exc') def test_contextmanager_no_reraise(self): @contextmanager diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index e024835b6460f3b..ad2cc94b8f860b0 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -143,15 +143,46 @@ async def f(): self.assertEqual(frames[0].line, '1/0') # Repeat with RuntimeError (which goes through a different code path) + class RuntimeErrorSubclass(RuntimeError): + pass + try: async with f(): - raise NotImplementedError(42) - except NotImplementedError as e: + raise RuntimeErrorSubclass(42) + except RuntimeErrorSubclass 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)') + self.assertEqual(frames[0].line, 'raise RuntimeErrorSubclass(42)') + + class StopIterationSubclass(StopIteration): + pass + + class StopAsyncIterationSubclass(StopAsyncIteration): + pass + + for stop_exc in ( + StopIteration('spam'), + StopAsyncIteration('ham'), + StopIterationSubclass('spam'), + StopAsyncIterationSubclass('spam') + ): + with self.subTest(type=type(stop_exc)): + try: + async with f(): + raise stop_exc + except type(stop_exc) as e: + self.assertIs(e, stop_exc) + frames = traceback.extract_tb(e.__traceback__) + else: + self.fail(f'{stop_exc} was suppressed') + + self.assertEqual(len(frames), 2) + self.assertEqual(frames[0].name, 'f') + self.assertEqual(frames[0].line, 'yield') + self.assertEqual(frames[1].name, 'test_contextmanager_traceback') + self.assertEqual(frames[1].line, 'raise stop_exc') @_async_test async def test_contextmanager_no_reraise(self): From 0c9ef8bbc36c59aeccfef06dad72d217ea13ec68 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 15:17:19 +0100 Subject: [PATCH 05/10] Update Lib/contextlib.py --- Lib/contextlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/contextlib.py b/Lib/contextlib.py index c878afa341ef5e2..a13c9f421c50ea0 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -240,7 +240,7 @@ async def __aexit__(self, typ, value, traceback): isinstance(value, (StopIteration, StopAsyncIteration)) and exc.__cause__ is value ): - exc.__traceback__ = traceback + value.__traceback__ = traceback return False raise except BaseException as exc: From cf3bcad196ccc046543dc62808b04ea3a85bf4b5 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 15:18:58 +0100 Subject: [PATCH 06/10] get rid of that extra yield frame from the traceback --- Lib/test/test_contextlib.py | 8 +++----- Lib/test/test_contextlib_async.py | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index a90be5b48ab2bc0..ec06785b5667a6c 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -134,11 +134,9 @@ class StopIterationSubclass(StopIteration): else: self.fail(f'{stop_exc} was suppressed') - self.assertEqual(len(frames), 2) - self.assertEqual(frames[0].name, 'f') - self.assertEqual(frames[0].line, 'yield') - self.assertEqual(frames[1].name, 'test_contextmanager_traceback') - self.assertEqual(frames[1].line, 'raise stop_exc') + self.assertEqual(len(frames), 1) + self.assertEqual(frames[0].name, 'test_contextmanager_traceback') + self.assertEqual(frames[0].line, 'raise stop_exc') def test_contextmanager_no_reraise(self): @contextmanager diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index ad2cc94b8f860b0..3d43ed0fcab1686 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -178,11 +178,9 @@ class StopAsyncIterationSubclass(StopAsyncIteration): else: self.fail(f'{stop_exc} was suppressed') - self.assertEqual(len(frames), 2) - self.assertEqual(frames[0].name, 'f') - self.assertEqual(frames[0].line, 'yield') - self.assertEqual(frames[1].name, 'test_contextmanager_traceback') - self.assertEqual(frames[1].line, 'raise stop_exc') + self.assertEqual(len(frames), 1) + self.assertEqual(frames[0].name, 'test_contextmanager_traceback') + self.assertEqual(frames[0].line, 'raise stop_exc') @_async_test async def test_contextmanager_no_reraise(self): From d448aaf671b5a6f9be794d8945a9d125e62b5f2a Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 15:21:29 +0100 Subject: [PATCH 07/10] and fix it for contextlib.contextmanager --- Lib/contextlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/contextlib.py b/Lib/contextlib.py index a13c9f421c50ea0..58e9a498878d01b 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -173,7 +173,7 @@ def __exit__(self, typ, value, traceback): isinstance(value, StopIteration) and exc.__cause__ is value ): - exc.__traceback__ = traceback + value.__traceback__ = traceback return False raise except BaseException as exc: From 7904e4bd3afbf9a301377f193643f536355d6a45 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 15:23:11 +0100 Subject: [PATCH 08/10] Update Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst --- .../next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst b/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst index 636648b55b6c2dc..e2468b022ce757c 100644 --- a/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst +++ b/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst @@ -1 +1 @@ -Fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks. +Fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks and fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks for :exc:`StopIteration` From b6c1f6eb8cacea583b82228da4cb36aff0aca545 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 15:30:20 +0100 Subject: [PATCH 09/10] this PR gets the ACK --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index c1f570acaaf8e04..d7463a393aeaaa6 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -643,6 +643,7 @@ Hans de Graaff Tim Graham Kim Gräsman Alex Grönholm +Thomas Grainger Nathaniel Gray Eddy De Greef Duane Griffin From d8800cd4ea2054f982d2155eddc871c416544795 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 11 Aug 2022 15:40:16 +0100 Subject: [PATCH 10/10] Update Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst --- .../next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst b/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst index e2468b022ce757c..9cdb237d5c8737f 100644 --- a/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst +++ b/Misc/NEWS.d/next/Library/2022-08-11-10-02-19.gh-issue-95882.FsUr72.rst @@ -1 +1 @@ -Fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks and fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks for :exc:`StopIteration` +Fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks and fix a 3.11 regression in :func:`~contextlib.contextmanager`, which caused it to propagate exceptions with incorrect tracebacks for :exc:`StopIteration`.