From b388f9a2c3637507f07fbeb403c02f2596d41216 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Tue, 30 Aug 2022 15:08:03 +0800 Subject: [PATCH 01/27] feat: Deprecate gen.throw(typ, val, tb) --- Objects/genobject.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index da4afecc69c8c1..dcbcc51f387d69 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -418,7 +418,9 @@ PyDoc_STRVAR(throw_doc, throw(type[,value[,tb]])\n\ \n\ Raise exception in generator, return next yielded value or raise\n\ -StopIteration."); +StopIteration.\n\ +the (type, val, tb) exception representation is deprecated, \n\ +and may be removed in a future version of Python."); static PyObject * _gen_throw(PyGenObject *gen, int close_on_genexit, @@ -559,6 +561,14 @@ gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs) if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) { return NULL; } + if (nargs > 1) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "the (type, val, tb) exception representation" + "is deprecated, and may be removed in a future version of Python.", + 1) < 0) { + return NULL; + } + } typ = args[0]; if (nargs == 3) { val = args[1]; @@ -1147,7 +1157,10 @@ PyDoc_STRVAR(coro_throw_doc, throw(type[,value[,traceback]])\n\ \n\ Raise exception in coroutine, return next iterated value or raise\n\ -StopIteration."); +StopIteration.\n\ +the (type, val, tb) exception representation is deprecated, \n\ +and may be removed in a future version of Python."); + PyDoc_STRVAR(coro_close_doc, "close() -> raise GeneratorExit inside coroutine."); From 713d77a2ad4a0a6823bdd6b317fa718ab0fd8630 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Wed, 31 Aug 2022 18:39:55 +0800 Subject: [PATCH 02/27] Update Objects/genobject.c Co-authored-by: Guido van Rossum --- Objects/genobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index dcbcc51f387d69..edad2ec131287a 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -563,8 +563,8 @@ gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs) } if (nargs > 1) { if (PyErr_WarnEx(PyExc_DeprecationWarning, - "the (type, val, tb) exception representation" - "is deprecated, and may be removed in a future version of Python.", + "the (type, val, tb) exception representation is deprecated, " + "and may be removed in a future version of Python.", 1) < 0) { return NULL; } From fcfb65e368d34915aea23fbf151da6d732626a05 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Thu, 1 Sep 2022 13:25:54 +0800 Subject: [PATCH 03/27] fix clear cases --- Lib/contextlib.py | 4 ++-- Lib/test/test_asyncgen.py | 14 +++++++------- Lib/test/test_coroutines.py | 2 +- Lib/test/test_types.py | 2 +- .../2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst | 1 + 5 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 625bb33b12d5fd..d5822219b3e25b 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -152,7 +152,7 @@ def __exit__(self, typ, value, traceback): # tell if we get the same exception back value = typ() try: - self.gen.throw(typ, value, traceback) + self.gen.throw(value) except StopIteration as exc: # Suppress StopIteration *unless* it's the same exception that # was passed to throw(). This prevents a StopIteration @@ -219,7 +219,7 @@ async def __aexit__(self, typ, value, traceback): # tell if we get the same exception back value = typ() try: - await self.gen.athrow(typ, value, traceback) + await self.gen.athrow(value) except StopAsyncIteration as exc: # Suppress StopIteration *unless* it's the same exception that # was passed to throw(). This prevents a StopIteration diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index fb22f411c2e296..423a9f711d2873 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -650,7 +650,7 @@ def test1(anext): agen = agenfn() with contextlib.closing(anext(agen, "default").__await__()) as g: self.assertEqual(g.send(None), 1) - self.assertEqual(g.throw(MyError, MyError(), None), 2) + self.assertEqual(g.throw(MyError()), 2) try: g.send(None) except StopIteration as e: @@ -663,9 +663,9 @@ def test2(anext): agen = agenfn() with contextlib.closing(anext(agen, "default").__await__()) as g: self.assertEqual(g.send(None), 1) - self.assertEqual(g.throw(MyError, MyError(), None), 2) + self.assertEqual(g.throw(MyError()), 2) with self.assertRaises(MyError): - g.throw(MyError, MyError(), None) + g.throw(MyError()) def test3(anext): agen = agenfn() @@ -692,9 +692,9 @@ async def agenfn(): agen = agenfn() with contextlib.closing(anext(agen, "default").__await__()) as g: self.assertEqual(g.send(None), 10) - self.assertEqual(g.throw(MyError, MyError(), None), 20) + self.assertEqual(g.throw(MyError()), 20) with self.assertRaisesRegex(MyError, 'val'): - g.throw(MyError, MyError('val'), None) + g.throw(MyError('val')) def test5(anext): @types.coroutine @@ -713,7 +713,7 @@ async def agenfn(): with contextlib.closing(anext(agen, "default").__await__()) as g: self.assertEqual(g.send(None), 10) with self.assertRaisesRegex(StopIteration, 'default'): - g.throw(MyError, MyError(), None) + g.throw(MyError()) def test6(anext): @types.coroutine @@ -728,7 +728,7 @@ async def agenfn(): agen = agenfn() with contextlib.closing(anext(agen, "default").__await__()) as g: with self.assertRaises(MyError): - g.throw(MyError, MyError(), None) + g.throw(MyError()) def run_test(test): with self.subTest('pure-Python anext()'): diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 8fff2d47c10fd5..4df8730157a8bf 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -709,7 +709,7 @@ async def foo(): aw = coro.__await__() next(aw) with self.assertRaises(ZeroDivisionError): - aw.throw(ZeroDivisionError, None, None) + aw.throw(ZeroDivisionError()) self.assertEqual(N, 102) def test_func_11(self): diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index f00da0a758d46f..af095632a36fcb 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -2072,7 +2072,7 @@ def foo(): return gen wrapper = foo() wrapper.send(None) with self.assertRaisesRegex(Exception, 'ham'): - wrapper.throw(Exception, Exception('ham')) + wrapper.throw(Exception('ham')) # decorate foo second time foo = types.coroutine(foo) diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst b/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst new file mode 100644 index 00000000000000..d0ec6180361ce6 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst @@ -0,0 +1 @@ +Deprecate ``gen.throw(typ, val, tb)`` From 073aa6278a64a15294a640ac5342572deb0edd91 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Sat, 3 Sep 2022 13:46:13 +0800 Subject: [PATCH 04/27] Update Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- .../2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst b/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst index d0ec6180361ce6..f51ec42d75f4ef 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst @@ -1 +1 @@ -Deprecate ``gen.throw(typ, val, tb)`` +Emit a DeprecationWarning when :meth:`~generator.throw` or :meth:`~coroutine.throw` are called with more than one argument. From 1c6ed30de1393704ceb22f1731cb4630f7fd0a2f Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Sat, 3 Sep 2022 17:01:50 +0800 Subject: [PATCH 05/27] Filter remaining warnings. --- Lib/test/test_asyncio/test_futures.py | 11 +++++++---- Lib/test/test_generators.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index f4a46ec90a16fe..7e3ce02d6adb49 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -10,6 +10,7 @@ from types import GenericAlias import asyncio from asyncio import futures +import warnings from test.test_asyncio import utils as test_utils from test import support @@ -619,10 +620,12 @@ def test_future_stop_iteration_args(self): def test_future_iter_throw(self): fut = self._new_future(loop=self.loop) fi = iter(fut) - self.assertRaises(TypeError, fi.throw, - Exception, Exception("elephant"), 32) - self.assertRaises(TypeError, fi.throw, - Exception("elephant"), Exception("elephant")) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + self.assertRaises(TypeError, fi.throw, + Exception, Exception("elephant"), 32) + self.assertRaises(TypeError, fi.throw, + Exception("elephant"), Exception("elephant")) self.assertRaises(TypeError, fi.throw, list) def test_future_del_collect(self): diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index e5aa7da1e0df13..46c8e3d31ba6ad 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -2113,6 +2113,13 @@ def printsolution(self, x): >>> g.throw(ValueError("xyz")) # value only caught ValueError (xyz) +>>> import warnings +>>> warnings.filterwarnings("ignore", category=DeprecationWarning) + +# Filter DeprecationWarning: the (type, val, tb) exception representation is deprecated, +# and may be removed in a future version of Python. +# Will re-enable it soon. + >>> g.throw(ValueError, ValueError(1)) # value+matching type caught ValueError (1) @@ -2181,6 +2188,12 @@ def printsolution(self, x): ... ValueError: 7 +>>> warnings.filters.pop(0) +('ignore', None, , None, 0) + +# Re-enable DeprecationWarning: the (type, val, tb) exception representation is deprecated, +# and may be removed in a future version of Python. + Plain "raise" inside a generator should preserve the traceback (#13188). The traceback should have 3 levels: - g.throw() From 488185517207c08b187cc15b2311e47adf9dceb5 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Tue, 6 Sep 2022 23:38:50 +0800 Subject: [PATCH 06/27] Add deprecated info in documentation. --- Doc/reference/expressions.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 6bf21a7dde49a0..4dd97ff9bda249 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -582,6 +582,11 @@ is already executing raises a :exc:`ValueError` exception. :attr:`~BaseException.__traceback__` attribute stored in *value* may be cleared. + .. deprecated:: 3.12 + + The second signature \(type\[, value\[, traceback\]\]\) is deprecated now. + It is still available, but may be removed in a future version of Python. + .. index:: exception: GeneratorExit From 01588161bdbcc634bfc999956232d7c337003281 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Wed, 7 Sep 2022 09:47:39 +0800 Subject: [PATCH 07/27] Use versionchanged instead of deprecated. --- Doc/reference/expressions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 4dd97ff9bda249..84d7270ea0950d 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -582,7 +582,7 @@ is already executing raises a :exc:`ValueError` exception. :attr:`~BaseException.__traceback__` attribute stored in *value* may be cleared. - .. deprecated:: 3.12 + .. versionchanged:: 3.12 The second signature \(type\[, value\[, traceback\]\]\) is deprecated now. It is still available, but may be removed in a future version of Python. From 8152b7ce848e82d8813f2646856b4c90efe2bf3b Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Fri, 9 Sep 2022 12:43:31 +0800 Subject: [PATCH 08/27] Update Doc/reference/expressions.rst Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Doc/reference/expressions.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 84d7270ea0950d..df1eaeae2ade22 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -584,8 +584,8 @@ is already executing raises a :exc:`ValueError` exception. .. versionchanged:: 3.12 - The second signature \(type\[, value\[, traceback\]\]\) is deprecated now. - It is still available, but may be removed in a future version of Python. + The second signature \(type\[, value\[, traceback\]\]\) is deprecated and + may be removed in a future version of Python. .. index:: exception: GeneratorExit From 670d8f8a188eb446ec85e304ab0ec6aa5ba556bb Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Fri, 9 Sep 2022 13:34:04 +0800 Subject: [PATCH 09/27] more docs and what's new entry --- Doc/reference/datamodel.rst | 5 +++++ Doc/reference/expressions.rst | 8 +++++++- Doc/whatsnew/3.12.rst | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 758f3aef3ee34d..c93269ab04b64f 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -2996,6 +2996,11 @@ generators, coroutines do not directly support iteration. above. If the exception is not caught in the coroutine, it propagates back to the caller. + .. versionchanged:: 3.12 + + The second signature \(type\[, value\[, traceback\]\]\) is deprecated and + may be removed in a future version of Python. + .. method:: coroutine.close() Causes the coroutine to clean itself up and exit. If the coroutine diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index df1eaeae2ade22..c5e05d225f1c4e 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -743,7 +743,8 @@ which are used to control the execution of a generator function. because there is no yield expression that could receive the value. -.. coroutinemethod:: agen.athrow(type[, value[, traceback]]) +.. coroutinemethod:: agen.athrow(value) + agen.athrow(type[, value[, traceback]]) Returns an awaitable that raises an exception of type ``type`` at the point where the asynchronous generator was paused, and returns the next value @@ -755,6 +756,11 @@ which are used to control the execution of a generator function. raises a different exception, then when the awaitable is run that exception propagates to the caller of the awaitable. + .. versionchanged:: 3.12 + + The second signature \(type\[, value\[, traceback\]\]\) is deprecated and + may be removed in a future version of Python. + .. index:: exception: GeneratorExit diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index f9fa8ac3123198..7cfcd85dedea80 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -159,6 +159,8 @@ Deprecated and tailor them to your needs. (Contributed by Erlend E. Aasland in :gh:`90016`.) +* The exception representation \(type\[, value\[, traceback\]\]\) is now deprecated. (:gh:`89874`.) + Pending Removal in Python 3.13 ------------------------------ From 0c20b3553bc99eb24803ed6c8dd91dd700dfa7a4 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Sat, 10 Sep 2022 13:06:49 +0800 Subject: [PATCH 10/27] Edit what's new. --- Doc/whatsnew/3.12.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 7cfcd85dedea80..bef58112f1b268 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -159,7 +159,7 @@ Deprecated and tailor them to your needs. (Contributed by Erlend E. Aasland in :gh:`90016`.) -* The exception representation \(type\[, value\[, traceback\]\]\) is now deprecated. (:gh:`89874`.) +* The throw\(type, value, traceback\) signature is now deprecated. (:gh:`89874`.) Pending Removal in Python 3.13 From ba9a87cb072402223e64014ef0d96f5680c7165a Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Tue, 13 Sep 2022 10:04:58 +0800 Subject: [PATCH 11/27] Update Objects/genobject.c Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Objects/genobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index edad2ec131287a..e425ebc8408a7f 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -419,7 +419,7 @@ throw(type[,value[,tb]])\n\ \n\ Raise exception in generator, return next yielded value or raise\n\ StopIteration.\n\ -the (type, val, tb) exception representation is deprecated, \n\ +the (type, val, tb) signature is deprecated, \n\ and may be removed in a future version of Python."); static PyObject * From 7f1dd7fa1fffdd1f794211ab517ed40454722d91 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Tue, 13 Sep 2022 10:05:09 +0800 Subject: [PATCH 12/27] Update Objects/genobject.c Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Objects/genobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index e425ebc8408a7f..eab4d33e655516 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -1158,7 +1158,7 @@ throw(type[,value[,traceback]])\n\ \n\ Raise exception in coroutine, return next iterated value or raise\n\ StopIteration.\n\ -the (type, val, tb) exception representation is deprecated, \n\ +the (type, val, tb) signature is deprecated, \n\ and may be removed in a future version of Python."); From 0b16455649bbf509f2a14921c59259e8c0d5fafa Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Tue, 13 Sep 2022 10:05:15 +0800 Subject: [PATCH 13/27] Update Lib/test/test_generators.py Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/test/test_generators.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 46c8e3d31ba6ad..a1a41b1c326b50 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -2116,9 +2116,8 @@ def printsolution(self, x): >>> import warnings >>> warnings.filterwarnings("ignore", category=DeprecationWarning) -# Filter DeprecationWarning: the (type, val, tb) exception representation is deprecated, -# and may be removed in a future version of Python. -# Will re-enable it soon. +# Filter DeprecationWarning: regarding the (type, val, tb) signature of throw(). +# Deprecation warnings are re-enabled below. >>> g.throw(ValueError, ValueError(1)) # value+matching type caught ValueError (1) From e204cc6191b280554e0a89386399490916ef2127 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Tue, 13 Sep 2022 10:05:31 +0800 Subject: [PATCH 14/27] Update Objects/genobject.c Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Objects/genobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/genobject.c b/Objects/genobject.c index eab4d33e655516..fa05cc5533f761 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -563,8 +563,8 @@ gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs) } if (nargs > 1) { if (PyErr_WarnEx(PyExc_DeprecationWarning, - "the (type, val, tb) exception representation is deprecated, " - "and may be removed in a future version of Python.", + "the (type, exc, tb) signature of throw() is deprecated, " + "use the single-arg signature instead.", 1) < 0) { return NULL; } From 0873dc3662ae811bbb4f0fb240af65377c21322a Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Tue, 20 Sep 2022 21:16:39 +0800 Subject: [PATCH 15/27] Reversed agen.athrow() documentation, and what's new with method link. --- Doc/reference/expressions.rst | 8 +------- Doc/whatsnew/3.12.rst | 4 +++- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index c5e05d225f1c4e..df1eaeae2ade22 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -743,8 +743,7 @@ which are used to control the execution of a generator function. because there is no yield expression that could receive the value. -.. coroutinemethod:: agen.athrow(value) - agen.athrow(type[, value[, traceback]]) +.. coroutinemethod:: agen.athrow(type[, value[, traceback]]) Returns an awaitable that raises an exception of type ``type`` at the point where the asynchronous generator was paused, and returns the next value @@ -756,11 +755,6 @@ which are used to control the execution of a generator function. raises a different exception, then when the awaitable is run that exception propagates to the caller of the awaitable. - .. versionchanged:: 3.12 - - The second signature \(type\[, value\[, traceback\]\]\) is deprecated and - may be removed in a future version of Python. - .. index:: exception: GeneratorExit diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index bef58112f1b268..e9759d3efe5886 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -159,7 +159,9 @@ Deprecated and tailor them to your needs. (Contributed by Erlend E. Aasland in :gh:`90016`.) -* The throw\(type, value, traceback\) signature is now deprecated. (:gh:`89874`.) +* The 3-arg signatures (type, value, traceback) of :meth:`~coroutine.throw` + and :meth:`~generator.throw` are deprecated and may be removed in a future + version of Python. Use the single-arg versions of these functions instead. (:gh:`89874`.) Pending Removal in Python 3.13 From 4a1539cd615ebdab43ef961d05bd96df7435e47e Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Thu, 22 Sep 2022 20:32:19 +0800 Subject: [PATCH 16/27] Apply suggestions from code review Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Doc/whatsnew/3.12.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index e9759d3efe5886..cda5d69dcdaacd 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -161,7 +161,8 @@ Deprecated * The 3-arg signatures (type, value, traceback) of :meth:`~coroutine.throw` and :meth:`~generator.throw` are deprecated and may be removed in a future - version of Python. Use the single-arg versions of these functions instead. (:gh:`89874`.) + version of Python. Use the single-arg versions of these functions instead. + (Contributed by Ofey Chan in :gh:`89874`.) Pending Removal in Python 3.13 From 85b67cb6480f469d140c0b1e5ebace2a9c85738f Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Thu, 22 Sep 2022 20:40:12 +0800 Subject: [PATCH 17/27] Add DeprecationWarning, doc and what's new to agen.athrow --- Doc/reference/expressions.rst | 8 +++++++- Doc/whatsnew/3.12.rst | 2 +- Objects/genobject.c | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index df1eaeae2ade22..c5e05d225f1c4e 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -743,7 +743,8 @@ which are used to control the execution of a generator function. because there is no yield expression that could receive the value. -.. coroutinemethod:: agen.athrow(type[, value[, traceback]]) +.. coroutinemethod:: agen.athrow(value) + agen.athrow(type[, value[, traceback]]) Returns an awaitable that raises an exception of type ``type`` at the point where the asynchronous generator was paused, and returns the next value @@ -755,6 +756,11 @@ which are used to control the execution of a generator function. raises a different exception, then when the awaitable is run that exception propagates to the caller of the awaitable. + .. versionchanged:: 3.12 + + The second signature \(type\[, value\[, traceback\]\]\) is deprecated and + may be removed in a future version of Python. + .. index:: exception: GeneratorExit diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index cda5d69dcdaacd..49521cc9d97357 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -160,7 +160,7 @@ Deprecated (Contributed by Erlend E. Aasland in :gh:`90016`.) * The 3-arg signatures (type, value, traceback) of :meth:`~coroutine.throw` - and :meth:`~generator.throw` are deprecated and may be removed in a future + , :meth:`~generator.throw` and :meth:`~agen.athrow` are deprecated and may be removed in a future version of Python. Use the single-arg versions of these functions instead. (Contributed by Ofey Chan in :gh:`89874`.) diff --git a/Objects/genobject.c b/Objects/genobject.c index fa05cc5533f761..ad4fbed6d8d579 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -1513,6 +1513,14 @@ async_gen_aclose(PyAsyncGenObject *o, PyObject *arg) static PyObject * async_gen_athrow(PyAsyncGenObject *o, PyObject *args) { + if (PyTuple_GET_SIZE(args) > 1) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "the (type, exc, tb) signature of athrow() is deprecated, " + "use the single-arg signature instead.", + 1) < 0) { + return NULL; + } + } if (async_gen_init_hooks(o)) { return NULL; } @@ -1550,7 +1558,12 @@ PyDoc_STRVAR(async_asend_doc, "asend(v) -> send 'v' in generator."); PyDoc_STRVAR(async_athrow_doc, -"athrow(typ[,val[,tb]]) -> raise exception in generator."); +"athrow(value)\n\ +athrow(type[,value[,tb]])\n\ +\n\ +raise exception in generator.\n\ +the (type, val, tb) signature is deprecated, \n\ +and may be removed in a future version of Python."); static PyMethodDef async_gen_methods[] = { {"asend", (PyCFunction)async_gen_asend, METH_O, async_asend_doc}, From 22bb65b782c851b2dea8c8e123f549945a28917f Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Thu, 22 Sep 2022 20:57:22 +0800 Subject: [PATCH 18/27] Update acks, and doc of anextawaitable_throw --- Misc/ACKS | 1 + Objects/iterobject.c | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Misc/ACKS b/Misc/ACKS index 0edea9219d05ef..fc0e745e28ceab 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -297,6 +297,7 @@ Michael Cetrulo Dave Chambers Pascal Chambon Nicholas Chammas +Ofey Chan John Chandler Hye-Shik Chang Jeffrey Chang diff --git a/Objects/iterobject.c b/Objects/iterobject.c index 1732a037600c9e..62c36146d64f69 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -428,8 +428,13 @@ return next yielded value or raise StopIteration."); PyDoc_STRVAR(throw_doc, -"throw(typ[,val[,tb]]) -> raise exception in the wrapped iterator,\n\ -return next yielded value or raise StopIteration."); +"throw(value)\n\ +throw(typ[,val[,tb]])\n\ +\n\ +raise exception in the wrapped iterator, return next yielded value\n\ +or raise StopIteration.\n\ +the (type, val, tb) signature is deprecated, \n\ +and may be removed in a future version of Python."); PyDoc_STRVAR(close_doc, From 7b786e420fdb62dc77cc74228c7eed7717cdf5b7 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Sat, 24 Sep 2022 13:04:34 +0800 Subject: [PATCH 19/27] Add test_async_gen_3_arg_deprecation_warning. --- Lib/test/test_asyncgen.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index 423a9f711d2873..1acefe9a5139ce 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -2,6 +2,7 @@ import types import unittest import contextlib +import warnings from test.support.import_helper import import_module from test.support import gc_collect, requires_working_socket @@ -377,6 +378,13 @@ async def async_gen_wrapper(): self.compare_generators(sync_gen_wrapper(), async_gen_wrapper()) + def test_async_gen_3_arg_deprecation_warning(self): + async def gen(): + yield 123 + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + gen().athrow(GeneratorExit, GeneratorExit(), None) + def test_async_gen_api_01(self): async def gen(): yield 123 From 198645abeddc65712496aa01467489f8bcee4e70 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Sun, 25 Sep 2022 17:09:03 +0800 Subject: [PATCH 20/27] Apply formatting suggestions from code review Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Doc/whatsnew/3.12.rst | 8 ++++---- Lib/test/test_asyncgen.py | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 49521cc9d97357..951b93876e59a8 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -159,10 +159,10 @@ Deprecated and tailor them to your needs. (Contributed by Erlend E. Aasland in :gh:`90016`.) -* The 3-arg signatures (type, value, traceback) of :meth:`~coroutine.throw` - , :meth:`~generator.throw` and :meth:`~agen.athrow` are deprecated and may be removed in a future - version of Python. Use the single-arg versions of these functions instead. - (Contributed by Ofey Chan in :gh:`89874`.) +* The 3-arg signatures (type, value, traceback) of :meth:`~coroutine.throw`, + :meth:`~generator.throw` and :meth:`~agen.athrow` are deprecated and + may be removed in a future version of Python. Use the single-arg versions + of these functions instead. (Contributed by Ofey Chan in :gh:`89874`.) Pending Removal in Python 3.13 diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index 1acefe9a5139ce..9567a201c6cc1c 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -381,6 +381,7 @@ async def async_gen_wrapper(): def test_async_gen_3_arg_deprecation_warning(self): async def gen(): yield 123 + with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) gen().athrow(GeneratorExit, GeneratorExit(), None) From 528031c43dd9cb370877bdbbe42d9f5b12551cc8 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Sun, 25 Sep 2022 22:50:41 +0800 Subject: [PATCH 21/27] patchcheck.py passed --- Lib/test/test_asyncgen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index 9567a201c6cc1c..e12a94253f478a 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -381,7 +381,7 @@ async def async_gen_wrapper(): def test_async_gen_3_arg_deprecation_warning(self): async def gen(): yield 123 - + with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) gen().athrow(GeneratorExit, GeneratorExit(), None) From 93db966245904f71594ad0a41696d841aa40c83c Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Sun, 25 Sep 2022 23:11:41 +0800 Subject: [PATCH 22/27] Update Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- .../2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst b/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst index f51ec42d75f4ef..5d3bd17b578669 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst @@ -1 +1,2 @@ -Emit a DeprecationWarning when :meth:`~generator.throw` or :meth:`~coroutine.throw` are called with more than one argument. +Emit a DeprecationWarning when :meth:`~generator.throw`, :meth:`~coroutine.throw` or :meth:`~agen.athrow` +are called with more than one argument. From 0e56eb9c550f4325bd9583d24c8870d3053c313e Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Sun, 25 Sep 2022 20:44:33 +0100 Subject: [PATCH 23/27] assert that deprecation warning is emitted --- Lib/test/test_asyncgen.py | 3 +-- Lib/test/test_asyncio/test_futures.py | 2 ++ Lib/test/test_coroutines.py | 7 +++++++ Lib/test/test_generators.py | 9 +++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index e12a94253f478a..f6184c0cab4694 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -382,8 +382,7 @@ def test_async_gen_3_arg_deprecation_warning(self): async def gen(): yield 123 - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=DeprecationWarning) + with self.assertWarns(DeprecationWarning): gen().athrow(GeneratorExit, GeneratorExit(), None) def test_async_gen_api_01(self): diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index 7e3ce02d6adb49..a3fabbb0545fa6 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -620,6 +620,8 @@ def test_future_stop_iteration_args(self): def test_future_iter_throw(self): fut = self._new_future(loop=self.loop) fi = iter(fut) + with self.assertWarns(DeprecationWarning): + fi.throw(Exception, Exception("zebra"), None) with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) self.assertRaises(TypeError, fi.throw, diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index 4df8730157a8bf..9a2279d353ffa4 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -712,6 +712,13 @@ async def foo(): aw.throw(ZeroDivisionError()) self.assertEqual(N, 102) + coro = foo() + aw = coro.__await__() + next(aw) + with self.assertRaises(ZeroDivisionError): + with self.assertWarns(DeprecationWarning): + aw.throw(ZeroDivisionError, ZeroDivisionError(), None) + def test_func_11(self): async def func(): pass coro = func() diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index a1a41b1c326b50..fb2d9ced0633f1 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -342,6 +342,15 @@ def generator(): with self.assertRaises(StopIteration): gen.throw(E) + def test_gen_3_arg_deprecation_warning(self): + def g(): + yield 42 + + gen = g() + with self.assertWarns(DeprecationWarning): + with self.assertRaises(TypeError): + gen.throw(TypeError, TypeError(24), None) + def test_stopiteration_error(self): # See also PEP 479. From 8738273cd1979f6f808484cdd94df0cadc0dafdd Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Wed, 28 Sep 2022 09:42:43 +0800 Subject: [PATCH 24/27] stage, exploring how to capture deprecation warning rather than supress them. --- Lib/test/test_asyncgen.py | 3 +-- Lib/test/test_generators.py | 46 ++++++++++++++----------------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index e12a94253f478a..f6184c0cab4694 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -382,8 +382,7 @@ def test_async_gen_3_arg_deprecation_warning(self): async def gen(): yield 123 - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=DeprecationWarning) + with self.assertWarns(DeprecationWarning): gen().athrow(GeneratorExit, GeneratorExit(), None) def test_async_gen_api_01(self): diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index a1a41b1c326b50..34f85254ece265 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -2113,30 +2113,30 @@ def printsolution(self, x): >>> g.throw(ValueError("xyz")) # value only caught ValueError (xyz) ->>> import warnings ->>> warnings.filterwarnings("ignore", category=DeprecationWarning) - -# Filter DeprecationWarning: regarding the (type, val, tb) signature of throw(). -# Deprecation warnings are re-enabled below. - >>> g.throw(ValueError, ValueError(1)) # value+matching type -caught ValueError (1) +Traceback (most recent call last): + ... +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError, TypeError(1)) # mismatched type, rewrapped -caught ValueError (1) +Traceback (most recent call last): + ... +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError, ValueError(1), None) # explicit None traceback -caught ValueError (1) +Traceback (most recent call last): + ... +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError(1), "foo") # bad args Traceback (most recent call last): ... -TypeError: instance exception may not have a separate value +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError, "foo", 23) # bad args Traceback (most recent call last): ... -TypeError: throw() third argument must be a traceback object +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw("abc") Traceback (most recent call last): @@ -2159,7 +2159,9 @@ def printsolution(self, x): ... except: ... g.throw(*sys.exc_info()) >>> throw(g,ValueError) # do it with traceback included -caught ValueError () +Traceback (most recent call last): + ... +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.send(1) 1 @@ -2167,31 +2169,17 @@ def printsolution(self, x): >>> throw(g,TypeError) # terminate the generator Traceback (most recent call last): ... -TypeError - ->>> print(g.gi_frame) -None - ->>> g.send(2) -Traceback (most recent call last): - ... -StopIteration +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> g.throw(ValueError,6) # throw on closed generator Traceback (most recent call last): ... -ValueError: 6 +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. >>> f().throw(ValueError,7) # throw on just-opened generator Traceback (most recent call last): ... -ValueError: 7 - ->>> warnings.filters.pop(0) -('ignore', None, , None, 0) - -# Re-enable DeprecationWarning: the (type, val, tb) exception representation is deprecated, -# and may be removed in a future version of Python. +DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. Plain "raise" inside a generator should preserve the traceback (#13188). The traceback should have 3 levels: From 31b2e9f762cc920c18d31e855daa721634d01360 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Wed, 28 Sep 2022 16:54:58 +0800 Subject: [PATCH 25/27] Revert "stage, exploring how to capture deprecation warning rather than supress them." This reverts commit 8738273cd1979f6f808484cdd94df0cadc0dafdd. --- Lib/test/test_asyncgen.py | 3 ++- Lib/test/test_generators.py | 46 +++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/Lib/test/test_asyncgen.py b/Lib/test/test_asyncgen.py index f6184c0cab4694..e12a94253f478a 100644 --- a/Lib/test/test_asyncgen.py +++ b/Lib/test/test_asyncgen.py @@ -382,7 +382,8 @@ def test_async_gen_3_arg_deprecation_warning(self): async def gen(): yield 123 - with self.assertWarns(DeprecationWarning): + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) gen().athrow(GeneratorExit, GeneratorExit(), None) def test_async_gen_api_01(self): diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index 34f85254ece265..a1a41b1c326b50 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -2113,30 +2113,30 @@ def printsolution(self, x): >>> g.throw(ValueError("xyz")) # value only caught ValueError (xyz) +>>> import warnings +>>> warnings.filterwarnings("ignore", category=DeprecationWarning) + +# Filter DeprecationWarning: regarding the (type, val, tb) signature of throw(). +# Deprecation warnings are re-enabled below. + >>> g.throw(ValueError, ValueError(1)) # value+matching type -Traceback (most recent call last): - ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +caught ValueError (1) >>> g.throw(ValueError, TypeError(1)) # mismatched type, rewrapped -Traceback (most recent call last): - ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +caught ValueError (1) >>> g.throw(ValueError, ValueError(1), None) # explicit None traceback -Traceback (most recent call last): - ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +caught ValueError (1) >>> g.throw(ValueError(1), "foo") # bad args Traceback (most recent call last): ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +TypeError: instance exception may not have a separate value >>> g.throw(ValueError, "foo", 23) # bad args Traceback (most recent call last): ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +TypeError: throw() third argument must be a traceback object >>> g.throw("abc") Traceback (most recent call last): @@ -2159,9 +2159,7 @@ def printsolution(self, x): ... except: ... g.throw(*sys.exc_info()) >>> throw(g,ValueError) # do it with traceback included -Traceback (most recent call last): - ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +caught ValueError () >>> g.send(1) 1 @@ -2169,17 +2167,31 @@ def printsolution(self, x): >>> throw(g,TypeError) # terminate the generator Traceback (most recent call last): ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +TypeError + +>>> print(g.gi_frame) +None + +>>> g.send(2) +Traceback (most recent call last): + ... +StopIteration >>> g.throw(ValueError,6) # throw on closed generator Traceback (most recent call last): ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +ValueError: 6 >>> f().throw(ValueError,7) # throw on just-opened generator Traceback (most recent call last): ... -DeprecationWarning: the (type, exc, tb) signature of throw() is deprecated, use the single-arg signature instead. +ValueError: 7 + +>>> warnings.filters.pop(0) +('ignore', None, , None, 0) + +# Re-enable DeprecationWarning: the (type, val, tb) exception representation is deprecated, +# and may be removed in a future version of Python. Plain "raise" inside a generator should preserve the traceback (#13188). The traceback should have 3 levels: From ed2e83aad35fe66c58f60de863b6bd2f42ae9247 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Wed, 28 Sep 2022 11:20:18 +0100 Subject: [PATCH 26/27] fix test --- Lib/test/test_asyncio/test_futures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index a3fabbb0545fa6..11d4273930804f 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -621,7 +621,7 @@ def test_future_iter_throw(self): fut = self._new_future(loop=self.loop) fi = iter(fut) with self.assertWarns(DeprecationWarning): - fi.throw(Exception, Exception("zebra"), None) + self.assertRaises(Exception, fi.throw, Exception, Exception("zebra"), None) with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) self.assertRaises(TypeError, fi.throw, From 03fc6e0654d193e8cfadd0f0f85eaa2db1aa1316 Mon Sep 17 00:00:00 2001 From: Ofey Chan Date: Thu, 29 Sep 2022 23:37:46 +0800 Subject: [PATCH 27/27] Add a warning in FutureIter_throw --- Modules/_asynciomodule.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 9d2f83bf6c73c1..5a5881b873e245 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1668,6 +1668,14 @@ FutureIter_throw(futureiterobject *self, PyObject *const *args, Py_ssize_t nargs if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) { return NULL; } + if (nargs > 1) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "the (type, exc, tb) signature of throw() is deprecated, " + "use the single-arg signature instead.", + 1) < 0) { + return NULL; + } + } type = args[0]; if (nargs == 3) {