This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add a gi_code attr to generators
Type: Stage:
Components: Interpreter Core Versions: Python 2.6
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: collinwinter, georg.brandl, gvanrossum, pje
Priority: normal Keywords: patch

Created on 2006-04-19 21:39 by collinwinter, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
gi_code.patch collinwinter, 2006-05-18 17:11 Add gi_code attr to generators, against r46040
NEWS.diff collinwinter, 2006-05-18 17:12 Add an item to Misc/NEWS about the gi_code attr, against r46040
Messages (7)
msg50081 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2006-04-19 21:39
In the test suite for one of my packages, I've used
something like gen.gi_frame.f_code.co_name to help make
human-readable assertions about when certain generators
are run deep inside the application. This was possible
because Python 2.4 guaranteed that gi_frame was always
a frame instance, even after the generator exhausted
itself. In Python 2.5, however, gi_frame is None when
the generator has run till exhaustion, meaning that I
can't always get to f_code.co_name.

I'd like to add a gi_code attribute to generators that
would allow users to access the code object behind the
generator, even when gi_frame is None. This attribute
would be read-only and would follow this rule:

>>> def f():
...     yield 5
>>> g = f()
>>> g.gi_code is f.func_code

The attached patch (against r45570) implements the
proposed attribute (in Include/genobject.h and
Objects/genobject.c) and adds test cases to
Lib/test/ for this attribute.
msg50082 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2006-04-29 21:11
Logged In: YES 

I've updated the patch; it's now against SVN r45808.
msg50083 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-04-30 11:17
Logged In: YES 

Phillip, do you have an opinion on this one?
msg50084 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2006-05-18 17:13
Logged In: YES 

In addition to updating the main patch to r46040, I've
included a diff against Misc/NEWS to make mention of the
gi_code attribute.

Any thoughts on getting this into 2.5a3?
msg61270 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-20 11:18
Guido, do you have an opinion? Seems like a nonproblematic and probably
useful change for 2.6.
msg61476 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-22 01:58
I noticed some whitespace issues in the patch, but apart from that I
have no objection.  (Not any particular enthusiasm, I have to admit -- I
don't quite get the use case, but as Georg remarks, it seems harmless.)
msg61710 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-26 14:14
Reformatted the patch to use tabs and committed as r60324.
Date User Action Args
2022-04-11 14:56:16adminsetgithub: 43254
2008-01-26 14:14:40georg.brandlsetstatus: open -> closed
assignee: gvanrossum -> georg.brandl
resolution: accepted
messages: + msg61710
2008-01-22 01:58:17gvanrossumsetmessages: + msg61476
2008-01-20 11:18:08georg.brandlsetassignee: pje -> gvanrossum
messages: + msg61270
nosy: + gvanrossum
2006-04-19 21:39:28collinwintercreate