<blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<span style="font-family: 'courier new', monospace; background-color: transparent; ">>>> def x(nonlocal_var):</span></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">... def y():</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">... z = lambda *, keyword_only=nonlocal_var: None</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">... return y</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">... </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">>>> x(None)()</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">Traceback (most recent call last):</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> File "<stdin>", line 1, in <module></font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> File "<stdin>", line 3, in y</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">SystemError: no locals when loading 'nonlocal_var'</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">>>> dis.dis(x)</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 2 0 LOAD_CONST 1 (<code object y at 0x7f1fa5d57030, file "<stdin>", line 2>) </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 3 MAKE_FUNCTION 0 </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 6 STORE_FAST 1 (y) </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"><br></font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 4 9 LOAD_FAST 1 (y) </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 12 RETURN_VALUE </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">>>> dis.dis(x(None))</font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 3 0 LOAD_CONST 1 ('keyword_only') </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 3 LOAD_NAME 0 (nonlocal_var) </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 6 LOAD_CONST 2 (<code object <lambda> at 0x7f1fa626aa48, file "<stdin>", line 3>) </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 9 MAKE_FUNCTION 256 </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 12 STORE_FAST 0 (z) </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 15 LOAD_CONST 0 (None) </font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"> 18 RETURN_VALUE </font></blockquote><div> <span style="background-color: transparent; color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; ">Conclusion:</span></div>
<div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; background-color: rgba(255, 255, 255, 0.917969); "><span style="background-color: transparent; ">When setting defaults to keyword-only arguments in lambdas which are inside non-global scopes, cPython is unable to access <b>either the free variables or global scope</b> and raises SystemError.</span></div>
<div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; background-color: rgba(255, 255, 255, 0.917969); "><span style="background-color: transparent; "><br></span></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; background-color: rgba(255, 255, 255, 0.917969); ">
<span style="background-color: transparent; ">This is cool, though:</span></div><div style="background-color: rgba(255, 255, 255, 0.917969); "><span style="background-color: transparent; "><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"><font color="#222222">>>> def y():<br></font><font color="#222222">... global_var<br></font><font color="#222222">... z = lambda *, keyword_only=global_var: None<br>
</font><font color="#222222">... <br></font><font color="#222222">>>> y()<br></font><font color="#222222">>>> </font></font></blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace">>>> dis.dis(y)<br> 2 0 LOAD_GLOBAL 0 (global_var) <br> 3 POP_TOP <br> 3 4 LOAD_CONST 1 ('keyword_only') <br>
7 LOAD_GLOBAL 0 (global_var) <br> 10 LOAD_CONST 2 (<code object <lambda> at 0x7f1fa5d4fd78, file "<stdin>", line 3>) <br> 13 MAKE_FUNCTION 256 <br>
16 STORE_FAST 0 (z) <br> 19 LOAD_CONST 0 (None) <br> 22 RETURN_VALUE </font></blockquote></span></div><div style="background-color: rgba(255, 255, 255, 0.917969); ">
<span style="background-color: transparent; "><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<font face="'courier new', monospace"><font color="#222222">>>> def x(nonlocal_var):<br></font><font color="#222222">... def y():<br></font><font color="#222222">... nonlocal_var<br></font><font color="#222222">... z = lambda *, keyword_only=nonlocal_var: None<br>
</font><font color="#222222">... return y<br></font><font color="#222222">... <br></font><font color="#222222">>>> x(None)()<br></font><font color="#222222">>>> </font></font></blockquote><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; ">
What is happening is that LOAD_NAME is trying to access the value 'nonlocal_var' from y.__code__.co_freevars, but compilation doesn't push it there from the lambda, so adding a call to it causes it to work. The change of opcode implies that locality is decided before the opcodes are made, and so not being pushed to co_freevars changes the opcode.</div>
<div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; "><br></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; ">AKA:</div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; ">
<b>When setting defaults to keyword-only arguments in lambdas which are inside non-global scopes, cPython doesn't push the name to co_freevars.</b></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; ">
<br></div><div style="color: rgb(34, 34, 34); font-family: arial, sans-serif; font-size: 10px; ">Now - where shall I report this?</div></span></div>