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: str(CancelledError()) is empty
Type: enhancement Stage: resolved
Components: asyncio Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, lilydjwg, serhiy.storchaka, yselivanov
Priority: normal Keywords:

Created on 2021-10-29 16:48 by lilydjwg, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg405318 - (view) Author: lilydjwg (lilydjwg) * Date: 2021-10-29 16:48
When I try to print an asyncio.CancelledError object, I do not see it and I thought something went wrong.

CancelledError inherits from BaseException and all BaseException subclasses (e.g. SystemExit, KeyboardInterrupted) seem to return empty strings for str(e). While I never tried to print SystemExit or KeyboardInterrupted, I did try to print an CancelledError. It doesn't have a lot information but all other exceptions I tried to print return something so I expect CancelledError to be the same.

I know repr(e). I was just lazy and I expect Python to not be confusing even I'm lazy.
msg405324 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2021-10-29 17:16
I don't think that we need to change something with the current behavior.

It exists for years; very many Python exceptions return an empty string on `str(exc)` but return something useful on `repr(exc)`.

If you arguing to change the behavior -- all such exceptions should be changed, not CancelledError only.
msg405326 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-29 18:21
Concur with Andrew.
msg405355 - (view) Author: lilydjwg (lilydjwg) * Date: 2021-10-30 03:19
Oh, I find that many exceptions return an empty string when created without any arguments, but most raised ones do have a descriptive error message.

Yes, if it's going to change, we'd change all of them. But now I doubt if it's worth the effort... What if we change so that if the exception were going to return an empty string, it returns the class name instead?
msg405361 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-10-30 07:31
Then you would get

>>> raise asyncio.CancelledError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
asyncio.exceptions.CancelledError: CancelledError

instead of

>>> raise asyncio.CancelledError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
asyncio.exceptions.CancelledError

Many existing code include both of exception name and message in the report about exception. Repeating the exception name as exception message will cause confusing.

You should follow common practice and print not just an exception object, but its name or repr. It will help in other cases, when the exception has non-empty message, but you do not know its type.
msg405363 - (view) Author: lilydjwg (lilydjwg) * Date: 2021-10-30 08:38
OK, I see.
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89834
2021-10-30 08:38:22lilydjwgsetstatus: open -> closed
resolution: not a bug
messages: + msg405363

stage: resolved
2021-10-30 07:31:40serhiy.storchakasetmessages: + msg405361
2021-10-30 03:19:04lilydjwgsetmessages: + msg405355
2021-10-29 18:21:35serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg405326
2021-10-29 17:16:41asvetlovsetmessages: + msg405324
2021-10-29 16:48:16lilydjwgcreate