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.
This commit is contained in:
Konrad Hinsen 2013-04-12 05:23:25 +02:00
parent db3e2bd346
commit 0c56885d42
3 changed files with 14 additions and 6 deletions

3
bin/hy
View File

@ -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

View File

@ -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>", "eval"), namespace)
return eval(compile_(_ast, "<eval>", "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:

View File

@ -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"