GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-79940: add introspection API for asynchronous generators by tkren · Pull Request #11590 · python/cpython

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.

Sign up for GitHub

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
kumaraditya303 merged 6 commits into python:main from tkren:fix-issue-35759
Mar 11, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-79940: add introspection API for asynchronous generators #11590

Show file tree
Hide file tree
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 Jan 16, 2019
093595a
inspect: use f-string in getasyncgenlocals()
tkren Mar 10, 2023
1931c0b
inspect: use anext() built-in in TestGetAsyncGenState
tkren Mar 10, 2023
771f95e
whatsnew: add an entry for inspect
tkren Mar 11, 2023
ac46ef8
inspect: use IsolatedAsyncioTestCase for TestGetAsyncGenState
tkren Mar 11, 2023
5302f23
whatsnew: use bpo issue instead of github id
tkren Mar 11, 2023
File filter
Filter by extension

Clear filters
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

28 changes: 26 additions & 2 deletions 28 Doc/library/inspect.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1439,8 +1439,8 @@ code execution::
pass


Current State of Generators and Coroutines
------------------------------------------
Current State of Generators, Coroutines, and Asynchronous Generators
Comment thread
tkren marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

--------------------------------------------------------------------

When implementing coroutine schedulers and for other advanced uses of
generators, it is useful to determine whether a generator is currently
Expand Down Expand Up @@ -1475,6 +1475,22 @@ generator to be determined easily.

.. versionadded:: 3.5

.. function:: getasyncgenstate(agen)

Get current state of an asynchronous generator object. The function is
intended to be used with asynchronous iterator objects created by
:keyword:`async def` functions which use the :keyword:`yield` statement,
but will accept any asynchronous generator-like object that has
``ag_running`` and ``ag_frame`` attributes.

Possible states are:
* AGEN_CREATED: Waiting to start execution.
* AGEN_RUNNING: Currently being executed by the interpreter.
* AGEN_SUSPENDED: Currently suspended at a yield expression.
* AGEN_CLOSED: Execution has completed.

.. versionadded:: 3.12

The current internal state of the generator can also be queried. This is
mostly useful for testing purposes, to ensure that internal state is being
updated as expected:
Expand Down Expand Up @@ -1506,6 +1522,14 @@ updated as expected:

.. versionadded:: 3.5

.. function:: getasyncgenlocals(agen)

This function is analogous to :func:`~inspect.getgeneratorlocals`, but
works for asynchronous generator objects created by :keyword:`async def`
functions which use the :keyword:`yield` statement.

.. versionadded:: 3.12


.. _inspect-module-co-flags:

Expand Down
4 changes: 4 additions & 0 deletions 4 Doc/whatsnew/3.12.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ inspect
a :term:`coroutine` for use with :func:`iscoroutinefunction`.
(Contributed Carlton Gibson in :gh:`99247`.)

* Add :func:`inspect.getasyncgenstate` and :func:`inspect.getasyncgenlocals`
for determining the current state of asynchronous generators.
(Contributed by Thomas Krennwallner in :issue:`35759`.)

pathlib
-------

Expand Down
50 changes: 50 additions & 0 deletions 50 Lib/inspect.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
'Yury Selivanov <[email protected]>')

__all__ = [
"AGEN_CLOSED",
"AGEN_CREATED",
"AGEN_RUNNING",
"AGEN_SUSPENDED",
"ArgInfo",
"Arguments",
"Attribute",
Expand Down Expand Up @@ -77,6 +81,8 @@
"getabsfile",
"getargs",
"getargvalues",
"getasyncgenlocals",
"getasyncgenstate",
"getattr_static",
"getblock",
"getcallargs",
Expand Down Expand Up @@ -1935,6 +1941,50 @@ def getcoroutinelocals(coroutine):
return {}


# ----------------------------------- asynchronous generator introspection

AGEN_CREATED = 'AGEN_CREATED'
AGEN_RUNNING = 'AGEN_RUNNING'
AGEN_SUSPENDED = 'AGEN_SUSPENDED'
AGEN_CLOSED = 'AGEN_CLOSED'


def getasyncgenstate(agen):
"""Get current state of an asynchronous generator object.

Possible states are:
AGEN_CREATED: Waiting to start execution.
AGEN_RUNNING: Currently being executed by the interpreter.
AGEN_SUSPENDED: Currently suspended at a yield expression.
AGEN_CLOSED: Execution has completed.
"""
if agen.ag_running:
return AGEN_RUNNING
if agen.ag_suspended:
return AGEN_SUSPENDED
if agen.ag_frame is None:
return AGEN_CLOSED
return AGEN_CREATED


def getasyncgenlocals(agen):
"""
Get the mapping of asynchronous generator local variables to their current
values.

A dict is returned, with the keys the local variable names and values the
bound values."""

if not isasyncgen(agen):
raise TypeError(f"{agen!r} is not a Python async generator")

frame = getattr(agen, "ag_frame", None)
if frame is not None:
return agen.ag_frame.f_locals
else:
return {}


###############################################################################
### Function Signature Object (PEP 362)
###############################################################################
Expand Down
107 changes: 107 additions & 0 deletions 107 Lib/test/test_inspect.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import builtins
import collections
import datetime
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2321,6 +2326,108 @@ async def func(a=None):
{'a': None, 'gencoro': gencoro, 'b': 'spam'})


class TestGetAsyncGenState(unittest.IsolatedAsyncioTestCase):

Comment thread
tkren marked this conversation as resolved.
Resolved

Uh 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
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2023-02-26-17-29-57.gh-issue-79940.SAfmAy.rst
Show comments
View file
Open in desktop
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.
10 changes: 10 additions & 0 deletions 10 Objects/genobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,15 @@ ag_getcode(PyGenObject *gen, void *Py_UNUSED(ignored))
return _gen_getcode(gen, "ag__code");
}

static PyObject *
ag_getsuspended(PyAsyncGenObject *ag, void *Py_UNUSED(ignored))
{
if (ag->ag_frame_state == FRAME_SUSPENDED) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
}

static PyGetSetDef async_gen_getsetlist[] = {
{"__name__", (getter)gen_get_name, (setter)gen_set_name,
PyDoc_STR("name of the async generator")},
Expand All @@ -1561,6 +1570,7 @@ static PyGetSetDef async_gen_getsetlist[] = {
PyDoc_STR("object being awaited on, or None")},
{"ag_frame", (getter)ag_getframe, NULL, NULL},
{"ag_code", (getter)ag_getcode, NULL, NULL},
{"ag_suspended", (getter)ag_getsuspended, NULL, NULL},
{NULL} /* Sentinel */
};

Expand Down
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.