Handle function docstrings on Python 3.7

This commit is contained in:
Kodi Arfer 2018-03-13 12:17:09 -04:00
parent f57463c0f8
commit dc30584a8a

View File

@ -12,7 +12,7 @@ from hy.lex.parser import mangle
import hy.macros import hy.macros
from hy._compat import ( 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) raise_empty)
from hy.macros import require, macroexpand, tag_macroexpand from hy.macros import require, macroexpand, tag_macroexpand
import hy.importer import hy.importer
@ -1949,10 +1949,11 @@ class HyASTCompiler(object):
) + expression ) + expression
expression = expression.replace(arg[0]) expression = expression.replace(arg[0])
# Docstrings must come at the start, so ensure that happens even if we # Before Python 3.7, docstrings must come at the start, so ensure that
# generate anonymous variables. # happens even if we generate anonymous variables.
if docstring is not None: if docstring is not None and not PY37:
expression.insert(0, docstring) expression.insert(0, docstring)
docstring = None
if PY3: if PY3:
# Python 3.4+ requires that args are an ast.arg object, rather # Python 3.4+ requires that args are an ast.arg object, rather
@ -1989,7 +1990,7 @@ class HyASTCompiler(object):
defaults=defaults) defaults=defaults)
body = self._compile_branch(expression) 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) ret += asty.Lambda(expression, args=args, body=body.force_expr)
return ret return ret
@ -2012,7 +2013,9 @@ class HyASTCompiler(object):
name=name, name=name,
args=args, args=args,
body=body.stmts, 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()) ast_name = asty.Name(expression, id=name, ctx=ast.Load())