Prajwal Suhas P wrote:
> Hi Dag,
> 
> I am currently looking at the tickets.
> 
> I was looking at this -
> 
> cdef int i, n
> n = 10
> for i in range(n):
>     if i == 5:
>         n *= 2
> print i
> 
> 
> The generated c code for this is -
> 
> __pyx_v_5test1_n = 10;
> 
>  for (__pyx_v_5test1_i = 0; __pyx_v_5test1_i < __pyx_v_5test1_n; 
> __pyx_v_5test1_i+=1) {
> 
>     __pyx_1 = (__pyx_v_5test1_i == 5);
>     if (__pyx_1) {
>         __pyx_v_5test1_n *= 2;
>         goto __pyx_L4;
>     }
>     __pyx_L4:;
> }
> 
> Can the var 'n' be held in another temp variable?
> 
> Something like this -
> 
> __pyx_v_5test1_n = 10;
> __pyx_v_5test1_const_n = 10;
> 
>  for (__pyx_v_5test1_i = 0; __pyx_v_5test1_i < __pyx_v_5test1_const_n; 
> __pyx_v_5test1_i+=1) {
> 
>     __pyx_1 = (__pyx_v_5test1_i == 5);
>     if (__pyx_1) {
>         __pyx_v_5test1_n *= 2;
>         goto __pyx_L4;
>     }
>     __pyx_L4:;
> }
> i--;
> 
> I haven't looked at the cython code which parses the loop and outputs 
> the c code.  I would really appreciate if you can give me a head start 
> into this and correct me if I am wrong.

You are correct, this is what you need to do. Some suggestions:

a) Add a flag to ForFromStatNode in Nodes.py, say, "freeze_endpoints".

b) IterationTransform in Optimize.py is what takes a "for i in 
range(...)" and turns it into a ForFromStatNode. Here you should turn 
the freeze_endpoints flag on when the ForFromStatNode is constructed.

c) If this flag is set, then ForFromStatNode.generate_execution_code 
must put the start or end points (or both, depending on the case) into 
temporary variables.

d) You can get hold of a temporary variable to use (like __pyx_t2) by 
calling code.funcstate.allocate_temp/release_temp. See Code.py for docs.

(Stefan: Yes, this can be done by temp handling nodes in the transform 
as well, but since the canonical way of doing that is a bit unclear/a 
mess at the moment I don't want to encourage that. This works as well, 
and is likely less code in the end.)

Another for-loop bug was also fixed with a bit of assistance on the 
mailing list, you may want to read through:

http://thread.gmane.org/gmane.comp.python.cython.devel/4195

Also please make sure you make a testcase in 
tests/bugs/something_T203.pyx (then run this with python runtests.py 
-T203 while developing).

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

Reply via email to