Generators in C code

G

Gabriel Genellina

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!
 
R

Raymond Hettinger

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
 
G

Gabriel Genellina

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top