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: wrong TypeError traceback in generator expressions
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: mwh Nosy List: euske, mwh, rhettinger
Priority: normal Keywords:

Created on 2005-10-14 20:25 by euske, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
1327110-fix.diff mwh, 2005-10-16 12:25 mwh's fix
Messages (6)
msg26600 - (view) Author: Yusuke Shinyama (euske) Date: 2005-10-14 20:25
In the following session, the TypeError traceback of '
'.join( foo(i) for i in range(10) ) is wrong. The cause
is not because of being a generator, but of its
manually created exception.
--------------------------
Python 2.4.2 (#1, Oct 14 2005, 16:08:57)
[GCC 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)] on
linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> def foo(x): raise TypeError('foo!')
...
>>> def bar(x): raise ValueError('bar!')
...
>>> ' '.join( foo(i) for i in range(10) )
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: sequence expected, generator found
>>> ' '.join( bar(i) for i in range(10) )
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in <generator expression>
  File "<stdin>", line 1, in bar
ValueError: bar!
msg26601 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-10-14 20:57
Logged In: YES 
user_id=80475

This isn't an error -- it was a design decision.  It is not
unusual to have a situation arise in Python where a high
level routine competes with a low level routine over which
is in the best position to provide the most useful error
message.  The low level routine typically knows the
proximate cause.  The high level routine typically knows
what the user was trying to do.  

In the case of str.join(), the high level routine usually
makes the most informative error message; however, it is
sometimes off the mark.

Will revisit the design decision to see if it should be
changed.  Lowering the priority because the code is working
as designed, the error type is correct, and it is not clear
that any change is warranted.
msg26602 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-10-16 12:25
Logged In: YES 
user_id=6656

Oh come on, this is just a bad idea (esp so in this case; the error message 
that you get for e.g. ''.join(1) is "iteration over non-sequence" which pretty 
clear -- I think this may have improved since the introduction of the 
PySequence_Fast API).

Here's a patch (with tests) that I'll check in after Jeremy has merged the 
ast-branch unless you talk very fast :)
msg26603 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-10-16 14:24
Logged In: YES 
user_id=80475

I don't think this should be backported.
msg26604 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-10-16 14:28
Logged In: YES 
user_id=6656

Agreed.
msg26605 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-10-21 11:46
Logged In: YES 
user_id=6656

Ok, this is checked in as:

Objects/stringobject.c revision 2.235
Objects/unicodeobject.c revision 2.235
Lib/test/test_string.py revision 1.28
Lib/test/string_tests.py revision 1.46

Thanks for the report.
History
Date User Action Args
2022-04-11 14:56:13adminsetgithub: 42486
2005-10-14 20:25:24euskecreate