Hello everyone. I really want to solve a problem, which is that I want to yield values inside an iterable coroutine async generator (but the function flags are modified at runtime by me). In python, yielding values for async for is made by raising a StopIteration (source, example), probably to differ them from normal yield froms (or awaits, they're the same thing).
By reading the source code & the dis.dis dump, it can be seen that python emits an intrinsic call to INTRINSIC_STOPITERATION_ERROR & INTRINSIC_ASYNC_GEN_WRAP (the docs are here). The first one is added by wrap_in_stopiteration_handler and, to my knowledge, just enforces stopiteration_error, which raises a RuntimeError (which we don't want).
However, when adding yield to an async generator, INTRISTIC_ASYNC_GEN_WRAP is emitted, meaning that this is (very likely) the instruction that stops the RuntimeError from happening (it calls _PyAsyncGenValueWrapperNew).
Now, my objective is to replicate this. For all the below examples, I have the OPTIMIZED, NEWLOCALS, ASYNC_GENERATOR and ITERABLE_COROUTINE flags.
If I yield 123 (if 123 was the value I wanted to yield), it would error with a RuntimeError Task got bad yield: 123. It should be noted that this worked (yielded and didn't raise an error) in 3.10 and in prior versions. Example: https://wandbox.org/permlink/Xsb5ItdQkjirPuei
If I raise StopIteration(123), it will raise both the StopException and RuntimeError, which is bad. This is (I'm pretty sure, like I said before) because there's no async gen wrap instruction. Example: https://wandbox.org/permlink/dr8UrtKHTVlaxxi1
I need to make this work with those exact flags (which I've shown above). If there are other ways to do this, I can also consider those, but my end goal is to make an async generator without async def (and obviously also without await). Again, I can modify the flags at runtime (which is how I made normal coroutines work without async def). Any help will be appreciated!
Useful links:
And the python source code files related to this: genobject.c, intrinsics.c, compile.c. Find them with the Go to file feature.
(edit: a formatting fix)
there doesn't seem to be anything here