From dc30584a8a42802dc5542743e7f95ef8c05d630c Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 13 Mar 2018 12:17:09 -0400 Subject: [PATCH] Handle function docstrings on Python 3.7 --- hy/compiler.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 0eb067e..ff38181 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -12,7 +12,7 @@ from hy.lex.parser import mangle import hy.macros from hy._compat import ( - str_type, string_types, bytes_type, long_type, PY3, PY35, + str_type, string_types, bytes_type, long_type, PY3, PY35, PY37, raise_empty) from hy.macros import require, macroexpand, tag_macroexpand import hy.importer @@ -1949,10 +1949,11 @@ class HyASTCompiler(object): ) + expression expression = expression.replace(arg[0]) - # Docstrings must come at the start, so ensure that happens even if we - # generate anonymous variables. - if docstring is not None: + # Before Python 3.7, docstrings must come at the start, so ensure that + # happens even if we generate anonymous variables. + if docstring is not None and not PY37: expression.insert(0, docstring) + docstring = None if PY3: # Python 3.4+ requires that args are an ast.arg object, rather @@ -1989,7 +1990,7 @@ class HyASTCompiler(object): defaults=defaults) body = self._compile_branch(expression) - if not force_functiondef and not body.stmts: + if not force_functiondef and not body.stmts and docstring is None: ret += asty.Lambda(expression, args=args, body=body.force_expr) return ret @@ -2012,7 +2013,9 @@ class HyASTCompiler(object): name=name, args=args, body=body.stmts, - decorator_list=[]) + decorator_list=[], + docstring=(None if docstring is None else + str_type(docstring))) ast_name = asty.Name(expression, id=name, ctx=ast.Load())