FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-124697: Represent inlined comprehensions as subscopes in the symbol table by iritkatriel · Pull Request #156819 · python/cpython · GitHub

Repository navigation

gh-124697: Represent inlined comprehensions as subscopes in the symbol table - #156819

Merged
iritkatriel merged 41 commits into
python:mainfrom
iritkatriel:subscope
Sep 23, 2026
Merged

iritkatriel merged 41 commits into
python:mainfrom
iritkatriel:subscope

Conversation

iritkatriel commented Sep 2, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

Resolves #124697

Inlined comprehensions are now represented in the symbol table as block of a new type InlinedComprehensionBlock, which is a subscope of the enclosing scope (not a separate compilation unit).

This moves the complexity of compiling inlined comprehensions from codegen to the symbol table
construction.

It removes the smell of the compiler modifying the symbol table in codegen.

iritkatriel and others added 2 commits September 2, 2026 12:12
…l tables

Co-authored-by: Cursor <cursoragent@cursor.com>

read-the-docs-community Bot commented Sep 2, 2026 •
edited
Loading

Copy link
Copy Markdown

carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This looks great! Thank you for working on this ❤️

Comment thread Python/symtable.c Outdated
Comment thread Python/symtable.c Outdated
Comment thread Python/codegen.c
Comment thread Python/symtable.c Outdated
Comment thread Python/symtable.c Outdated

JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Very nice! This also fixes #121377 and #156664, but doesn't add tests for their reproducers. Can you add those tests?

I have a PR for the latter almost ready at #156691, but I'm happy to retire it in favor of this PR. @carljm @iritkatriel what do you think about backporting either my change or others? This PR feels a bit much to backport, but we may want to backport fixes for some of the bugs. 156691 is also a somewhat invasive change though.

Codex found that this now fails:

import sys

def outer(x):
    def inner():
        return [(lambda: x, dict(**sys._getframe().f_locals))
                for x in x]
    return inner()

outer([1])

With TypeError: dict() got multiple values for keyword argument 'x'.. #156691 has a larger change to framelocalsproxy to deal with this sort of thing; you may want to incorporate its approach.

Copy link
Copy Markdown
Member

Another relevant bug is #156091 but that one still crashes under this PR.

carljm commented Sep 4, 2026

Copy link
Copy Markdown
Member

I'd be cautious about backporting an invasive fix for #156664. It's clearly a rarely-encountered edge case, given that nobody other than PyPy's test suite discovered the bug since 3.12, and the risk of introducing new bugs in an invasive fix seems high (particularly without this refactor in place.) I think "better the bugs you know than the ones you don't" applies in this case.

Copy link
Copy Markdown
Member Author

Codex did find a few issues.

Impressive!

carljm commented Sep 21, 2026

Copy link
Copy Markdown
Member

@iritkatriel I see all the inline comments are marked resolved, so I take it this is ready for another review?

carljm self-requested a review September 21, 2026 14:50

Copy link
Copy Markdown
Member Author

@iritkatriel I see all the inline comments are marked resolved, so I take it this is ready for another review?

I think so. Thanks.

carljm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is excellent, thank you!!

Comment thread Python/symtable.c
Comment thread Objects/frameobject.c
return NULL;
}
// An inlined comprehension cell can share a name with a free var.
PyObject *seen = PySet_New(NULL);

carljm Sep 21, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I wonder if we need to do any compilation performance testing of this PR on release builds? We are now doing this set allocation and per-name deduplication for every frame, including ordinary functions with no comprehensions, cells, or free variables. (Same for values(), items(), and len().)

If this cost is totally invisible in real workloads, maybe this is fine and we just keep the code simpler. But it also seems quite feasible to limit the duplicate-tracking cost to code objects that could actually be affected by it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Good point, though I think this code impacts introspection tools rather than the compiler. Let me see if it's easy to optimize.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Doable, but not trivial. I'd like to defer the optimisation to a separate PR.

Comment thread InternalDocs/inlined_comprehensions.md Outdated
@@ -0,0 +1,131 @@
Inlined comprehensions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is super helpful, thank you

iritkatriel added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Sep 22, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @iritkatriel for commit be7f556 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F156819%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Sep 22, 2026
iritkatriel added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Sep 22, 2026

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @iritkatriel for commit b61c46f 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F156819%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Sep 22, 2026
iritkatriel merged commit a9d42dc into python:main Sep 23, 2026
90 checks passed

Copy link
Copy Markdown
Member Author

Many thanks to the people and bots. This was fun.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor the implementation of inlined comprehensions

4 participants


Back | FazBrowse Home | New Git URL