hyrule icon indicating copy to clipboard operation
hyrule copied to clipboard

A macro to allow ignoring exceptions

Open James4Ever0 opened this issue 2 years ago • 9 comments

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.

James4Ever0 avatar Nov 14 '22 04:11 James4Ever0

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.

Kodiologist avatar Nov 14 '22 04:11 Kodiologist

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

James4Ever0 avatar Nov 14 '22 04:11 James4Ever0

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

James4Ever0 avatar Nov 14 '22 04:11 James4Ever0

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.

Kodiologist avatar Nov 14 '22 04:11 Kodiologist

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

James4Ever0 avatar Nov 14 '22 08:11 James4Ever0

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

James4Ever0 avatar Dec 05 '22 18:12 James4Ever0

@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.

Kodiologist avatar Dec 05 '22 18:12 Kodiologist

License issue fixed: hy_mod

James4Ever0 avatar Dec 05 '22 19:12 James4Ever0

Thanks.

Kodiologist avatar Dec 05 '22 19:12 Kodiologist