The PyGenObject is the structure that wraps the Python generator.  The declaration is in the file Include/genobject.h.

typedef struct {
PyObject_HEAD
/* The gi_ prefix is intended to remind of generator-iterator. */

/* Note: gi_frame can be NULL if the generator is “finished” */
struct _frame *gi_frame;

/* True if generator is being executed. */
int gi_running;

/* The code object backing the generator */
PyObject *gi_code;

/* List of weak reference. */
PyObject *gi_weakreflist;
} PyGenObject;

You can observe the generator object contains a reference to the frame to which the function must enter after yielding.

Open the file Objects/genobject.c and insert a breakpoint in line number 385.

Enter the function:

>>>def gen(a):
while a < 10:
a += 1
yield

>>> a = gen(5)

You see that the breakpoint is trapped. Observe how the generator is created.

>> a.next()

And insert a breakpoint into line number 283. There is call to function gen_send_ex. Insert a breakpoint into line number 85, which is a call to the interpreter loop for the current frame and instruction pointer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s