Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-96348: Deprecate the 3-arg signature of coroutine.throw, generator.throw and agen.athrow#96428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
30 commits Select commit Hold shift + click to select a range
b388f9a feat: Deprecate gen.throw(typ, val, tb)
ofey404 713d77a Update Objects/genobject.c
ofey404 fcfb65e fix clear cases
ofey404 073aa62 Update Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issu…
ofey404 1c6ed30 Filter remaining warnings.
ofey404 4881855 Add deprecated info in documentation.
ofey404 0158816 Use versionchanged instead of deprecated.
ofey404 8152b7c Update Doc/reference/expressions.rst
ofey404 670d8f8 more docs and what's new entry
ofey404 0c20b35 Edit what's new.
ofey404 ba9a87c Update Objects/genobject.c
ofey404 7f1dd7f Update Objects/genobject.c
ofey404 0b16455 Update Lib/test/test_generators.py
ofey404 e204cc6 Update Objects/genobject.c
ofey404 0873dc3 Reversed agen.athrow() documentation, and what's new with method link.
ofey404 4a1539c Apply suggestions from code review
ofey404 85b67cb Add DeprecationWarning, doc and what's new to agen.athrow
ofey404 22bb65b Update acks, and doc of anextawaitable_throw
ofey404 7b786e4 Add test_async_gen_3_arg_deprecation_warning.
ofey404 198645a Apply formatting suggestions from code review
ofey404 528031c patchcheck.py passed
ofey404 53c74db Merge branch 'main' into ofey404/deprecate-get-throw
ofey404 93db966 Update Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issu…
ofey404 0e56eb9 assert that deprecation warning is emitted
iritkatriel 8738273 stage, exploring how to capture deprecation warning rather than supre…
ofey404 31b2e9f Revert "stage, exploring how to capture deprecation warning rather th…
ofey404 8b701d6 Merge pull request #6 from iritkatriel/pr96428
ofey404 70e1f28 Merge branch 'main' into ofey404/deprecate-get-throw
iritkatriel ed2e83a fix test
iritkatriel 03fc6e0 Add a warning in FutureIter_throw
ofey404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -582,6 +582,11 @@ is already executing raises a :exc:`ValueError` exception. | ||
| :attr:`~BaseException.__traceback__` attribute stored in *value* may | ||
| be cleared. | ||
| .. versionchanged:: 3.12 | ||
| The second signature \(type\[, value\[, traceback\]\]\) is deprecated and | ||
| may be removed in a future version of Python. | ||
| .. index:: exception: GeneratorExit | ||
| @@ -738,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 | ||
| @@ -750,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. | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| .. index:: exception: GeneratorExit | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
ofey404 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| with self.assertWarns(DeprecationWarning): | ||
| gen().athrow(GeneratorExit, GeneratorExit(), None) | ||
| def test_async_gen_api_01(self): | ||
| async def gen(): | ||
| yield 123 | ||
| @@ -650,7 +658,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 +671,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 +700,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 +721,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 +736,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()'): | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Core and Builtins/2022-08-31-18-46-13.gh-issue-96348.xzCoTP.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Emit a DeprecationWarning when :meth:`~generator.throw`, :meth:`~coroutine.throw` or :meth:`~agen.athrow` | ||
| are called with more than one argument. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) signature is deprecated, \n\ | ||
| and may be removed in a future version of Python."); | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| 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, exc, tb) signature of throw() is deprecated, " | ||
| "use the single-arg signature instead.", | ||
| 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) signature is deprecated, \n\ | ||
| and may be removed in a future version of Python."); | ||
| PyDoc_STRVAR(coro_close_doc, | ||
| "close() -> raise GeneratorExit inside coroutine."); | ||
| @@ -1500,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; | ||
| } | ||
| @@ -1537,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}, | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future PRs, not that code should generally be marked up like