Skip to content

Conversation

@jopemachine
Copy link
Contributor

@jopemachine jopemachine commented Oct 16, 2022

This PR trying to fix #3934.

I think the compile context of the lambda expression should be resolved as Function after executing enter_function like in the compile_function_def.

The detailed explanations are in the above issue's comments.

Result

Example code:

>>>>> class X:
.....   v = 999
.....   f = lambda x=v:x
..... 
>>>>> 

Example code's bytecodes:

  1           0 LoadGlobal           (0, __name__)
              1 StoreLocal           (1, __module__)
              2 LoadConst            ("X")
              3 StoreLocal           (2, __qualname__)
              4 LoadConst            (None)
              5 StoreLocal           (3, __doc__)

  2           6 LoadConst            (999)
              7 StoreLocal           (4, v)

  3           8 LoadNameAny          (4, v)
              9 BuildTuple           (1, false)
             10 LoadConst            (<code object <lambda> at ??? file "a.py", line 3>)
             11 LoadConst            ("<lambda>")
             12 MakeFunction         (DEFAULTS)
             13 StoreLocal           (5, f)
             14 LoadConst            (None)
             15 ReturnValue

@youknowone
Copy link
Member

awesome, would you add the example code in #3934 under extra_tests/snippets?

@jopemachine
Copy link
Contributor Author

awesome, would you add the example code in #3934 under extra_tests/snippets?

Sure, I added the issue's example code directly.
If I should add more extra tests, please let me know for more work.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

because the bug is more like scope bug than lambda itself, I changed the test file name

@youknowone youknowone merged commit 72332c9 into RustPython:main Oct 17, 2022
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.

member field + lambda results unexpected UnboundLocalError

2 participants