nac3core: support custom context managers #583

Merged
sb10q merged 1 commits from mwojcik/nac3:custom_context_managers into master 2025-02-19 22:25:49 +08:00
Member

closes #75

Untested yet on hardware (that's why WIP), but compiles the test/example, cargo fmt done

Also, should order of the exit calls be the same as enter (FIFO) or reversed (stack)?

closes #75 Untested yet on hardware (that's why WIP), but compiles the test/example, cargo fmt done Also, should order of the __exit__ calls be the same as __enter__ (FIFO) or reversed (stack)?
mwojcik added 1 commit 2025-02-10 16:58:22 +08:00
Owner

Obviously it's a stack - read the Python docs, understand what context managers are supposed to do, and try it in the REPL.

Obviously it's a stack - read the Python docs, understand what context managers are supposed to do, and try it in the REPL.
mwojcik force-pushed custom_context_managers from 0ffbbcef20 to 77a6baa84c 2025-02-10 17:13:27 +08:00 Compare
mwojcik force-pushed custom_context_managers from 77a6baa84c to 3a10aaed3e 2025-02-11 11:58:29 +08:00 Compare
sb10q reviewed 2025-02-11 20:20:35 +08:00
@ -0,0 +79,4 @@
if __name__ == "__main__":
CtxMgr().run()
Owner

I think the RTIO stuff can be removed and then this can become a nac3standalone demo? Using RTIO here doesn't add anything.

I think the RTIO stuff can be removed and then this can become a nac3standalone demo? Using RTIO here doesn't add anything.
mwojcik marked this conversation as resolved
Owner

Exceptions escaping the context manager are supposed to call __exit__ I believe.

Exceptions escaping the context manager are supposed to call ``__exit__`` I believe.
Owner

It's just like:

__enter__()
try:
  ...
finally:
  __exit__()

So you copy/call the implementation of try/finally.

It's just like: ``` __enter__() try: ... finally: __exit__() ``` So you copy/call the implementation of try/finally.
mwojcik force-pushed custom_context_managers from 3a10aaed3e to b083ec586c 2025-02-12 15:16:30 +08:00 Compare
mwojcik added 2 commits 2025-02-17 16:45:59 +08:00
Author
Member

moved the test back to nac3artiq as standalone does not support exceptions (more importantly __nac3_resume that's used is missing).

followed gen_try and slimmed it down to behave as except part didn't exist and finally was always there. Considered extracting the common code and introducing a new function to be used by both gen_try and gen_with but that I didn't find any other places in this module doing that; a little bit of redundancy for clarity won't hurt I think.

Also looked into rebuilding the body/handler/etc. and calling gen_try instead but trying to build all the Located Exprs was a bit too much.

moved the test back to nac3artiq as standalone does not support exceptions (more importantly ``__nac3_resume`` that's used is missing). followed ``gen_try`` and slimmed it down to behave as ``except`` part didn't exist and ``finally`` was always there. Considered extracting the common code and introducing a new function to be used by both ``gen_try`` and ``gen_with`` but that I didn't find any other places in this module doing that; a little bit of redundancy for clarity won't hurt I think. Also looked into rebuilding the body/handler/etc. and calling ``gen_try`` instead but trying to build all the ``Located`` ``Expr``s was a bit too much.
mwojcik added 1 commit 2025-02-18 14:05:31 +08:00
mwojcik force-pushed custom_context_managers from 3d87aea685 to 9094c965f1 2025-02-19 15:29:53 +08:00 Compare
mwojcik changed title from [WIP] nac3core: support custom context managers to nac3core: support custom context managers 2025-02-19 15:30:24 +08:00
mwojcik force-pushed custom_context_managers from 9094c965f1 to 2c9222445a 2025-02-19 16:00:13 +08:00 Compare
sb10q merged commit acb47154dc into master 2025-02-19 22:25:49 +08:00
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#583
No description provided.