Author gdr@garethrees.org
Recipients gdr@garethrees.org
Date 2014-02-04.11:09:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1391512142.14.0.683696819623.issue20507@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2014-02-04 11:09:02gdr@garethrees.orgsetrecipients: + gdr@garethrees.org
2014-02-04 11:09:02gdr@garethrees.orgsetmessageid: <1391512142.14.0.683696819623.issue20507@psf.upfronthosting.co.za>
2014-02-04 11:09:01gdr@garethrees.orglinkissue20507 messages
2014-02-04 11:09:01gdr@garethrees.orgcreate