Skip to content

About checking for CancelledError and its subclasses #113848

Closed

Description

@serhiy-storchaka

For long time I wanted to replace the use of PyObject_IsInstance() with CancelledError in _asynciomodule.c. Even if the C code is correct and is closest to the corresponding Python code, it looked unnecessary complicated and bugprone. Also, PyErr_GivenExceptionMatches() is used in except implementation, so it may be more correct than an isinstance check. But I did not have tests for CancelledError subclasses which would show the difference.

Other issue. @gvanrossum noticed that asyncio.timeout() only checks for exact CancelledError, and not its subclasses. asyncio.TaskGroup() also only checks for exact CancelledError. It is suspicious, because all other code (except _convert_future_exc() in futures.py) treats CancelledError subclasses in the same way as CancelledError. asyncio.timeout() and asyncio.TaskGroup() were added recently, so perhaps it is error in their implementation. On other hand, I do not know any use case for CancelledError subclasses.

Linked PRs

Activity

  1. added 2 commits that reference this issue on Jan 9, 2024
  2. moved this from Todo to In Progress in asyncioon Jan 9, 2024
  3. added 2 commits that reference this issue on Jan 9, 2024
  4. moved this from In Progress to Done in asyncioon Jan 9, 2024
  5. added 2 commits that reference this issue on Jan 22, 2024
  6. added 2 commits that reference this issue on Feb 11, 2024
  7. added 2 commits that reference this issue on Sep 2, 2024
  8. btakita commented on May 17, 2025

    @btakita

    Related discussion on Subclassing CancelledError...having real-world implications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions