From 18acfe6495e83dccc9c0f3ddf105dc7ccef5a86a Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 25 Mar 2017 17:15:10 -0700 Subject: [PATCH] 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. --- NEWS | 1 - docs/language/api.rst | 4 ---- hy/compiler.py | 26 +++++--------------------- hy/contrib/loop.hy | 2 +- hy/contrib/multi.hy | 2 +- tests/importer/test_importer.py | 3 +-- tests/native_tests/with_decorator.hy | 8 -------- 7 files changed, 8 insertions(+), 38 deletions(-) diff --git a/NEWS b/NEWS index fd3a1e1..01d139b 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/docs/language/api.rst b/docs/language/api.rst index 9c55e70..ea0fb30 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -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. - #@ ~~ diff --git a/hy/compiler.py b/hy/compiler.py index 88c6ec7..1bf3bdf 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -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, - 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: + if not fn.stmts or not isinstance(fn.stmts[-1], (ast.FunctionDef, + ast.ClassDef)): 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) diff --git a/hy/contrib/loop.hy b/hy/contrib/loop.hy index 5d9c9dc..831bbb3 100644 --- a/hy/contrib/loop.hy +++ b/hy/contrib/loop.hy @@ -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)) diff --git a/hy/contrib/multi.hy b/hy/contrib/multi.hy index e97617f..36beccc 100644 --- a/hy/contrib/multi.hy +++ b/hy/contrib/multi.hy @@ -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))))) diff --git a/tests/importer/test_importer.py b/tests/importer/test_importer.py index 0106f19..edfbb5a 100644 --- a/tests/importer/test_importer.py +++ b/tests/importer/test_importer.py @@ -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 diff --git a/tests/native_tests/with_decorator.hy b/tests/native_tests/with_decorator.hy index 1fe2da6..383e866 100644 --- a/tests/native_tests/with_decorator.hy +++ b/tests/native_tests/with_decorator.hy @@ -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