Handle module docstrings on Python 3.7

This commit is contained in:
Kodi Arfer 2018-03-13 16:29:41 -04:00
parent cfb042304c
commit e3058b5cf5
2 changed files with 13 additions and 7 deletions

View File

@ -2228,9 +2228,16 @@ def hy_compile(tree, module_name, root=ast.Module, get_expr=False):
if not get_expr: if not get_expr:
result += result.expr_as_stmt() result += result.expr_as_stmt()
module_docstring = None
if (PY37 and result.stmts and
isinstance(result.stmts[0], ast.Expr) and
isinstance(result.stmts[0].value, ast.Str)):
module_docstring = result.stmts.pop(0).value.s
body = compiler.imports_as_stmts(tree) + result.stmts body = compiler.imports_as_stmts(tree) + result.stmts
ret = root(body=body) ret = root(body=body, docstring=(
None if module_docstring is None else module_docstring))
if get_expr: if get_expr:
expr = ast.Expression(body=expr) expr = ast.Expression(body=expr)

View File

@ -53,7 +53,7 @@ def cant_compile(expr):
def s(x): def s(x):
return can_compile(x).body[0].value.s return can_compile('"module docstring" ' + x).body[-1].value.s
def test_ast_bad_type(): def test_ast_bad_type():
@ -476,13 +476,12 @@ def test_ast_unicode_strings():
def _compile_string(s): def _compile_string(s):
hy_s = HyString(s) hy_s = HyString(s)
hy_s.start_line = hy_s.end_line = 0
hy_s.start_column = hy_s.end_column = 0
code = hy_compile(hy_s, "__main__") code = hy_compile([hy_s], "__main__")
# We put hy_s in a list so it isn't interpreted as a docstring.
# code == ast.Module(body=[ast.Expr(value=ast.Str(s=xxx))]) # code == ast.Module(body=[ast.Expr(value=ast.List(elts=[ast.Str(s=xxx)]))])
return code.body[0].value.s return code.body[0].value.elts[0].s
assert _compile_string("test") == "test" assert _compile_string("test") == "test"
assert _compile_string("\u03b1\u03b2") == "\u03b1\u03b2" assert _compile_string("\u03b1\u03b2") == "\u03b1\u03b2"