From a9fca8001e3eeb2a53eb52cfa63c1116ee6b4a1b Mon Sep 17 00:00:00 2001 From: "Brandon T. Willard" Date: Mon, 27 Aug 2018 18:30:40 -0500 Subject: [PATCH 1/2] Fix AST handling of docstrings and __future__ ordering This closes hylang/hy#1367 and closes hylang/hy#1540 --- hy/compiler.py | 14 +++++++++++++- tests/compilers/test_ast.py | 22 ++++++++++++++++++++++ tests/resources/pydemo.hy | 1 + tests/test_hy2py.py | 3 +++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/hy/compiler.py b/hy/compiler.py index 575c5a9..bd43cf7 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1719,7 +1719,19 @@ def hy_compile(tree, module_name, root=ast.Module, get_expr=False): if not get_expr: result += result.expr_as_stmt() - body = compiler.imports_as_stmts(tree) + result.stmts + body = [] + + # Pull out a single docstring and prepend to the resulting body. + if (len(result.stmts) > 0 and + issubclass(root, ast.Module) and + isinstance(result.stmts[0], ast.Expr) and + isinstance(result.stmts[0].value, ast.Str)): + + body += [result.stmts.pop(0)] + + body += sorted(compiler.imports_as_stmts(tree) + result.stmts, + key=lambda a: not (isinstance(a, ast.ImportFrom) and + a.module == '__future__')) ret = root(body=body) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 3a1e52a..cb3ab7b 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -664,3 +664,25 @@ def test_ast_bad_yield_from(): def test_eval_generator_with_return(): """Ensure generators with a return statement works.""" can_eval("(fn [] (yield 1) (yield 2) (return))") + + +def test_futures_imports(): + """Make sure __future__ imports go first, especially when builtins are + automatically added (e.g. via use of a builtin name like `name`).""" + hy_ast = can_compile(( + '(import [__future__ [print_function]])\n' + '(import sys)\n' + '(setv name [1 2])' + '(print (first name))')) + + assert hy_ast.body[0].module == '__future__' + assert hy_ast.body[1].module == 'hy.core.language' + + hy_ast = can_compile(( + '(import sys)\n' + '(import [__future__ [print_function]])\n' + '(setv name [1 2])' + '(print (first name))')) + + assert hy_ast.body[0].module == '__future__' + assert hy_ast.body[1].module == 'hy.core.language' diff --git a/tests/resources/pydemo.hy b/tests/resources/pydemo.hy index b5e9cd7..d8a2447 100644 --- a/tests/resources/pydemo.hy +++ b/tests/resources/pydemo.hy @@ -4,6 +4,7 @@ ;; This Hy module is intended to concisely demonstrate all of ;; Python's major syntactic features for the purpose of testing hy2py. +"This is a module docstring." (setv mystring (* "foo" 3)) diff --git a/tests/test_hy2py.py b/tests/test_hy2py.py index a0f1188..6bb0786 100644 --- a/tests/test_hy2py.py +++ b/tests/test_hy2py.py @@ -24,6 +24,9 @@ def test_hy2py_import(tmpdir): def assert_stuff(m): + # This makes sure that automatically imported builtins go after docstrings. + assert m.__doc__ == u'This is a module docstring.' + assert m.mystring == "foofoofoo" assert m.long_string == u"This is a very long string literal, which would surely exceed any limitations on how long a line or a string literal can be. The string literal alone exceeds 256 characters. It also has a character outside the Basic Multilingual Plane: 😂. Here's a double quote: \". Here are some escaped newlines:\n\n\nHere is a literal newline:\nCall me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me." From f4e5f02b3e7b297d6e4ffa2567fd025a021d3127 Mon Sep 17 00:00:00 2001 From: "Brandon T. Willard" Date: Tue, 28 Aug 2018 16:58:59 -0500 Subject: [PATCH 2/2] Updated NEWS --- NEWS.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.rst b/NEWS.rst index 1fe1e41..5a67e80 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -22,6 +22,8 @@ Bug Fixes attribute access. * Fixed crashes on Windows when calling `hy-repr` on date and time objects. +* Fixed errors from `from __future__ import ...` statements and missing Hy + module docstrings caused by automatic importing of Hy builtins. 0.15.0 ==============================