Skip to content

Commit bf6aa92

Browse files
author
Sunjay Cauligi
committed
Let doc find builtin macros
1 parent f4545f0 commit bf6aa92

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

hy/core/macros.hy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,13 @@
764764
Use ``require`` to make other macros available.
765765
766766
Use ``(help foo)`` instead for help with runtime objects."
767-
`(help (.get __macros__ (mangle '~symbol) None)))
767+
(setv symbol (str symbol))
768+
(setv mangled (mangle symbol))
769+
(setv builtins (gensym "builtins"))
770+
`(do (import [builtins :as ~builtins])
771+
(help (or (.get __macros__ ~mangled)
772+
(.get (. ~builtins __macros__) ~mangled)
773+
(raise (NameError f"macro {~symbol !r} is not defined"))))))
768774

769775

770776
(defmacro cfor [f #* generator]

tests/native_tests/core.hy

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
;;;; some simple helpers
66

7+
(import pytest)
8+
79
(defn assert-true [x]
810
(assert (= True x)))
911

@@ -699,6 +701,17 @@ result['y in globals'] = 'y' in globals()")
699701
<p> Move along. (Nothing to see here.)</p>)))
700702

701703
(defn test-doc [capsys]
704+
;; https://github.com/hylang/hy/issues/1970
705+
;; Let's first make sure we can doc the builtin macros
706+
;; before we create the user macros.
707+
(doc doc)
708+
(setv [out err] (.readouterr capsys))
709+
(assert (in "Gets help for a macro function" out))
710+
711+
(doc "#@")
712+
(setv [out err] (.readouterr capsys))
713+
(assert (in "with-decorator tag macro" out))
714+
702715
(defmacro <-mangle-> []
703716
"a fancy docstring"
704717
'(+ 2 2))
@@ -716,7 +729,12 @@ result['y in globals'] = 'y' in globals()")
716729
(doc "#pillgrums")
717730
(setv [out err] (.readouterr capsys))
718731
(assert (in "Look at the quality of that picture!" out))
719-
(assert (empty? err)))
732+
(assert (empty? err))
733+
734+
;; make sure doc raises an error instead of
735+
;; presenting a default value help screen
736+
(with [(pytest.raises NameError)]
737+
(doc does-not-exist)))
720738

721739

722740
(defn test-do-n []

0 commit comments

Comments
 (0)