9

Are generators supported in RPython, because I just read something in PyPy's documentation that says they are not

PyPy Doc - Coding Guide

They seem easy to be translated to a statically typed language like C because every generation step is generated in function call.

Can someone explain why ? Or shed some more light on the subject. I am currently trying to learn the basics of writing RPython safe code.

costy.petrisor
  • 783
  • 7
  • 17
  • 1
    This is an orthogonal question, but for what language are you writing an interpreter? If you're not, then RPython is probably the wrong solution. – DSM Jun 08 '11 at 10:17
  • I am not writing an interpreter, as I said I want to know tips for writing safe RPython code, and while I was reading the PyPy Coding Guide I got intrigued by this generator thing – costy.petrisor Jun 08 '11 at 11:38

1 Answers1

22

Generators are not supported simply because they were not needed at the time. The problem is not really having a roughly equivalent functionality in C, but needing to keep a frame of generator alive. Since RPython frames are translated to C frames, to support full python generators you would need some support for getting C frame and copy it somewhere else, or some equivalent.

This was simply hard/not needed and was not implemented.

fijal
  • 3,190
  • 18
  • 21
  • When you say 'to keep a generator frame alive' do you refer of generators based on yield ? – costy.petrisor Jun 08 '11 at 12:43
  • Can't generators implemented with yeild be transformed in-memory to a class-based generator ? Which I suppose does not need a frame of it's own – costy.petrisor Jun 08 '11 at 12:44
  • @costy: I'm pretty sure such a transformation would be possible (similar to [lambda lifting](http://en.wikipedia.org/wiki/Lambda_lifting) perhaps), but even then it would be a lot of work. –  Jun 08 '11 at 18:32
  • 1
    yes, I talk about generators based on yield. Transformation is of course possible, it's just that we didn't care. General-purpose iterators are more interesting and yet not supported by RPython btw – fijal Jun 10 '11 at 11:28