Allow coroutines to be decorated

This commit is contained in:
Simon Gomizelj 2017-12-31 00:04:15 -05:00 committed by Kodi Arfer
parent 300cb2856d
commit f69ccd2421
2 changed files with 14 additions and 2 deletions

View File

@ -76,6 +76,9 @@ def _is_hy_builtin(name, module_name):
_compile_table = {} _compile_table = {}
_decoratables = (ast.FunctionDef, ast.ClassDef)
if PY35:
_decoratables += (ast.AsyncFunctionDef,)
def ast_str(foobar): def ast_str(foobar):
@ -1301,8 +1304,7 @@ class HyASTCompiler(object):
def compile_decorate_expression(self, expr): def compile_decorate_expression(self, expr):
expr.pop(0) # with-decorator expr.pop(0) # with-decorator
fn = self.compile(expr.pop()) fn = self.compile(expr.pop())
if not fn.stmts or not isinstance(fn.stmts[-1], (ast.FunctionDef, if not fn.stmts or not isinstance(fn.stmts[-1], _decoratables):
ast.ClassDef)):
raise HyTypeError(expr, "Decorated a non-function") raise HyTypeError(expr, "Decorated a non-function")
decorators, ret, _ = self._compile_collect(expr) decorators, ret, _ = self._compile_collect(expr)
fn.stmts[-1].decorator_list = decorators + fn.stmts[-1].decorator_list fn.stmts[-1].decorator_list = decorators + fn.stmts[-1].decorator_list

View File

@ -45,6 +45,16 @@
(assert (= (run-coroutine coro-test) [1 2 3]))) (assert (= (run-coroutine coro-test) [1 2 3])))
(defn test-decorated-defn/a []
(defn decorator [func] (fn/a [] (/ (await (func)) 2)))
#@(decorator
(defn/a coro-test []
(await (sleep 0))
42))
(assert (= (run-coroutine coro-test) 21)))
(defclass AsyncWithTest [] (defclass AsyncWithTest []
(defn --init-- [self val] (defn --init-- [self val]
(setv self.val val) (setv self.val val)