Marco Pagliaricci <pagliaricc...@gmail.com> added the comment:

Chris,
I'm attaching to this e-mail the code I'm referring to.
As you can see, in line 10, I re-raise the asyncio.CancelledError exception
with a message "TEST".
That message is lost, due to the reasons we've talked about.

My point is that, if we substitute that line 10, with the commented line
11, and we comment the line 10, so we raise a ValueError("TEST") exception,
as you can see, the message "TEST" is NOT LOST.
I just find this counter-intuitive, and error-prone.

AT LEAST should be very well specified in the docs.

Regards,
M.

On Sat, Oct 9, 2021 at 2:51 PM Chris Jerdonek <rep...@bugs.python.org>
wrote:

>
> Chris Jerdonek <chris.jerdo...@gmail.com> added the comment:
>
> > 2) Now: if I re-raise the asyncio.CancelledError as-is, I lose the
> message,
> if I call the `asyncio.Task.exception()` function.
>
> Re-raise asyncio.CancelledError where? (And what do you mean by
> "re-raise"?) Call asyncio.Task.exception() where? This isn't part of your
> example, so it's not clear what you mean exactly.
>
> ----------
>
> _______________________________________
> Python tracker <rep...@bugs.python.org>
> <https://bugs.python.org/issue45390>
> _______________________________________
>

----------
Added file: https://bugs.python.org/file50333/task_bug.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45390>
_______________________________________
import asyncio


async def job():
        print("job(): START...")
        try:
                await asyncio.sleep(5.0)
        except asyncio.CancelledError as e:
                print("job(): CANCELLED!", e)
                #raise asyncio.CancelledError("TEST")
                raise ValueError("TEST")
        print("job(): DONE.")


async def cancel_task_after(task, time):
        try:
                await asyncio.sleep(time)
        except asyncio.CancelledError:
                print("cancel_task_after(): CANCELLED!")
        except Exception as e:
                print("cancel_task_after(): Exception!", e.__class__.__name__, 
e)
        task.cancel("Hello!")


async def main():
        task = asyncio.create_task(job())
        # RUN/CANCEL task.
        try:
                await asyncio.gather(task, cancel_task_after(task, 1.0))
        except asyncio.CancelledError as e:
                print("In running task, we encountered a cancellation! 
Excpetion message is: ", e)
        except Exception as e:
                print("In running task, we got a generic Exception:", 
e.__class__.__name__, e)
        # GET result.
        try:
                result = task.result()
        except asyncio.CancelledError as e:
                print("Task has been cancelled, exception message is: ", e)
        except Exception as e:
                print("Task raised generic exception", e.__class__.__name__, e)
        else:
                print("Task result is: ", result)


if __name__=="__main__":
        asyncio.run(main())

_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to