From 0c56885d42af686b6dada5d0d72fa5d836bb2714 Mon Sep 17 00:00:00 2001 From: Konrad Hinsen Date: Fri, 12 Apr 2013 05:23:25 +0200 Subject: [PATCH] Use __future__.division for all Hy code Fixes #106 Note: This is implemented by replacing all calls to Python's builtin "compile" function by calls to hy.importer.compile_, which adds the "future division" flag. Anyone using "compile" in future work will have to remember this. --- bin/hy | 3 ++- hy/importer.py | 12 ++++++++---- tests/native_tests/math.hy | 5 ++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/bin/hy b/bin/hy index 2e27f56..45405aa 100755 --- a/bin/hy +++ b/bin/hy @@ -21,6 +21,7 @@ from hy.lex.states import Idle, LexException from hy.lex.machine import Machine from hy.compiler import hy_compile from hy.core import process +from hy.importer import compile_ import hy.completer @@ -58,7 +59,7 @@ class HyREPL(code.InteractiveConsole): _machine = Machine(Idle, 1, 0) try: _ast = hy_compile(tokens, root=ast.Interactive) - code = compile(_ast, filename, symbol) + code = compile_(_ast, filename, symbol) except Exception: self.showtraceback() return False diff --git a/hy/importer.py b/hy/importer.py index ee9c405..580020e 100644 --- a/hy/importer.py +++ b/hy/importer.py @@ -31,7 +31,7 @@ import imp import sys import ast import os - +import __future__ if sys.version_info[0] >= 3: from io import StringIO @@ -39,6 +39,10 @@ else: from StringIO import StringIO # NOQA +def compile_(ast, filename, mode): + return compile(ast, filename, mode, __future__.CO_FUTURE_DIVISION) + + def import_buffer_to_hst(fd): tree = tokenize(fd.read() + "\n") tree = process(tree) @@ -65,7 +69,7 @@ def import_file_to_module(name, fpath): _ast = import_file_to_ast(fpath) mod = imp.new_module(name) mod.__file__ = fpath - eval(compile(_ast, fpath, "exec"), mod.__dict__) + eval(compile_(_ast, fpath, "exec"), mod.__dict__) return mod @@ -77,7 +81,7 @@ def hy_eval(hytree, namespace): foo.end_column = 0 hytree.replace(foo) _ast = hy_compile(hytree, root=ast.Expression) - return eval(compile(_ast, "", "eval"), namespace) + return eval(compile_(_ast, "", "eval"), namespace) def write_hy_as_pyc(fname): @@ -88,7 +92,7 @@ def write_hy_as_pyc(fname): timestamp = long(os.stat(fname).st_mtime) _ast = import_file_to_ast(fname) - code = compile(_ast, fname, "exec") + code = compile_(_ast, fname, "exec") cfile = "%s.pyc" % fname[:-len(".hy")] with open(cfile, 'wb') as fc: diff --git a/tests/native_tests/math.hy b/tests/native_tests/math.hy index 8570ceb..d2bcaea 100644 --- a/tests/native_tests/math.hy +++ b/tests/native_tests/math.hy @@ -27,7 +27,10 @@ (setv test_div (fn [] "NATIVE: Test division" - (assert (= 25 (/ 100 2 2))))) + (assert (= 25 (/ 100 2 2))) + ; Commented out until float constants get implemented + ; (assert (= 0.5 (/ 1 2))) + (assert (= 1 (* 2 (/ 1 2)))))) (setv test_int_div (fn [] "NATIVE: Test integer division"