Hey all,
Why does --python mode modify the behaviour of coroutines?
Guido is to blame. Python generators/coroutines work like this:
iter = coro() # first time calling for x = iter() ...# iterate
And that's what Parrot is doing too with the --python switch. But there is more ugliness: Python returns "None" if the generator is left through the end and a StopIteration is thrown (which you have to catch).
That's all really messy and needs definitely some cleanup. OTOH it was the most straight forward translation of Python bytecode.
$ parrot --python pcoro.txt StopIteration in file '(unknown file)' near line -1 ab $ cat pcoro.txt .sub __main__ .local object gen .local object res .local object iter
gen = global "_gen_g"
iter = gen() .local pmc None None = new .None loop: unless iter, ex res = shift iter if res == None goto ex print res goto loop ex: end .end
.sub _gen_g prototyped .local object res res = new PerlString
res = "a" .pcc_begin_yield .return res .pcc_end_yield
res = "b" .pcc_begin_yield .return res .pcc_end_yield
.local pmc None None = new .None .pcc_begin_return .return None .pcc_end_return .end