Issue 795947: Partial implementation of generator comprehensions (original) (raw)
Hello.
Recently, Generator Comprehensions were mentioned again on python-list. I have written an implementation for the compiler module. To try it out, however, you must be able to rebuild Python from source, because it also requires a change to Grammar.
- Edit Python-2.3/Grammar/Grammar and add an alternative to the "listmaker" production: -listmaker: test ( list_for | (',' test)* [','] ) +listmaker: test ( list_for | (',' test)* [','] ) | 'yield' test list_for
1.5. Now [yield None for x in []] parses, but crashes the written-in-C compiler: >>> [yield None for x in []] SystemError: com_node: unexpected node type
Apply the patch below to Lib/compiler
Use compiler.compile to compile code with generator comprehensions: from compiler import compile import dis
code = compile(""" def f(): gg = [yield (x,y) for x in range(10) for y
in range(10) if y > x] print gg, type(gg), list(gg) """, "", "exec") exec code dis.dis(f.func_code.co_consts[1]) f()
- It's possible to write code so that import uses compiler.compile instead of the written-in-C compiler, but I don't have this code handy. Also, a test suite is needed, and presumably a written-in-C implementation as well. (option 2: make the compiler.compile interface the standard compiler, and let the builtin compiler support a Python subset sufficient to bootstrap the written-in-python compiler, or arrange to ship .pyc of the compiler package and completely get rid of the written-in-C compiler)