Index: Lib/test/test_generators.py =================================================================== --- Lib/test/test_generators.py (revision 46040) +++ Lib/test/test_generators.py (working copy) @@ -382,7 +382,7 @@ >>> type(i) >>> [s for s in dir(i) if not s.startswith('_')] -['close', 'gi_frame', 'gi_running', 'next', 'send', 'throw'] +['close', 'gi_code', 'gi_frame', 'gi_running', 'next', 'send', 'throw'] >>> print i.next.__doc__ x.next() -> the next value, or raise StopIteration >>> iter(i) is i @@ -899,6 +899,24 @@ >>> print g.next() Traceback (most recent call last): StopIteration + + +Test the gi_code attribute + +>>> def f(): +... yield 5 +... +>>> g = f() +>>> g.gi_code is f.func_code +True +>>> g.next() +5 +>>> g.next() +Traceback (most recent call last): +StopIteration +>>> g.gi_code is f.func_code +True + """ # conjoin is a simple backtracking generator, named in honor of Icon's Index: Objects/genobject.c =================================================================== --- Objects/genobject.c (revision 46040) +++ Objects/genobject.c (working copy) @@ -11,6 +11,7 @@ gen_traverse(PyGenObject *gen, visitproc visit, void *arg) { Py_VISIT((PyObject *)gen->gi_frame); + Py_VISIT(gen->gi_code); return 0; } @@ -35,6 +36,7 @@ _PyObject_GC_UNTRACK(self); Py_CLEAR(gen->gi_frame); + Py_CLEAR(gen->gi_code); PyObject_GC_Del(gen); } @@ -285,6 +287,7 @@ static PyMemberDef gen_memberlist[] = { {"gi_frame", T_OBJECT, offsetof(PyGenObject, gi_frame), RO}, {"gi_running", T_INT, offsetof(PyGenObject, gi_running), RO}, + {"gi_code", T_OBJECT, offsetof(PyGenObject, gi_code), RO}, {NULL} /* Sentinel */ }; @@ -356,7 +359,9 @@ return NULL; } gen->gi_frame = f; - gen->gi_running = 0; + Py_INCREF(f->f_code); + gen->gi_code = (PyObject *)(f->f_code); + gen->gi_running = 0; gen->gi_weakreflist = NULL; _PyObject_GC_TRACK(gen); return (PyObject *)gen; Index: Include/genobject.h =================================================================== --- Include/genobject.h (revision 46040) +++ Include/genobject.h (working copy) @@ -18,6 +18,9 @@ /* 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;