Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Generators in C code

0 views
Skip to first unread message

Gabriel Genellina

unread,
May 15, 2007, 12:55:59 AM5/15/07
to pytho...@python.org
Hi

I'd like to write a generator in C, callable from Python.
The only way to create a generator object I can see, is using
PyGen_New(frame) - and a frame object implies Python code, right? holding
global state and local variables and stack and a lot of stuff, so perhaps
there is no (easy) way I could write that in C code.
Or perhaps some way to execute a yield statement inside a C function?
Being able to resume execution at the same point later?
I feel I'm out of luck, but if someone could point some way to write a
generator in C, I'be very grateful!

--
Gabriel Genellina

Raymond Hettinger

unread,
May 15, 2007, 1:12:40 AM5/15/07
to
On May 14, 9:55 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:

> I feel I'm out of luck, but if someone could point some way to write a
> generator in C, I'be very grateful!

Perhaps the code in the itertools module will provide a good example
-- they behave like generators in many respects except that you are
responsible for tracking state and jumping to an appropriate resume
point. Being C, it won't be as convenient as Python generators, but
should be able to translate any generator into equivalent C.


Raymond Hettinger


Gabriel Genellina

unread,
May 15, 2007, 1:57:35 AM5/15/07
to pytho...@python.org
En Tue, 15 May 2007 02:12:40 -0300, Raymond Hettinger <pyt...@rcn.com>
escribió:

>> I feel I'm out of luck, but if someone could point some way to write a
>> generator in C, I'be very grateful!
>
> Perhaps the code in the itertools module will provide a good example
> -- they behave like generators in many respects except that you are
> responsible for tracking state and jumping to an appropriate resume
> point. Being C, it won't be as convenient as Python generators, but
> should be able to translate any generator into equivalent C.

Oh, thanks for pointing that! (I didn't know the itertools module was
written in C - I supposed it was a Python module).
After a brief reading, I think I'll use something similar to how tee and
teedataobject store current state.

--
Gabriel Genellina

0 new messages