python generator send 的源代码在哪里?

Where is the source code of python generator send?

问题

请帮助查明实现生成器发送部分的 Python 源代码。我想在 github This is Python version 3.8.7rc1 的某个地方,但不熟悉存储库的组织方式。

背景

难以理解 PEP 342 和有关生成器发送(值)的文档。因此试图找出它是如何实现的。

Specification: Sending Values into Generators

Because generator-iterators begin execution at the top of the generator's function body, there is no yield expression to receive a value when the generator has just been created. Therefore, calling send() with a non-None argument is prohibited when the generator iterator has just started, and a TypeError is raised if this occurs (presumably due to a logic error of some kind). Thus, before you can communicate with a coroutine you must first call next() or send(None) to advance its execution to the first yield expression.

generator.send(value)

Resumes the execution and “sends” a value into the generator function. The value argument becomes the result of the current yield expression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value. When send() is called to start the generator, it must be called with None as the argument, because there is no yield expression that could receive the value.

我想 yield 就像一个 UNIX 系统调用移动到一个例程中,其中保存了堆栈帧和执行指针,并且暂停了生成器协同例程。我认为当调用 save(value) 时,那里会发生一些技巧,这些技巧与文档中的神秘部分有关。

虽然 sent_value = (yield value) 是一行语句,但我认为阻塞和恢复都发生在同一行。执行不会在 yield 之后恢复,而是在其中恢复,因此想知道 block/resume 是如何实现的。我也相信next(generator) is the same with generator.send(None),想验证一下

here for class Generator, also look for this文件,里面是C上生成器的完整实现Python