classification
Title: TypeError from str.join has no message
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, gdr@garethrees.org, pitrou, python-dev, serhiy.storchaka, thatiparthy
Priority: normal Keywords: patch

Created on 2014-02-04 11:09 by gdr@garethrees.org, last changed 2014-02-15 18:19 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
join.patch gdr@garethrees.org, 2014-02-04 11:09 review
Messages (5)
msg210200 - (view) Author: Gareth Rees (gdr@garethrees.org) * (Python triager) Date: 2014-02-04 11:09
If you pass an object of the wrong type to str.join, Python raises a
TypeError with no error message:

    Python 3.4.0b3 (default, Jan 27 2014, 02:26:41) 
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> ''.join(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError

It is unnecessarily hard to understand from this error what the
problem actually was. Which object had the wrong type? What type
should it have been? Normally a TypeError is associated with a message
explaining which type was wrong, and what it should have been. For
example:

    >>> b''.join(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only join an iterable

It would be nice if the TypeError from ''.join(1) included a message
like this.

The reason for the lack of message is that PyUnicode_Join starts out
by calling PySequence_Fast(seq, "") which suppresses the error message
from PyObject_GetIter. This commit by Tim Peters is responsible:
<http://hg.python.org/cpython/rev/8579859f198c>. The commit message
doesn't mention the suppression of the message so I can assume that it
was an oversight.

I suggest replacing the line:

    fseq = PySequence_Fast(seq, "");

in PyUnicode_Join in unicodeobject.c with:

    fseq = PySequence_Fast(seq, "can only join an iterable");

for consistency with bytes_join in stringlib/join.h. Patch attached.
msg210207 - (view) Author: Srinivas Reddy Thatiparthy(శ్రీనివాస్ రెడ్డి తాటిపర్తి) (thatiparthy) * Date: 2014-02-04 11:46
The exact behavior is present in 2.7 version too. So tagging 2.7 to 3.4
msg210273 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-02-04 22:33
Looks good to me.
msg211285 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-02-15 18:03
New changeset b9947b68ab61 by Benjamin Peterson in branch '3.3':
give non-iterable TypeError a message (closes #20507)
http://hg.python.org/cpython/rev/b9947b68ab61

New changeset f8e1fdf79823 by Benjamin Peterson in branch '2.7':
give non-iterable TypeError a message (closes #20507)
http://hg.python.org/cpython/rev/f8e1fdf79823

New changeset b077ee45f14f by Benjamin Peterson in branch 'default':
merge 3.3 (#20507)
http://hg.python.org/cpython/rev/b077ee45f14f
msg211286 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-02-15 18:19
There are similar issues in Objects/dictobject.c:1893, Modules/_elementtree.c:1045 and Modules/_elementtree.c:1662.
History
Date User Action Args
2014-02-15 18:19:47serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg211286
2014-02-15 18:03:28python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg211285

resolution: fixed
stage: patch review -> resolved
2014-02-15 15:58:00ezio.melottisetnosy: + ezio.melotti
2014-02-04 22:33:30pitrousetmessages: + msg210273
2014-02-04 11:54:32serhiy.storchakasetnosy: + pitrou
stage: patch review

versions: - Python 3.1, Python 3.2
2014-02-04 11:46:20thatiparthysetnosy: + thatiparthy

messages: + msg210207
versions: + Python 3.1, Python 2.7, Python 3.2, Python 3.3
2014-02-04 11:09:02gdr@garethrees.orgcreate