IDLE being too clever checking nonlocal declarations?
Steven D'Aprano
steve at pearwood.info
Tue Oct 22 01:57:02 EDT 2013
On Mon, 21 Oct 2013 23:26:28 -0400, Terry Reedy wrote:
> On 10/21/2013 7:52 PM, Steven D'Aprano wrote:
>> On Mon, 21 Oct 2013 15:51:56 -0400, Terry Reedy wrote:
>>
>>> On 10/21/2013 11:06 AM, Chris Angelico wrote:
>>>> Try typing this into IDLE:
>>>>
>>>>>>> def a():
>>>> def b():
>>>> nonlocal q
>>>> SyntaxError: no binding for nonlocal 'q' found
>>>
>>> If you submit those three lines to Python from the command line, that
>>> is what you see.
>>
>> Arguably, that's also too strict,
>
> As I quoted from the doc, it is an error for a program to contain a
> nonlocal with no referent. The reason is one only needs nonlocal to bind
> and unlike with 'global newname', it would be undefined where to do the
> binding.
Yep, I got that, but what I'm saying is that it is too strict to raise
the exception at the point where it sees "nonlocal q". The CPython
interpreter allows q to be defined inside function a but after function
b, e.g. this is allowed:
def a():
def b():
nonlocal q
q += 1
q = 2 # <=======
If IDLE and the code.py module requires q to be strictly defined before
function b, then it is too strict. Your analysis of the bug as being in
code.py seems plausible.
>> [steve at ando ~]$ python3.3 -c "def a():
>>> def b():
>>> nonlocal q
>>> q = 1
>>> "
>
> What system lets you do that? (See other thread about Windows not
> allowing that, because newline terminates the command even after ".) Is
> '>' a line continuation marker (like '...' in Python)?
Yes, sorry I should have said. That's bash, under Linux.
Here's another way:
steve at runes:~$ python3.3 -c "def a():^M def b():^M nonlocal q^M
q=1^Mprint(a() is None)"
True
Still bash under Linux (a different machine), the ^M is *not* a pair of
characters ^ followed by M but an actually newline, generated by typing
Ctrl-V Enter (that's the ENTER key, not the letters E n t e r).
In theory I should be able to get something working with \n escapes
instead of ^M, but I can't get it working. But I'm not an expect at bash's
arcane rules for quoting and escaping special characters.
--
Steven
More information about the Python-list
mailing list