Revert the extension of with-decorator to setv

This is no longer necessary now that `defn` always produces a `FunctionDef`.

To compensate, I've made small edits to two contrib modules and reverted a small test change.
This commit is contained in:
Kodi Arfer 2017-03-25 17:15:10 -07:00 committed by Tuukka Turto
parent 7c203abe4d
commit 18acfe6495
7 changed files with 8 additions and 38 deletions

1
NEWS
View File

@ -8,7 +8,6 @@ Changes from 0.12.1
and `str` objects under Python 2
* Commas and underscores are allowed in numeric literals
* `setv` always returns None
* with-decorator: Allow a `setv` form as the form to be decorated
* xor: If exactly one argument is true, return it
* hy.core.reserved is now hy.extra.reserved

View File

@ -1686,10 +1686,6 @@ will be 4 (``1+1 + 1+1``).
=> (addition 1 1)
8
In addition to ``defn`` forms, ``with-decorator`` can be used with ``defclass``
and ``setv`` forms. In the latter case, the generated Python code uses an
ordinary function call rather than decorator syntax.
#@
~~

View File

@ -1417,28 +1417,12 @@ class HyASTCompiler(object):
def compile_decorate_expression(self, expr):
expr.pop(0) # with-decorator
fn = self.compile(expr.pop(-1))
if fn.stmts and isinstance(fn.stmts[-1], (ast.FunctionDef,
if not fn.stmts or not isinstance(fn.stmts[-1], (ast.FunctionDef,
ast.ClassDef)):
decorators, ret, _ = self._compile_collect(expr)
fn.stmts[-1].decorator_list = (decorators +
fn.stmts[-1].decorator_list)
return ret + fn
elif fn.stmts and isinstance(fn.stmts[-1], ast.Assign):
# E.g., (with-decorator foo (setv f (fn [] 5)))
# We can't use Python's decorator syntax, but we can get the
# same effect.
decorators, ret, _ = self._compile_collect(expr)
for d in decorators:
fn.stmts[-1].value = ast.Call(func=d,
args=[fn.stmts[-1].value],
keywords=[],
starargs=None,
kwargs=None,
lineno=expr.start_line,
col_offset=expr.start_column)
return fn
else:
raise HyTypeError(expr, "Decorated a non-function")
decorators, ret, _ = self._compile_collect(expr)
fn.stmts[-1].decorator_list = decorators + fn.stmts[-1].decorator_list
return ret + fn
@builds("with*")
@checkargs(min=2)

View File

@ -58,7 +58,7 @@
(import [hy.contrib.loop [--trampoline--]])
(with-decorator
--trampoline--
(def ~g!recur-fn (fn [~@signature] ~@new-body)))
(defn ~g!recur-fn [~@signature] ~@new-body))
~g!recur-fn))

View File

@ -110,4 +110,4 @@
ret)
(do
(setv [lambda-list body] (head-tail bodies))
`(setv ~name (fn ~lambda-list ~@body)))))
`(setv ~name (fn* ~lambda-list ~@body)))))

View File

@ -12,8 +12,7 @@ def test_basics():
def test_stringer():
"Make sure the basics of the importer work"
_ast = import_buffer_to_ast(
"(defn square [x] (print \"hello\") (* x x))", '')
_ast = import_buffer_to_ast("(defn square [x] (* x x))", '')
assert type(_ast.body[0]) == ast.FunctionDef

View File

@ -28,14 +28,6 @@
(assert (= cls.attr2 456)))
(defn test-decorated-setv []
(defn d [func]
(fn [] (+ (func) "z")))
(with-decorator d
(setv f (fn [] "hello")))
(assert (= (f) "helloz")))
(defn test-decorator-clobbing []
"NATIVE: Tests whether nested decorators work"
(do