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: Empty exception message of str.join
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: fossilet, pitrou, python-dev, r.david.murray, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-09-10 02:54 by fossilet, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
str_join_exception_message.diff fossilet, 2014-09-10 02:54 patch review
str_join_exception_message_1.diff fossilet, 2014-09-12 06:58 new patch that checks exception message review
test_for_35.diff fossilet, 2014-09-12 07:01 add test for 3.5 review
test_for_35-1.diff fossilet, 2014-09-15 08:33 review
Messages (11)
msg226675 - (view) Author: Yongzhi Pan (fossilet) * Date: 2014-09-10 02:54
In the 2.7 branch, the exception message of str.join is missing when the argument is non-sequence:

>>> ' '.join(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError

I fix this with a patch. After this:

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

I also add test to this case. Can the test also be added to 3.x branches?
msg226695 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-10 12:55
Thanks for the report and patch.

The test doesn't fail before your patch.  I think checkraises should be changed to make sure the message isn't empty.

And yes, the tests should be added to 3.5.
msg226808 - (view) Author: Yongzhi Pan (fossilet) * Date: 2014-09-12 07:01
I updated the patches. Since exceptions in 3 do not have a message attribute, I did not check them. Are they OK?
msg226818 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-12 13:33
You can check .args[0] in python3.

Can you include a complete patch for python3?  Your test_for_35 only has a change for test_bytes, not the ones for string_tests.
msg226830 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-12 17:58
> You can check .args[0] in python3.

Or str(cm.exception). This works on 2.7 too.
msg226904 - (view) Author: Yongzhi Pan (fossilet) * Date: 2014-09-15 08:33
I have updated the test for 3.5. The related tests pass after the patching. Are they OK now? 

There are two caveats: I did not update test_bytes in 2.7, and I did not add checkraises in test_bytes in 3.5.
msg226923 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-15 15:41
The str of the exception will always be nonblank (it will contain the string 'TypeError' at a minimum).  So you need to peel off the 'TypeError:' prefix before testing if you want to use str(e).  That's why I suggested using args[0], but I suppose that might be a little more fragile.
msg226943 - (view) Author: Yongzhi Pan (fossilet) * Date: 2014-09-16 08:34
I tested it and find str of an exception does not contain the exception type:

>>> a = TypeError()
>>> str(a)
''
>>> a = TypeError('b', 3)
>>> a.args
('b', 3)
>>> str(a)
"('b', 3)"

Am I missing some other circumstances?
msg226962 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-09-16 17:47
No, you are right and I am wrong.  I should have checked myself :)
msg227750 - (view) Author: Yongzhi Pan (fossilet) * Date: 2014-09-28 03:13
Do the patches still have problems?
msg227768 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-09-28 16:57
New changeset 0ad19246d16d by Benjamin Peterson in branch '2.7':
give exception a nice message (closes #22379)
https://hg.python.org/cpython/rev/0ad19246d16d

New changeset ab1570d0132d by Benjamin Peterson in branch '3.4':
check that exception messages are not empty (#22379)
https://hg.python.org/cpython/rev/ab1570d0132d

New changeset 78727a11b5ae by Benjamin Peterson in branch 'default':
merge 3.4 (#22379)
https://hg.python.org/cpython/rev/78727a11b5ae
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66573
2014-09-28 16:57:31python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg227768

resolution: fixed
stage: needs patch -> resolved
2014-09-28 03:13:36fossiletsetmessages: + msg227750
2014-09-16 17:47:47r.david.murraysetmessages: + msg226962
2014-09-16 08:34:46fossiletsetmessages: + msg226943
2014-09-15 15:41:08r.david.murraysetmessages: + msg226923
2014-09-15 08:33:49fossiletsetfiles: + test_for_35-1.diff

messages: + msg226904
2014-09-12 17:58:50serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg226830
2014-09-12 13:33:53r.david.murraysetmessages: + msg226818
2014-09-12 07:01:26fossiletsetfiles: + test_for_35.diff

messages: + msg226808
2014-09-12 06:58:33fossiletsetfiles: + str_join_exception_message_1.diff
versions: + Python 3.5
2014-09-11 08:02:57serhiy.storchakasetnosy: + pitrou
2014-09-10 12:55:55r.david.murraysetnosy: + r.david.murray
messages: + msg226695

components: + Interpreter Core, - ctypes
type: enhancement -> behavior
stage: needs patch
2014-09-10 02:54:51fossiletcreate