Archives
Tags
android beauty bible blogging brisbane community compiling composition design development formatting framework free God google hate highlights i3 ideas javascript laptop mercurial music oop politics programming programming languages public transport pygame pyj python refactoring scripting software story subversion testing tips tools trosnoth troubleshooting twisted ubuntu web window managerLinks
Monthly Archives: March 2012
Something Fishy with Python ASTs
|
1 2 3 4 5 6 7 8 9 10 |
$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import ast >>> m = ast.parse('from __future__ import division') >>> m <_ast.Module object at 0xb764418c> >>> compile(m, '<string>', 'exec') <code object <module> at 0xb7636e78, file "<string>", line 1> |
Ok, so far so good. Now comes the interesting bit.
|
1 2 3 4 5 6 7 8 9 |
>>> import pickle >>> m2 = pickle.loads(pickle.dumps(m)) >>> m2 <_ast.Module object at 0xb7651c0c> >>> compile(m2, '<string>', 'exec') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1 SyntaxError: from __future__ imports must occur at the beginning of the file |
When you parse an AST with a __future__ import, it looks as if it stores some hidden flags that suppress this error. These flags, if they exist, are … Continue reading