Tuesday, February 27, 2007

nonlocal implemented

I'm going to love Python 3000! Thomas Wouters and I implemented PEP 3104 tonight. It fixes a wart in the original nested scopes implementation that I did in 2001. In that version of nested scopes, we did not allow names defined in one function to be rebound in an enclosing function. It was impossible for the compiler to distinguish between an assignment that creates a local and an assignment that rebinds. Python 3000 will fix this using the nonlocal statement. I hate the name nonlocal, but no one has thought of a better name. Ka-Ping Yee wrote PEP 3104 and provided an exhaustive list of alternate names,

The code itself was quite simple. The only changes in compile.c were trivial. The symbol table needed more changes, because it had to recognize a new kind of declaration and propagate that information to the compiler. The symbol table uses a bit-field where a handful of the bits are used to represent the scope. I spent a lot of time scratching my head until I remember that I needed to increase the width of mask used to extract the scope-related bits. I think I'm not happy with the bit-field representation.

Pete Shinners and Neal Norwitz reviewed code and helped think of tests cases.

That wraps up a fun day of sprinting for me. I also closed several bugs and spent a few hours pouring over typeobject.c to fix some crasher bugs that Armin Rigo reported. I have a fix for one of them, but I want to do a little refactoring before I check it in. Next week, perhaps. I'm flying home first thing in the morning.

3 comments:

Anonymous said...

I would like to see this in Python 2.6

Jeremy Hylton said...

I expect we will backport most of the new language features that are backwards compatible, including this one.

Ping said...

Thanks for doing this! Great work.