◐ Shell
clean mode source ↗

Fix lambda expression's unexpected `UnboundLocalError` by jopemachine · Pull Request #4226 · RustPython/RustPython

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.

>>>>> class X:
.....   v = 999
.....   f = lambda x=v:x
..... 
>>>>> 
  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