Fix bug with unset __name__ of one-line functions

The bug was a regression that I introduced in #1228.

I've created a new special form named `fn*` that works like the old `fn` (that is, it always creates a `FunctionDef`). Since this is intended only for internal use, like `with*`, I haven't documented it.
This commit is contained in:
Kodi Arfer 2017-03-25 18:07:45 -07:00 committed by Tuukka Turto
parent 491b474e7f
commit 7c203abe4d
3 changed files with 20 additions and 3 deletions

View File

@ -2297,9 +2297,13 @@ class HyASTCompiler(object):
return ret
@builds("fn")
@builds("fn*")
# The starred version is for internal use (particularly, in the
# definition of `defn`). It ensures that a FunctionDef is
# produced rather than a Lambda.
@checkargs(min=1)
def compile_function_def(self, expression):
expression.pop(0)
force_functiondef = expression.pop(0) == "fn*"
arglist = expression.pop(0)
if not isinstance(arglist, HyList):
@ -2376,7 +2380,7 @@ class HyASTCompiler(object):
defaults=defaults)
body = self._compile_branch(expression)
if not body.stmts:
if not force_functiondef and not body.stmts:
ret += ast.Lambda(
lineno=expression.start_line,
col_offset=expression.start_column,

View File

@ -46,7 +46,7 @@
(macro-error name "defn takes a name as first argument"))
(if (not (isinstance lambda-list hy.HyList))
(macro-error name "defn takes a parameter list as second argument"))
`(setv ~name (fn ~lambda-list ~@body)))
`(setv ~name (fn* ~lambda-list ~@body)))
(defmacro if-python2 [python2-form python3-form]
"If running on python2, execute python2-form, else, execute python3-form"

View File

@ -900,6 +900,19 @@
(assert (= 43 (my-fun 42))))
(defn test-defn-dunder-name []
"NATIVE: test that defn preserves __name__"
(defn phooey [x]
(+ x 1))
(assert (= phooey.__name__ "phooey"))
(defn mooey [x]
(+= x 1)
x)
(assert (= mooey.__name__ "mooey")))
(defn test-mangles []
"NATIVE: test mangles"
(assert (= 2 ((fn [] (+ 1 1))))))