[Patches] [ python-Patches-795947 ] Partial implementation of generator comprehensions

SourceForge.net noreply at sourceforge.net
Sat Aug 30 17:52:27 EDT 2003


Patches item #795947, was opened at 2003-08-27 07:48
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=795947&group_id=5470

Category: Parser/Compiler
Group: Python 2.4
>Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Jeff Epler (jepler)
Assigned to: Nobody/Anonymous (nobody)
Summary: Partial implementation of generator comprehensions

Initial Comment:
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.

1. 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

2. Apply the patch below to Lib/compiler

3. 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)
    """, "<None>", "exec")
    exec code
    dis.dis(f.func_code.co_consts[1])
    f()

4. 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)


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-08-30 18:52

Message:
Logged In: YES 
user_id=80475

Since the pep has been rejected, am closing the patch.
But will create a link to it from the PEP (note, the patch stays 
on the record even after it is closed).

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=795947&group_id=5470



More information about the Patches mailing list