Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-104374: Remove access to class scopes for inlined comprehensions#104528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 10 commits intopython:mainfromJelleZijlstra:classcomp2
May 18, 2023

Conversation

JelleZijlstra
Copy link
Member

@JelleZijlstraJelleZijlstra commentedMay 16, 2023
edited by bedevere-bot
Loading

Copy link
Member

@carljmcarljm left a comment

Choose a reason for hiding this comment

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

This doesn't look bad at all! Thanks for pursuing it. I guess we can present both this and#104519 as options.

@JelleZijlstra
Copy link
MemberAuthor

Found the following crash with a variation of Carl's fuzzer:

>>> class a: [a for a in [b] for b[[b for _ in [] if a if b]] in [2]]... SystemError: compiler_lookup_arg(name='a') with reftype=1 failed in <module>; freevars of code a: ('a',)

This doesn't reproduce on main, so it's new from this PR.

@JelleZijlstra
Copy link
MemberAuthor

Found a different issue trying to simplify it:

>>> class C: [a for a in [] if [a for _ in []]]... SystemError: _PyST_GetScope(name='a') failed: unknown scope in unit <module> (5033699440); symbols: {'C': 2050}; locals: {}; globals: {}

@JelleZijlstra
Copy link
MemberAuthor

The problem occurs if it's a cell var in the outer comprehension and a free var in the inner one. In that case we should just do LOAD_FAST, but right now we're probably emitting _DEREF opcodes that explode because those cells don't exist in the class namespace.

@carljm
Copy link
Member

I pushed a fix for the above issues; it's just a matter of correcting how we decide if a comprehension is "in a class block." Looking only atc->u->u_ste->ste_type gives the wrong answer for nested comprehensions; they are inside their outer comprehension, so never in a class block. We need to usec->u->u_ste->ste_type == ClassBlock && !c->u->u_in_inlined_comp.

JelleZijlstra and mokko reacted with heart emoji


def test_nested_free_var_in_iter(self):
code = """
items = [_C for _C in [1] for [0, 1][[x for x in [1] if _C][0]] in [2]]
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Would be nice if we could test here that the name resolution in the inner listcomp here is right. I'll try to come up with some more tests around nested listcomps.

carljm reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

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

Are you still planning to add more tests here, or improve this one?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Yes, will push some more tests today or tomorrow. If you'd prefer to merge this now we can also do that; I can just write more tests in another PR.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think if we get known fixes merged sooner it's easier to do more effective fuzz testing; if we find more issues we can fix them in separate PRs, and we can add more tests that we think of in separate PRs.

JelleZijlstra reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Just added two more tests and turned on automerge.

carljm reacted with thumbs up emoji
@JelleZijlstra
Copy link
MemberAuthor

Now this segfaults:

class a:    def a():        class a:            [(lambda : (a := a[(a := 2)])[b]) for b in (lambda b, a: 7)[a]]            [][2] = b            (1)[lambda a: a] = 4            (2)[2] = b = a        (4)[lambda b, a: b] = a = lambda : 1

Haven't tried to minimize it, but it's a compiler crash, something related to the cell/freevars:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x8)  * frame #0: 0x000000010008b33c python.exe`Py_TYPE(ob=0x0000000000000000) at object.h:204:16 [opt]    frame #1: 0x000000010008ca8c python.exe`Py_IS_TYPE(ob=<unavailable>, type=0x000000010042d148) at object.h:235:12 [opt]    frame #2: 0x000000010008db38 python.exe`_PyCode_ConstantKey(op=0x0000000000000000) at codeobject.c:2163:11 [opt]    frame #3: 0x000000010008dd00 python.exe`_PyCode_ConstantKey(op=0x0000000103557a70) at codeobject.c:2225:24 [opt]    frame #4: 0x0000000100173334 python.exe`_PyCompile_ConstCacheMergeOne(const_cache=0x0000000103023890, obj=0x000000016fdfedd8) at compile.c:7380:21 [opt]    frame #5: 0x000000010014d88c python.exe`makecode(umd=0x0000000103407bb0, a=0x000000016fdfee50, const_cache=0x0000000103023890, constslist=0x000000010354b750, maxdepth=5, nlocalsplus=2, code_flags=0, filename=0x0000000103077e20) at assemble.c:569:9 [opt]    frame #6: 0x000000010014d5b8 python.exe`_PyAssemble_MakeCodeObject(umd=0x0000000103407bb0, const_cache=0x0000000103023890, consts=0x000000010354b750, maxdepth=5, instrs=<unavailable>, nlocalsplus=2, code_flags=0, filename=0x0000000103077e20) at assemble.c:598:14 [opt]

@JelleZijlstra
Copy link
MemberAuthor

JelleZijlstra commentedMay 17, 2023
edited
Loading

Reduced:

defa():classa:        [(lambda :b)forbin [a]]print(b)
carljm reacted with thumbs up emoji

@JelleZijlstra
Copy link
MemberAuthor

This also reproduces on main (without this PR) and with two functions, so it's not just related to class scopes:

defa():defa():        [(lambda :b)forbin [a]]print(b)

Crashes on current main with the same backtrace as above.

carljm, itamaro, and erlend-aasland reacted with thumbs up emoji

@carljm
Copy link
Member

Thanks, I'll open a separate issue and PR for this.

JelleZijlstra, itamaro, erlend-aasland, and mokko reacted with thumbs up emoji


def test_nested_free_var_in_iter(self):
code = """
items = [_C for _C in [1] for [0, 1][[x for x in [1] if _C][0]] in [2]]
Copy link
Member

Choose a reason for hiding this comment

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

Are you still planning to add more tests here, or improve this one?

@JelleZijlstraJelleZijlstraenabled auto-merge (squash)May 18, 2023 04:55
@JelleZijlstraJelleZijlstra merged commit662aede intopython:mainMay 18, 2023
@JelleZijlstraJelleZijlstra deleted the classcomp2 branchMay 18, 2023 05:22
@JelleZijlstraJelleZijlstra restored the classcomp2 branchSeptember 10, 2024 23:37
@JelleZijlstraJelleZijlstra deleted the classcomp2 branchDecember 3, 2024 02:02
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@carljmcarljmcarljm approved these changes

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

@iritkatrieliritkatrielAwaiting requested review from iritkatrieliritkatriel is a code owner

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@JelleZijlstra@carljm@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp