Co-routines

Duncan Booth duncan at NOSPAMrcp.co.uk
Thu Jul 17 17:37:23 CEST 2003


<thewrights at ozemail.com.au> wrote in
news:pBxRa.308$fF.9875 at nnrp1.ozemail.com.au: 

> OK. Thanks.  I was just giving some simple examples...but I hope this
> is better:
> 
> From the python grammar:
> 
>     funcdef    ::= "def" funcname "(" [parameter_list] ")" ":" suite
> 
> I want the statements in <suite> to be executed on a statement by
> statement basis, with no particular restrictions on *what* those
> statements are. It's the execution stepping I'm interested in... the
> I/O in my example was just that. I understand that there will be all
> the problems on contention/race etc (just like if the functions were
> going in separate threads) 

The only way I can think to implement this 'easily' would be to use the 
parser module to parse your source code, then walk the parse tree looking 
for statements inside one of your functions and add a yield_stmt after 
every other statement. Then compile the tree into bytecode and execute it.

Of course your users may be surprised to find that they cannot directly 
call any of their functions from another function. You could do a further 
rewrite on the tree to convert obvious calls to your users' functions into 
for loops.

Why?

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list