0

When trying to require the following macro in another file, the raise seems to be triggering immediately:

(defmacro assert [a [b True] [message None]]
    `(unless (= ~a ~b) (raise (AssertionError (or ~message ~a)))))

This results in an AssertionError: True.

Steps to reproduce (sometimes works, and sometimes doesn't):

echo '(defmacro assert [a [b True] [message None]] `(unless (= ~a ~b) (raise (AssertionError (or ~message ~a)))))' > a.hy
echo '(require a [assert])' > b.hy
echo '(assert True)' >> b.hy
hy b.hy
ShadowRylander
  • 361
  • 2
  • 8
  • 1
    (1) Giving a macro the same name as a core macro is a bad idea unless you know what you're doing. (2) Like I said in my comment on your previous question, when you want to talk about a problem that involves multiple files, provide complete and exact reproduction steps as a sequence of shell commands (use lines like `echo '(my-code)' > file.hy` to set the contents of each file). (3) Simplify! I bet you can figure this one out yourself if you simplify it one step at a time. – Kodiologist Jul 29 '22 at 12:30
  • I can't seem to isolate what the problem is; sometimes it works, and sometimes it doesn't. I've updated my question, but I don't how else to simplify the code. – ShadowRylander Jul 29 '22 at 20:23
  • If the problem occurs inconsistently, it's probably related to bytecode caching, as in your previous question. Investigate by setting `PYTHONDONTWRITEBYTECODE` or deleting `__pycache__` manually. – Kodiologist Jul 29 '22 at 20:37
  • For some reason, it seems to work if I replace `unless (= ~a ~b)` with `if (not (= ~a ~b))`; there's not supposed to be a difference there, right? And on a side note, the `pytest` doesn't seem to work either, though individual tests succeed when running them manually with `hy`. – ShadowRylander Jul 30 '22 at 00:57
  • 1
    Good work, you're 90% of the way there. Since you now understand that `unless` is the culprit, try looking up its documentation. Or, simplify some more. Does `unless` work like you expect in a one-line example program? – Kodiologist Jul 30 '22 at 11:30
  • ... Is this an issue where the `NameErrors` for macros are superseded by other errors again? This is becoming a little confusing... I'd thought `unless` had been integrated into the `hy` core. Ah, well. The pytest is still an issue, but I feel that's a problem with my plugin; I'm always getting an `INTERNALERROR`. Either way, thanks for all the help! I'll answer the question, then? – ShadowRylander Jul 30 '22 at 18:09
  • 1
    "Is this an issue where the `NameErrors` for macros are superseded by other errors again?" — Right. Python evaluates the arguments before the function name. "I'd thought `unless` had been integrated into the hy core." — No, it started out there, but I moved it to Hyrule a little over a year ago. – Kodiologist Jul 30 '22 at 18:19
  • Right; makes sense. I'll just add an answer with the context then! – ShadowRylander Jul 30 '22 at 18:40

1 Answers1

1

In another instance of Python evaluating function arguments before names, as stated by Kodiologist in their comment here and their answer on one of my previous questions "Non-existent hy macros runs assertions, but fails appropriately with everything else", the issue was that unless didn't trigger a NameError when it didn't exist, since it was moved from the hy core to hyrule a little over a year ago.

ShadowRylander
  • 361
  • 2
  • 8