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-79940: add introspection API for asynchronous generators#11590
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
6 commits Select commit Hold shift + click to select a range
bca9939 inspect: add introspection API for asynchronous generators
tkren 093595a inspect: use f-string in getasyncgenlocals()
tkren 1931c0b inspect: use anext() built-in in TestGetAsyncGenState
tkren 771f95e whatsnew: add an entry for inspect
tkren ac46ef8 inspect: use IsolatedAsyncioTestCase for TestGetAsyncGenState
tkren 5302f23 whatsnew: use bpo issue instead of github id
tkren 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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import asyncio | ||
| import builtins | ||
| import collections | ||
| import datetime | ||
| @@ -65,6 +66,10 @@ def revise(filename, *args): | ||
| git = mod.StupidGit() | ||
| def tearDownModule(): | ||
| asyncio.set_event_loop_policy(None) | ||
| def signatures_with_lexicographic_keyword_only_parameters(): | ||
| """ | ||
| Yields a whole bunch of functions with only keyword-only parameters, | ||
| @@ -2321,6 +2326,108 @@ async def func(a=None): | ||
| {'a': None, 'gencoro': gencoro, 'b': 'spam'}) | ||
| class TestGetAsyncGenState(unittest.IsolatedAsyncioTestCase): | ||
tkren marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| def setUp(self): | ||
| async def number_asyncgen(): | ||
| for number in range(5): | ||
| yield number | ||
| self.asyncgen = number_asyncgen() | ||
| async def asyncTearDown(self): | ||
| await self.asyncgen.aclose() | ||
| def _asyncgenstate(self): | ||
| return inspect.getasyncgenstate(self.asyncgen) | ||
| def test_created(self): | ||
| self.assertEqual(self._asyncgenstate(), inspect.AGEN_CREATED) | ||
| async def test_suspended(self): | ||
| value = await anext(self.asyncgen) | ||
| self.assertEqual(self._asyncgenstate(), inspect.AGEN_SUSPENDED) | ||
| self.assertEqual(value, 0) | ||
| async def test_closed_after_exhaustion(self): | ||
| countdown = 7 | ||
| with self.assertRaises(StopAsyncIteration): | ||
| while countdown := countdown - 1: | ||
| await anext(self.asyncgen) | ||
| self.assertEqual(countdown, 1) | ||
| self.assertEqual(self._asyncgenstate(), inspect.AGEN_CLOSED) | ||
| async def test_closed_after_immediate_exception(self): | ||
| with self.assertRaises(RuntimeError): | ||
| await self.asyncgen.athrow(RuntimeError) | ||
| self.assertEqual(self._asyncgenstate(), inspect.AGEN_CLOSED) | ||
| async def test_running(self): | ||
| async def running_check_asyncgen(): | ||
| for number in range(5): | ||
| self.assertEqual(self._asyncgenstate(), inspect.AGEN_RUNNING) | ||
| yield number | ||
| self.assertEqual(self._asyncgenstate(), inspect.AGEN_RUNNING) | ||
| self.asyncgen = running_check_asyncgen() | ||
| # Running up to the first yield | ||
| await anext(self.asyncgen) | ||
| self.assertEqual(self._asyncgenstate(), inspect.AGEN_SUSPENDED) | ||
| # Running after the first yield | ||
| await anext(self.asyncgen) | ||
| self.assertEqual(self._asyncgenstate(), inspect.AGEN_SUSPENDED) | ||
| def test_easy_debugging(self): | ||
| # repr() and str() of a asyncgen state should contain the state name | ||
| names = 'AGEN_CREATED AGEN_RUNNING AGEN_SUSPENDED AGEN_CLOSED'.split() | ||
| for name in names: | ||
| state = getattr(inspect, name) | ||
| self.assertIn(name, repr(state)) | ||
| self.assertIn(name, str(state)) | ||
| async def test_getasyncgenlocals(self): | ||
| async def each(lst, a=None): | ||
| b=(1, 2, 3) | ||
| for v in lst: | ||
| if v == 3: | ||
| c = 12 | ||
| yield v | ||
| numbers = each([1, 2, 3]) | ||
| self.assertEqual(inspect.getasyncgenlocals(numbers), | ||
| {'a': None, 'lst': [1, 2, 3]}) | ||
| await anext(numbers) | ||
| self.assertEqual(inspect.getasyncgenlocals(numbers), | ||
| {'a': None, 'lst': [1, 2, 3], 'v': 1, | ||
| 'b': (1, 2, 3)}) | ||
| await anext(numbers) | ||
| self.assertEqual(inspect.getasyncgenlocals(numbers), | ||
| {'a': None, 'lst': [1, 2, 3], 'v': 2, | ||
| 'b': (1, 2, 3)}) | ||
| await anext(numbers) | ||
| self.assertEqual(inspect.getasyncgenlocals(numbers), | ||
| {'a': None, 'lst': [1, 2, 3], 'v': 3, | ||
| 'b': (1, 2, 3), 'c': 12}) | ||
| with self.assertRaises(StopAsyncIteration): | ||
| await anext(numbers) | ||
| self.assertEqual(inspect.getasyncgenlocals(numbers),{}) | ||
| async def test_getasyncgenlocals_empty(self): | ||
| async def yield_one(): | ||
| yield 1 | ||
| one = yield_one() | ||
| self.assertEqual(inspect.getasyncgenlocals(one),{}) | ||
| await anext(one) | ||
| self.assertEqual(inspect.getasyncgenlocals(one),{}) | ||
| with self.assertRaises(StopAsyncIteration): | ||
| await anext(one) | ||
| self.assertEqual(inspect.getasyncgenlocals(one),{}) | ||
| def test_getasyncgenlocals_error(self): | ||
| self.assertRaises(TypeError, inspect.getasyncgenlocals, 1) | ||
| self.assertRaises(TypeError, inspect.getasyncgenlocals, lambda x: True) | ||
| self.assertRaises(TypeError, inspect.getasyncgenlocals, set) | ||
| self.assertRaises(TypeError, inspect.getasyncgenlocals, (2,3)) | ||
| class MySignature(inspect.Signature): | ||
| # Top-level to make it picklable; | ||
| # used in test_signature_object_pickle | ||
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2023-02-26-17-29-57.gh-issue-79940.SAfmAy.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 @@ | ||
| Add :func:`inspect.getasyncgenstate` and :func:`inspect.getasyncgenlocals`. | ||
| Patch by Thomas Krennwallner. |
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.