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.

classification
Title: Generators produce wrong exception
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: anders_lehmann, rhettinger, tim.peters
Priority: normal Keywords:

Created on 2004-06-10 14:58 by anders_lehmann, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg21119 - (view) Author: Anders Lehmann (anders_lehmann) Date: 2004-06-10 14:58
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.
msg21120 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-06-10 15:38
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.
msg21121 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-06-12 07:03
Logged In: YES 
user_id=80475

Ander, if this resolves your issue, please close this bug.
msg21122 - (view) Author: Anders Lehmann (anders_lehmann) Date: 2004-06-12 08:15
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.
msg21123 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-06-12 08:54
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.
History
Date User Action Args
2022-04-11 14:56:04adminsetgithub: 40379
2004-06-10 14:58:07anders_lehmanncreate