◐ Shell
clean mode source ↗

bpo-43683: Handle generator entry in bytecode by markshannon · Pull Request #25138 · python/cpython

@markshannon

Adds GEN_START bytecode to handle checking for None when starting a generator or coroutine.

This means that:

  • send() doesn't need to check the state of the generator.
  • Should speed up iteration as check is only performed once per generator, not once per iteration.

https://bugs.python.org/issue43683

willingc

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

…o bytecode.

Document new bytecode and make it fail gracefully if mis-compiled.

Add NEWS item

Bump magic number.

gvanrossum

gvanrossum

entryblock = b;
}

if (insert_generator_prefix(c, entryblock)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're compiling a generator (or coroutine etc.) the new function assumes (without asserting) that entryblock is not NULL. But evidently it could be, since below on line 6690 there's a check whether it could be. I don't know how it could be NULL - that would mean there's no code in the function at all, but it seems there's code above that always adds at least one instruction (RETURN_VALUE on line 6668). So maybe the check for entryblock == NULL below can be replaced by an assert? (I'd place that assert right underneath the for-loop above on lines 6683-6686.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entryblock cannot be NULL. I'll add the assert.

@markshannon

@markshannon markshannon deleted the handle-generator-entry-in-bytecode branch

April 14, 2021 13:27