Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AST handling of docstrings and __future__ ordering #1673

Merged
merged 2 commits into from Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.rst
Expand Up @@ -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
==============================
Expand Down
14 changes: 13 additions & 1 deletion hy/compiler.py
Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions tests/compilers/test_ast.py
Expand Up @@ -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'
1 change: 1 addition & 0 deletions tests/resources/pydemo.hy
Expand Up @@ -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))

Expand Down
3 changes: 3 additions & 0 deletions tests/test_hy2py.py
Expand Up @@ -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."
Expand Down