diff --git a/pep-0709.rst b/pep-0709.rst index aed2284ca..ae775a50d 100644 --- a/pep-0709.rst +++ b/pep-0709.rst @@ -139,6 +139,21 @@ Generator expressions are currently never inlined in the reference implementation of this PEP. In the future, some generator expressions may be inlined, where the returned generator object does not leak. +In more complex cases, the comprehension iteration variable may be a global or +cellvar or freevar in the outer function scope. In these cases, the compiler +also internally pushes and pops the scope information for the variable when +entering/leaving the comprehension, so that semantics are maintained. For +example, if the variable is a global outside the comprehension, ``LOAD_GLOBAL`` +will still be used where it is referenced. If it is a cellvar/freevar outside +the comprehension, the ``LOAD_FAST_AND_CLEAR`` / ``STORE_FAST`` used to +save/restore it do not change (there is no ``LOAD_DEREF_AND_CLEAR``), meaning +that the entire cell (not just the value within it) is saved/restored, so the +comprehension does not write to the cell. + +In effect, comprehensions introduce a sub-function scope where local variables +are fully isolated, but without the performance cost or stack frame entry of a +call. + Backwards Compatibility ======================= @@ -224,6 +239,18 @@ comprehension and its containing function and point to a calling frame outside the library. In such a scenario it would usually be simpler and more reliable to raise the warning closer to the calling code and bypass fewer frames. +Impact on other Python implementations +====================================== + +Per comments from representatives of `GraalPython +`_ and +`PyPy `_, +they would likely feel the need to adapt to the observable behavior changes +here, given the likelihood that someone, at some point, will depend on them. +Thus, all else equal, fewer observable changes would be less work. But these +changes (at least in the case of GraalPython) should be manageable "without much +headache". + How to Teach This =================