[ python-Bugs-970459 ] Generators produce wrong exception

SourceForge.net noreply at sourceforge.net
Sat Jun 12 04:54:52 EDT 2004


Bugs item #970459, was opened at 2004-06-10 09:58
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=970459&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Closed
>Resolution: Duplicate
Priority: 5
Submitted By: Anders Lehmann (anders_lehmann)
Assigned to: Nobody/Anonymous (nobody)
Summary: Generators produce wrong exception

Initial Comment:
The following script :

def f():
    yield "%s" %('Et','to')
    
for i in f(): print i

will produce the following traceback in Python 2.3.4

Traceback (most recent call last):
  File "python_generator_bug.py", line 6, in ?
    b+=f()
TypeError: argument to += must be iterable

Where I would expect a:

TypeError : not all arguments converted during string 
formatting.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-06-12 03:54

Message:
Logged In: YES 
user_id=80475

Okay, I was able to reproduce this under Py2.3.4 but not
Py2.4a0 where it appears to have already been fixed.   

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

>>> def f():
        yield "%s" %('Et','to')
>>> b = []
>>> b += f()

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in -toplevel-
    b += f()
TypeError: argument to += must be iterable


While this particular example has been fixed, others similar
situations exist.  Fixing them is not easy (there are many
places where a one error message gets trounced by another
message from an enclosing function which is trying to
provide more information).  Leaving this bug report as
closed because the specific case has been fixed and because
there is another open bug report for the more general case.

----------------------------------------------------------------------

Comment By: Anders Lehmann (anders_lehmann)
Date: 2004-06-12 03:15

Message:
Logged In: YES 
user_id=1060856

I am sorry  that I overoptimized the script that should 
demonstrate the error.

The problem was discovered with the :
b=[]
b+=f()

I find it very confusing that the reported error is that the 
function is not iterable ( which isnt due to a format error ).

When the generator is more complex than the example the 
exception doesn't help in locating the bug.

But if Tim thinks it is the correct behaviour I will close the bug.


----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2004-06-12 02:03

Message:
Logged In: YES 
user_id=80475

Ander, if this resolves your issue, please close this bug.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2004-06-10 10:38

Message:
Logged In: YES 
user_id=31435

The script you gave does produce the message you expect:

>>> def f():
...     yield "%s" %('Et','to')
...
>>> for i in f(): print i
...
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in f
TypeError: not all arguments converted during string 
formatting
>>>

The traceback you gave contains the line

b+=f()

which doesn't appear in the script you gave.  If the script 
you *actually* ran had, for example,

>>> b = []
>>> b += f()

Then

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: argument to += must be iterable
>>>

is an appropriate exception.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=970459&group_id=5470



More information about the Python-bugs-list mailing list