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

A macro to allow ignoring exceptions #53

Open
James4Ever0 opened this issue Nov 14, 2022 · 9 comments
Open

A macro to allow ignoring exceptions #53

James4Ever0 opened this issue Nov 14, 2022 · 9 comments
Labels
feature New feature or request

Comments

@James4Ever0
Copy link

James4Ever0 commented Nov 14, 2022

For example, running the following code in hy:

(raise (Exception "some exception"))
(print "skip the above line")

Skipping the exception is not possible in hy, currently. However, in clisp, we can do this with commandline clisp -on-error debug test.lisp:

; inside test.lisp
(print "val")
(/ 1 0)
(format t "Hello, World!")

It offers option to restart on exactly the line of division by zero and does not re-execute the line (print "val"), without aborting the whole process.

clisp -on-error debug test.lisp

"val" 
*** - /: division by zero
The following restarts are available:
SKIP           :R1      skip (/ 1 0)
RETRY          :R2      retry (/ 1 0)
STOP           :R3      stop loading file test.lisp
Break 1 [3]>  ; type whatever you want here, execute new code, 
              ; reload file, define new function, macro, even changing 
              ; the behavior of the "/" operator, to fix the error and continue

This feature is provided as clisp's command line argument. I wonder if hy interpreter can do the same.

@Kodiologist
Copy link
Member

How would this be accomplished, given Python's exception model? Raising an exception causes an immediate and unconditional stack unwinding. You can arrest the process with try (or with), but you can't wind back the parts of the stack you've already unwound.

@James4Ever0
Copy link
Author

By internally wrapping every line of hy code inside try...except...?

@James4Ever0
Copy link
Author

Or somehow modify the base class Exception, changing its behavior when raising it?

@Kodiologist
Copy link
Member

Kodiologist commented Nov 14, 2022

Lines aren't really a unit of Hy syntax, and the mapping between Hy syntax and Python lines is arbitrary. You could try wrapping every form in a try. I think the performance penalty will be severe, though, and I wouldn't be surprised if there were subtle side-effects on semantics. Try implementing it with a macro if you're interested.

Exception is immutable in CPython. So is BaseException.

@James4Ever0
Copy link
Author

James4Ever0 commented Nov 14, 2022

Performance isn't an issue compared to debugging by restarting from scratch. Also it is possible to turn this behavior off when deployed.

@Kodiologist Kodiologist transferred this issue from hylang/hy Nov 14, 2022
@Kodiologist Kodiologist changed the title Make hot-patching runtime/uncaught error possible for hy A macro to allow ignoring exceptions Nov 14, 2022
@Kodiologist Kodiologist added the feature New feature or request label Nov 14, 2022
@James4Ever0
Copy link
Author

Gloriously propose my mod on hy interpreter, though not using any 'macro' based methods, also leaves lots of things to implement in README.

@Kodiologist
Copy link
Member

@James4Ever0 Your repository contains code from Hy, so you need to abide by the terms of Hy's LICENSE and track copyrights to not infringe on the Hy authors' copyrights. This kind of thing is easier to get right if you use GitHub's fork button instead of creating a new repository from scratch.

@James4Ever0
Copy link
Author

License issue fixed: hy_mod

@Kodiologist
Copy link
Member

Thanks.

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

No branches or pull requests

2 participants