Python 3.9 asyncio: Task cancel() throws asyncio.exceptions.CancelledError instead of asyncio.CancelledError

Chris Angelico rosuav at gmail.com
Fri Mar 31 19:14:18 EDT 2023


On Sat, 1 Apr 2023 at 10:05, Clint Olsen <clint.olsen at gmail.com> wrote:
>
> On Friday, March 31, 2023 at 3:23:24 PM UTC-7, Chris Angelico wrote:
> > On Sat, 1 Apr 2023 at 09:19, Clint Olsen <xxx... at gmail.com> wrote:
> > > Attempting to catch asyncio.CancelledError or asyncio.CancelledError does not work. The function in question looks like:
> > >>> asyncio.exceptions.CancelledError is asyncio.CancelledError
> > True
> >
> > Does that answer the question?
>
> No, I couldn't catch either exception even though they are the same.
>

Okay, so that deals with the part from the subject line, leaving a
slightly different problem: The caught exception is not of the same
type as you were expecting. First question: Can you reproduce the
issue on command? If so, I would recommend trying this:

except BaseException as e:
    print("Got an exception!", type(e))
    print(id(type(e)))
    print(id(asyncio.CancelledError)
except:
    print("Weird things are happening")
    import sys
    print(sys.exc_info())
    print(id(sys.exc_info()[0]))
    print(id(asyncio.CancelledError))

Basically, I want to know whether (a) BaseException has changed, which
would be a nightmare; and (b) whether asyncio.CancelledError has
changed.

This is the kind of bizarre behaviour that can happen if a module is
reloaded, or if there are multiple versions loaded for some reason.
But if that ISN'T what's happening, there'll have to be some other
explanation.

ChrisA


More information about the Python-list mailing list