[New-bugs-announce] [issue20507] TypeError from str.join has no message

Gareth Rees report at bugs.python.org
Tue Feb 4 12:09:02 CET 2014


New submission from Gareth Rees:

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.

----------
components: Interpreter Core
files: join.patch
keywords: patch
messages: 210200
nosy: Gareth.Rees
priority: normal
severity: normal
status: open
title: TypeError from str.join has no message
type: behavior
versions: Python 3.4
Added file: http://bugs.python.org/file33900/join.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20507>
_______________________________________


More information about the New-bugs-announce mailing list