2009/3/18 Anoop Saldanha <[email protected]>:

[snip]

>
> But
>
> cdef int g():
>      print "g called"
>      return 5
>
> def f():
>     cdef int i
>     for i in range(g()):
>         print i
>     print i
>
> uses a ForFromStatNode for the "for in" loop!

I'm not a core developer, so this is open to correction, but putting
in some PrintTree() calls into
Cython/Compiler/Main.py:create_pipeline() shows that it starts out as
a ForInStatNode and is changed to a ForFromStatNode.  Perhaps because
cython inferrs that g returns a C int and it triggers the 'range'
optimization.

Before any transformations:

- (root): ModuleNode(pos=(foo.pyx:1:0))
  - body: StatListNode(pos=(foo.pyx:1:0))
    - stats[0]: CFuncDefNode(pos=(foo.pyx:1:5))
     [removing cdef int g() stuff]
    - stats[1]: DefNode(name="f")
      - body: StatListNode(pos=(foo.pyx:6:4))
        - stats[0]: CVarDefNode(pos=(foo.pyx:6:9))
          - base_type: CSimpleBaseTypeNode(pos=(foo.pyx:6:9))
          - declarators[0]: CNameDeclaratorNode(pos=(foo.pyx:6:13))
        - stats[1]: ForInStatNode(pos=(foo.pyx:8:4)) <== starts out as
as ForInStatNode
          [snip rest of def f() function body]

After all transformations:

- (root): ModuleNode(pos=(foo.pyx:1:0))
  - body: StatListNode(pos=(foo.pyx:1:0))
    - stats[0]: CFuncDefNode(pos=(foo.pyx:1:5))
       [snip func body]
    - stats[1]: DefNode(name="f")
      - body: StatListNode(pos=(foo.pyx:6:4))
        - stats[0]: ForFromStatNode(pos=(foo.pyx:8:4))  <== Changed to
a ForFromStatNode.
          - target: NameNode(type=<CNumericType int>, name="i")
          - bound1: IntNode(type=<CNumericType long>)
          - bound2: SimpleCallNode(type=<CNumericType int>)
            - function: NameNode(type=<CFuncType <CNumericType int>
[]>, name="g")
          - step: IntNode(type=<CNumericType long>)
          - body: StatListNode(pos=(foo.pyx:9:8))
            - stats[0]: PrintStatNode(pos=(foo.pyx:9:8))
              - arg_tuple: TupleNode(type=<PyTuple_Type>)
                - args[0]: CoerceToPyTypeNode(type=<PyObjectType>)
                  - arg: NameNode(type=<CNumericType int>, name="i")
        - stats[1]: PrintStatNode(pos=(foo.pyx:10:4))
          - arg_tuple: TupleNode(type=<PyTuple_Type>)
            - args[0]: CoerceToPyTypeNode(type=<PyObjectType>)
              - arg: NameNode(type=<CNumericType int>, name="i")

Kurt
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to