Merge pull request #1447 from gilch/astor

update astor to 0.6
This commit is contained in:
Tuukka Turto 2017-11-01 07:02:37 +02:00 committed by GitHub
commit 687be1e78c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 21 deletions

View File

@ -11,7 +11,7 @@ import sys
import os import os
import importlib import importlib
import astor.codegen import astor.code_gen
import hy import hy
@ -413,17 +413,17 @@ def hy2py_main():
else pretty_error(import_buffer_to_ast, stdin_text, module_name)) else pretty_error(import_buffer_to_ast, stdin_text, module_name))
if options.with_ast: if options.with_ast:
if PY3 and platform.system() == "Windows": if PY3 and platform.system() == "Windows":
_print_for_windows(astor.dump(_ast)) _print_for_windows(astor.dump_tree(_ast))
else: else:
print(astor.dump(_ast)) print(astor.dump_tree(_ast))
print() print()
print() print()
if not options.without_python: if not options.without_python:
if PY3 and platform.system() == "Windows": if PY3 and platform.system() == "Windows":
_print_for_windows(astor.codegen.to_source(_ast)) _print_for_windows(astor.code_gen.to_source(_ast))
else: else:
print(astor.codegen.to_source(_ast)) print(astor.code_gen.to_source(_ast))
parser.exit(0) parser.exit(0)

View File

@ -27,6 +27,7 @@ import copy
import inspect import inspect
from collections import defaultdict from collections import defaultdict
from cmath import isnan
if PY3: if PY3:
import builtins import builtins
@ -2155,11 +2156,27 @@ class HyASTCompiler(object):
raise HyTypeError(cons, "Can't compile a top-level cons cell") raise HyTypeError(cons, "Can't compile a top-level cons cell")
@builds(HyInteger, HyFloat, HyComplex) @builds(HyInteger, HyFloat, HyComplex)
def compile_numeric_literal(self, number, building): def compile_numeric_literal(self, x, building):
f = {HyInteger: long_type, f = {HyInteger: long_type,
HyFloat: float, HyFloat: float,
HyComplex: complex}[building] HyComplex: complex}[building]
return asty.Num(number, n=f(number)) # Work around https://github.com/berkerpeksag/astor/issues/85 :
# astor can't generate Num nodes with NaN, so we have
# to build an expression that evaluates to NaN.
def nn(number):
return asty.Num(x, n=number)
if isnan(x):
def nan(): return asty.BinOp(
x, left=nn(1e900), op=ast.Sub(), right=nn(1e900))
if f is complex:
return asty.Call(
x,
func=asty.Name(x, id="complex", ctx=ast.Load()),
keywords=[],
args=[nan() if isnan(x.real) else nn(x.real),
nan() if isnan(x.imag) else nn(x.imag)])
return nan()
return nn(f(x))
@builds(HySymbol) @builds(HySymbol)
def compile_symbol(self, symbol): def compile_symbol(self, symbol):

View File

@ -79,8 +79,8 @@ If the second argument `codegen` is true, generate python code instead."
(spoof-positions tree) (spoof-positions tree)
(setv compiled (hy.compiler.hy-compile tree (calling-module-name))) (setv compiled (hy.compiler.hy-compile tree (calling-module-name)))
((if codegen ((if codegen
astor.codegen.to_source astor.code-gen.to-source
astor.dump) astor.dump-tree)
compiled)) compiled))
(defn distinct [coll] (defn distinct [coll]

View File

@ -30,7 +30,7 @@ class Install(install):
"." + filename[:-len(".hy")]) "." + filename[:-len(".hy")])
install.run(self) install.run(self)
install_requires = ['rply>=0.7.5', 'astor>=0.5', 'clint>=0.4'] install_requires = ['rply>=0.7.5', 'astor>=0.6', 'clint>=0.4']
if os.name == 'nt': if os.name == 'nt':
install_requires.append('pyreadline>=2.1') install_requires.append('pyreadline>=2.1')

View File

@ -1550,13 +1550,23 @@
"NATIVE: Test the disassemble function" "NATIVE: Test the disassemble function"
(if PY35 (if PY35
(assert (= (disassemble '(do (leaky) (leaky) (macros))) (assert (= (disassemble '(do (leaky) (leaky) (macros)))
"Module(\n body=[Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[])),\n Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[])),\n Expr(value=Call(func=Name(id='macros'), args=[], keywords=[]))])")) "Module(
body=[Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[])),
Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[])),
Expr(value=Call(func=Name(id='macros'), args=[], keywords=[]))])"))
(assert (= (disassemble '(do (leaky) (leaky) (macros))) (assert (= (disassemble '(do (leaky) (leaky) (macros)))
"Module(\n body=[\n Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[], starargs=None, kwargs=None)),\n Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[], starargs=None, kwargs=None)),\n Expr(value=Call(func=Name(id='macros'), args=[], keywords=[], starargs=None, kwargs=None))])"))) "Module(
body=[
Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[], starargs=None, kwargs=None)),
Expr(value=Call(func=Name(id='leaky'), args=[], keywords=[], starargs=None, kwargs=None)),
Expr(value=Call(func=Name(id='macros'), args=[], keywords=[], starargs=None, kwargs=None))])")))
(assert (= (disassemble '(do (leaky) (leaky) (macros)) True) (assert (= (disassemble '(do (leaky) (leaky) (macros)) True)
"leaky()\nleaky()\nmacros()")) "leaky()
leaky()
macros()
"))
(assert (= (re.sub r"[L() ]" "" (disassemble `(+ ~(+ 1 1) 40) True)) (assert (= (re.sub r"[L() ]" "" (disassemble `(+ ~(+ 1 1) 40) True))
"2+40"))) "2+40\n")))
(defn test-attribute-access [] (defn test-attribute-access []

View File

@ -144,7 +144,7 @@
(defn test-gensym-in-macros [] (defn test-gensym-in-macros []
(import ast) (import ast)
(import [astor.codegen [to_source]]) (import [astor.code-gen [to-source]])
(import [hy.importer [import_buffer_to_ast]]) (import [hy.importer [import_buffer_to_ast]])
(setv macro1 "(defmacro nif [expr pos zero neg] (setv macro1 "(defmacro nif [expr pos zero neg]
(setv g (gensym)) (setv g (gensym))
@ -170,7 +170,7 @@
(defn test-with-gensym [] (defn test-with-gensym []
(import ast) (import ast)
(import [astor.codegen [to_source]]) (import [astor.code-gen [to-source]])
(import [hy.importer [import_buffer_to_ast]]) (import [hy.importer [import_buffer_to_ast]])
(setv macro1 "(defmacro nif [expr pos zero neg] (setv macro1 "(defmacro nif [expr pos zero neg]
(with-gensyms [a] (with-gensyms [a]
@ -194,7 +194,7 @@
(defn test-defmacro-g! [] (defn test-defmacro-g! []
(import ast) (import ast)
(import [astor.codegen [to_source]]) (import [astor.code-gen [to-source]])
(import [hy.importer [import_buffer_to_ast]]) (import [hy.importer [import_buffer_to_ast]])
(setv macro1 "(defmacro/g! nif [expr pos zero neg] (setv macro1 "(defmacro/g! nif [expr pos zero neg]
`(do `(do
@ -223,7 +223,7 @@
(defn test-defmacro! [] (defn test-defmacro! []
;; defmacro! must do everything defmacro/g! can ;; defmacro! must do everything defmacro/g! can
(import ast) (import ast)
(import [astor.codegen [to_source]]) (import [astor.code-gen [to-source]])
(import [hy.importer [import_buffer_to_ast]]) (import [hy.importer [import_buffer_to_ast]])
(setv macro1 "(defmacro! nif [expr pos zero neg] (setv macro1 "(defmacro! nif [expr pos zero neg]
`(do `(do

View File

@ -72,7 +72,7 @@ def test_bin_hy_stdin():
output, _ = run_cmd("hy --spy", '(koan)') output, _ = run_cmd("hy --spy", '(koan)')
assert "monk" in output assert "monk" in output
assert "\\n Ummon" in output assert "\n Ummon" in output
# --spy should work even when an exception is thrown # --spy should work even when an exception is thrown
output, _ = run_cmd("hy --spy", '(foof)') output, _ = run_cmd("hy --spy", '(foof)')
@ -195,7 +195,7 @@ def test_bin_hy_icmd_file():
def test_bin_hy_icmd_and_spy(): def test_bin_hy_icmd_and_spy():
output, _ = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)") output, _ = run_cmd("hy -i \"(+ [] [])\" --spy", "(+ 1 1)")
assert "([] + [])" in output assert "[] + []" in output
def test_bin_hy_missing_file(): def test_bin_hy_missing_file():