[Python-checkins] r63320 - in python/trunk: Lib/test/test_generators.py Lib/test/test_genexps.py Misc/NEWS Objects/genobject.c

georg.brandl python-checkins at python.org
Thu May 15 17:08:32 CEST 2008


Author: georg.brandl
Date: Thu May 15 17:08:32 2008
New Revision: 63320

Log:
#2863: add gen.__name__ and add this name to generator repr().


Modified:
   python/trunk/Lib/test/test_generators.py
   python/trunk/Lib/test/test_genexps.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/genobject.c

Modified: python/trunk/Lib/test/test_generators.py
==============================================================================
--- python/trunk/Lib/test/test_generators.py	(original)
+++ python/trunk/Lib/test/test_generators.py	Thu May 15 17:08:32 2008
@@ -917,6 +917,17 @@
 >>> g.gi_code is f.func_code
 True
 
+
+Test the __name__ attribute and the repr()
+
+>>> def f():
+...    yield 5
+...
+>>> g = f()
+>>> g.__name__
+'f'
+>>> repr(g)  # doctest: +ELLIPSIS
+'<f generator object at ...>'
 """
 
 # conjoin is a simple backtracking generator, named in honor of Icon's

Modified: python/trunk/Lib/test/test_genexps.py
==============================================================================
--- python/trunk/Lib/test/test_genexps.py	(original)
+++ python/trunk/Lib/test/test_genexps.py	Thu May 15 17:08:32 2008
@@ -92,7 +92,7 @@
 Verify that parenthesis are required when used as a keyword argument value
 
     >>> dict(a = (i for i in xrange(10))) #doctest: +ELLIPSIS
-    {'a': <generator object at ...>}
+    {'a': <<genexp> generator object at ...>}
 
 Verify early binding for the outermost for-expression
 

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Thu May 15 17:08:32 2008
@@ -12,6 +12,11 @@
 Core and Builtins
 -----------------
 
+- Issue #2863: generators now have a ``gen.__name__`` attribute that equals
+  ``gen.gi_code.co_name``, like ``func.__name___`` that equals
+  ``func.func_code.co_name``.  The repr() of a generator now also contains
+  this name.
+
 - Issue #2831: enumerate() now has a ``start`` argument.
 
 - Issue #2801: fix bug in the float.is_integer method where a ValueError

Modified: python/trunk/Objects/genobject.c
==============================================================================
--- python/trunk/Objects/genobject.c	(original)
+++ python/trunk/Objects/genobject.c	Thu May 15 17:08:32 2008
@@ -281,6 +281,36 @@
 }
 
 
+static PyObject *
+gen_repr(PyGenObject *gen)
+{
+	char *code_name;
+	code_name = PyString_AsString(((PyCodeObject *)gen->gi_code)->co_name);
+	if (code_name == NULL)
+		return NULL;
+	return PyString_FromFormat("<%.200s generator object at %p>",
+				   code_name, gen);
+}
+
+
+static PyObject *
+gen_get_name(PyGenObject *gen)
+{
+	PyObject *name = ((PyCodeObject *)gen->gi_code)->co_name;
+	Py_INCREF(name);
+	return name;
+}
+
+
+PyDoc_STRVAR(gen__name__doc__,
+"Return the name of the generator's associated code object.");
+
+static PyGetSetDef gen_getsetlist[] = {
+	{"__name__", (getter)gen_get_name, NULL, NULL, gen__name__doc__},
+	{NULL}
+};
+
+
 static PyMemberDef gen_memberlist[] = {
 	{"gi_frame",	T_OBJECT, offsetof(PyGenObject, gi_frame),	RO},
 	{"gi_running",	T_INT,    offsetof(PyGenObject, gi_running),	RO},
@@ -306,7 +336,7 @@
 	0, 					/* tp_getattr */
 	0,					/* tp_setattr */
 	0,					/* tp_compare */
-	0,					/* tp_repr */
+	(reprfunc)gen_repr,			/* tp_repr */
 	0,					/* tp_as_number */
 	0,					/* tp_as_sequence */
 	0,					/* tp_as_mapping */
@@ -326,7 +356,7 @@
 	(iternextfunc)gen_iternext,		/* tp_iternext */
 	gen_methods,				/* tp_methods */
 	gen_memberlist,				/* tp_members */
-	0,					/* tp_getset */
+	gen_getsetlist,				/* tp_getset */
 	0,					/* tp_base */
 	0,					/* tp_dict */
 


More information about the Python-checkins mailing list