]> granicus.if.org Git - python/commitdiff
#2863: add gen.__name__ and add this name to generator repr().
authorGeorg Brandl <georg@python.org>
Thu, 15 May 2008 15:08:32 +0000 (15:08 +0000)
committerGeorg Brandl <georg@python.org>
Thu, 15 May 2008 15:08:32 +0000 (15:08 +0000)
Lib/test/test_generators.py
Lib/test/test_genexps.py
Misc/NEWS
Objects/genobject.c

index ab0fca05064b42ac35b523251b22e809c86207a2..7be0a90abf1bbfe4b0b1b614ef30dc7f1b470631 100644 (file)
@@ -917,6 +917,17 @@ StopIteration
 >>> 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
index 2598a7913ad4e1a4a38e492b3f7b54cd46ade8d0..0317955ab14bc2889a3747423d32aa12e7085d2a 100644 (file)
@@ -92,7 +92,7 @@ Verify that parenthesis are required when used as a keyword argument value
 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
 
index 829aac68d88ad8cb8e1e9d61fc0357bf4c4a8c13..7d3dcff75cabd6f31c521752d785fb2776ba410d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,11 @@ What's New in Python 2.6 beta 1?
 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
index 38682955c3ade3c3726c8922f099a400e4f9e257..3cd911d5f849077da7bbbbabbbfcc318ec887c9f 100644 (file)
@@ -281,6 +281,36 @@ gen_iternext(PyGenObject *gen)
 }
 
 
+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 @@ PyTypeObject PyGen_Type = {
        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 @@ PyTypeObject PyGen_Type = {
        (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 */