Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support task methods for exception and result #54

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

imnotjames
Copy link
Contributor

@imnotjames imnotjames commented Nov 8, 2023

Adds the following methods to the Task class to bring it closer in line with CPython

  • get_coro - a method that returns the micropython specific Task.coro property
  • result - returns the result if no exception was raised or raised the exception that this task had
  • exception - returns the exception if an exception was raised
  • cancelled - returns true if this task had been cancelled
  • set_exception - exists on the class but always raises an exception per CPython's implementation
  • set_result - exists on the class but always raises an exception per CPython implementation

Task.cancel was updated to support a msg parameter which is passed to the CancelledError

In progress is add_done_callback and remove_done_callback - these currently raise a NotImplementedError but are how CPython manages the done callbacks (unlike CircuitPython supporting via modifying Task.state)

This requires changes to circuitpython's _asyncio to properly work.

fixes #53

@imnotjames imnotjames force-pushed the feat/53/extra-task-methods branch from d5f2123 to 9a63a27 Compare November 9, 2023 17:46
@dhalbert
Copy link
Contributor

dhalbert commented Nov 9, 2023

@imnotjames I thought I saw some questions from you about where to add exceptions, how close we should adhere to CPython, etc., but I cannot find them. Did you ask them here and then delete them? I cannot find them in discord either.

_asyncio is supposed to be optional, so this library should be able to work without it. Hence the guarded imports for _asyncio.

@imnotjames
Copy link
Contributor Author

imnotjames commented Nov 9, 2023

@dhalbert No worries! It was in discord, there was a bit of a wall of text and then a question, so totally understandable to miss it.

But onto a bit of a question -- if I'm implementing some python methods in C and need it to throw an error - in this case CancelledError - do I need that error available at runtime or compile time? In other words, do I need to pull that out of the asyncio external module and implement an error in _asyncio?

I think during compile time so I'd have to define it as part of the modasyncio.c but I'm not the most familiar with circuitpython internals.

If they are needed during compile time then it means I'd move CancelledError & InvalidStateError into task.py + implement them as well in the optional _asyncio.

I might have answered my own question via some attempts to make it work any other way, though -- I can't find a way to reference the CancelledError from C without having it be there during compile time. If there's some way I'm missing, though, definitely interested to learn more.

@dhalbert
Copy link
Contributor

dhalbert commented Nov 9, 2023

Got it. Any exception referenced by C has to be defined in C (or else defined at runtime in some tortuous way, which we wouldn't normally do). So definitely just define it like the other _asyncio C exceptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Task methods result(), exception(), and cancelled()
2 participants