Merge branch 'master' into pr/145
This commit is contained in:
commit
47b2709c47
@ -6,7 +6,9 @@ python:
|
|||||||
- "3.3"
|
- "3.3"
|
||||||
- "2.6"
|
- "2.6"
|
||||||
# command to install dependencies
|
# command to install dependencies
|
||||||
install: "pip install -r requirements.txt --use-mirrors"
|
install:
|
||||||
|
- pip install -r requirements.txt --use-mirrors
|
||||||
|
- python setup.py -q install
|
||||||
# # command to run tests
|
# # command to run tests
|
||||||
script: nosetests
|
script: nosetests
|
||||||
notifications:
|
notifications:
|
||||||
|
1
AUTHORS
1
AUTHORS
@ -9,3 +9,4 @@
|
|||||||
* Gergely Nagy <algernon@madhouse-project.org>
|
* Gergely Nagy <algernon@madhouse-project.org>
|
||||||
* Konrad Hinsen <konrad.hinsen@fastmail.net>
|
* Konrad Hinsen <konrad.hinsen@fastmail.net>
|
||||||
* Vladimir Gorbunov <vsg@suburban.me>
|
* Vladimir Gorbunov <vsg@suburban.me>
|
||||||
|
* John Jacobsen <john@mail.npxdesigns.com>
|
||||||
|
8
bin/hy
8
bin/hy
@ -21,7 +21,7 @@ from hy.lex.states import Idle, LexException
|
|||||||
from hy.lex.machine import Machine
|
from hy.lex.machine import Machine
|
||||||
from hy.compiler import hy_compile
|
from hy.compiler import hy_compile
|
||||||
from hy.core import process
|
from hy.core import process
|
||||||
from hy.importer import compile_
|
from hy.importer import ast_compile
|
||||||
|
|
||||||
import hy.completer
|
import hy.completer
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ class HyREPL(code.InteractiveConsole):
|
|||||||
_machine = Machine(Idle, 1, 0)
|
_machine = Machine(Idle, 1, 0)
|
||||||
try:
|
try:
|
||||||
_ast = hy_compile(tokens, root=ast.Interactive)
|
_ast = hy_compile(tokens, root=ast.Interactive)
|
||||||
code = compile_(_ast, filename, symbol)
|
code = ast_compile(_ast, filename, symbol)
|
||||||
except Exception:
|
except Exception:
|
||||||
self.showtraceback()
|
self.showtraceback()
|
||||||
return False
|
return False
|
||||||
@ -98,7 +98,7 @@ def koan_macro(tree):
|
|||||||
return HyExpression([HySymbol('print'),
|
return HyExpression([HySymbol('print'),
|
||||||
HyString("""
|
HyString("""
|
||||||
|
|
||||||
=> (import-from sh figlet)
|
=> (import [sh [figlet]])
|
||||||
=> (figlet "Hi, Hy!")
|
=> (figlet "Hi, Hy!")
|
||||||
_ _ _ _ _ _
|
_ _ _ _ _ _
|
||||||
| | | (_) | | | |_ _| |
|
| | | (_) | | | |_ _| |
|
||||||
@ -113,7 +113,7 @@ def koan_macro(tree):
|
|||||||
|
|
||||||
|
|
||||||
;;; this one plays with command line bits
|
;;; this one plays with command line bits
|
||||||
(import-from sh cat grep)
|
(import [sh [cat grep]])
|
||||||
(-> (cat "/usr/share/dict/words") (grep "-E" "bro$"))
|
(-> (cat "/usr/share/dict/words") (grep "-E" "bro$"))
|
||||||
|
|
||||||
|
|
||||||
|
2
bin/hyc
2
bin/hyc
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env hy
|
#!/usr/bin/env hy
|
||||||
|
|
||||||
(import sys)
|
(import sys)
|
||||||
(import-from hy.importer write-hy-as-pyc)
|
(import [hy.importer [write-hy-as-pyc]])
|
||||||
|
|
||||||
(write-hy-as-pyc (get sys.argv 1))
|
(write-hy-as-pyc (get sys.argv 1))
|
||||||
|
@ -324,6 +324,18 @@ Comments start with semicolons:
|
|||||||
; (print "but this will not")
|
; (print "but this will not")
|
||||||
(+ 1 2 3) ; we'll execute the addition, but not this comment!
|
(+ 1 2 3) ; we'll execute the addition, but not this comment!
|
||||||
|
|
||||||
|
Python's context managers ('with' statements) are used like this:
|
||||||
|
|
||||||
|
.. code-block:: clj
|
||||||
|
|
||||||
|
(with [f (file "/tmp/data.in")]
|
||||||
|
(print (.read f)))
|
||||||
|
|
||||||
|
which is equivalent to::
|
||||||
|
|
||||||
|
with file("/tmp/data.in") as f:
|
||||||
|
print f.read()
|
||||||
|
|
||||||
And yes, we do have lisp comprehensions! In Python you might do::
|
And yes, we do have lisp comprehensions! In Python you might do::
|
||||||
|
|
||||||
odds_squared = [
|
odds_squared = [
|
||||||
@ -392,7 +404,7 @@ a pipe:
|
|||||||
|
|
||||||
.. code-block:: clj
|
.. code-block:: clj
|
||||||
|
|
||||||
=> (import-from sh cat grep wc)
|
=> (import [sh [cat grep wc]])
|
||||||
=> (-> (cat "/usr/share/dict/words") (grep "-E" "^hy") (wc "-l"))
|
=> (-> (cat "/usr/share/dict/words") (grep "-E" "^hy") (wc "-l"))
|
||||||
210
|
210
|
||||||
|
|
||||||
@ -408,6 +420,8 @@ Much more readable, no! Use the threading macro!
|
|||||||
TODO
|
TODO
|
||||||
====
|
====
|
||||||
|
|
||||||
|
- How do I index into arrays or dictionaries?
|
||||||
|
- How do I do array ranges? e.g. x[5:] or y[2:10]
|
||||||
- How do I define classes?
|
- How do I define classes?
|
||||||
- Blow your mind with macros!
|
- Blow your mind with macros!
|
||||||
- Where's my banana???
|
- Where's my banana???
|
||||||
|
@ -35,6 +35,6 @@
|
|||||||
(print source "is a(n)" (get block "Description"))
|
(print source "is a(n)" (get block "Description"))
|
||||||
|
|
||||||
|
|
||||||
(import-from sh apt-cache)
|
(import [sh [apt-cache]])
|
||||||
(setv archive-block (parse-rfc822-stream (.show apt-cache source)))
|
(setv archive-block (parse-rfc822-stream (.show apt-cache source)))
|
||||||
(print "The archive has version" (get archive-block "Version") "of" source)
|
(print "The archive has version" (get archive-block "Version") "of" source)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
(import-from gevent.server StreamServer)
|
(import [gevent.server [StreamServer]])
|
||||||
|
|
||||||
|
|
||||||
(defn handle [socket address]
|
(defn handle [socket address]
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
(import-from concurrent.futures ThreadPoolExecutor as-completed)
|
(import [concurrent.futures [ThreadPoolExecutor as-completed]]
|
||||||
(import-from random randint)
|
[random [randint]]
|
||||||
|
[sh [sleep]])
|
||||||
(import-from sh sleep)
|
|
||||||
|
|
||||||
(defn task-to-do [] (sleep (randint 1 5)))
|
(defn task-to-do [] (sleep (randint 1 5)))
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
;; python-sh from hy
|
;; python-sh from hy
|
||||||
|
|
||||||
(import-from sh cat grep)
|
(import [sh [cat grep]])
|
||||||
(print "Words that end with `tag`:")
|
(print "Words that end with `tag`:")
|
||||||
(print (-> (cat "/usr/share/dict/words") (grep "-E" "tag$")))
|
(print (-> (cat "/usr/share/dict/words") (grep "-E" "tag$")))
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
; the source.
|
; the source.
|
||||||
|
|
||||||
(import sys)
|
(import sys)
|
||||||
(import-from sunlight openstates)
|
(import [sunlight [openstates]]
|
||||||
(import-from collections Counter)
|
[collections [Counter]])
|
||||||
|
|
||||||
|
|
||||||
(def *state* (get sys.argv 1))
|
(def *state* (get sys.argv 1))
|
||||||
|
@ -567,29 +567,10 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
raise TypeError("Unknown entry (`%s`) in the HyList" % (entry))
|
raise TypeError("Unknown entry (`%s`) in the HyList" % (entry))
|
||||||
|
|
||||||
return rimports
|
if len(rimports) == 1:
|
||||||
|
return rimports[0]
|
||||||
@builds("import_as")
|
else:
|
||||||
def compile_import_as_expression(self, expr):
|
return rimports
|
||||||
expr.pop(0) # index
|
|
||||||
modlist = [expr[i:i + 2] for i in range(0, len(expr), 2)]
|
|
||||||
return ast.Import(
|
|
||||||
lineno=expr.start_line,
|
|
||||||
col_offset=expr.start_column,
|
|
||||||
module=ast_str(expr.pop(0)),
|
|
||||||
names=[ast.alias(name=ast_str(x[0]),
|
|
||||||
asname=ast_str(x[1])) for x in modlist])
|
|
||||||
|
|
||||||
@builds("import_from")
|
|
||||||
@checkargs(min=1)
|
|
||||||
def compile_import_from_expression(self, expr):
|
|
||||||
expr.pop(0) # index
|
|
||||||
return ast.ImportFrom(
|
|
||||||
lineno=expr.start_line,
|
|
||||||
col_offset=expr.start_column,
|
|
||||||
module=ast_str(expr.pop(0)),
|
|
||||||
names=[ast.alias(name=ast_str(x), asname=None) for x in expr],
|
|
||||||
level=0)
|
|
||||||
|
|
||||||
@builds("get")
|
@builds("get")
|
||||||
@checkargs(2)
|
@checkargs(2)
|
||||||
@ -1027,7 +1008,7 @@ class HyASTCompiler(object):
|
|||||||
col_offset=expr.start_column)
|
col_offset=expr.start_column)
|
||||||
|
|
||||||
@builds("fn")
|
@builds("fn")
|
||||||
@checkargs(min=2)
|
@checkargs(min=1)
|
||||||
def compile_fn_expression(self, expression):
|
def compile_fn_expression(self, expression):
|
||||||
expression.pop(0) # fn
|
expression.pop(0) # fn
|
||||||
|
|
||||||
@ -1171,9 +1152,8 @@ def hy_compile(tree, root=None):
|
|||||||
|
|
||||||
imported.add(entry)
|
imported.add(entry)
|
||||||
imports.append(HyExpression([
|
imports.append(HyExpression([
|
||||||
HySymbol("import_from"),
|
HySymbol("import"),
|
||||||
HySymbol(package),
|
HyList([HySymbol(package), HyList([HySymbol(entry)])])
|
||||||
HySymbol(entry)
|
|
||||||
]).replace(replace))
|
]).replace(replace))
|
||||||
|
|
||||||
_ast = compiler.compile(imports) + _ast
|
_ast = compiler.compile(imports) + _ast
|
||||||
|
@ -34,45 +34,47 @@ import os
|
|||||||
import __future__
|
import __future__
|
||||||
|
|
||||||
if sys.version_info[0] >= 3:
|
if sys.version_info[0] >= 3:
|
||||||
from io import StringIO
|
|
||||||
long_type = int
|
long_type = int
|
||||||
else:
|
else:
|
||||||
from StringIO import StringIO # NOQA
|
|
||||||
import __builtin__
|
import __builtin__
|
||||||
long_type = long # NOQA
|
long_type = long # NOQA
|
||||||
|
|
||||||
|
|
||||||
def compile_(ast, filename, mode):
|
def ast_compile(ast, filename, mode):
|
||||||
|
"""Compile AST.
|
||||||
|
Like Python's compile, but with some special flags."""
|
||||||
return compile(ast, filename, mode, __future__.CO_FUTURE_DIVISION)
|
return compile(ast, filename, mode, __future__.CO_FUTURE_DIVISION)
|
||||||
|
|
||||||
|
|
||||||
def import_buffer_to_hst(fd):
|
def import_buffer_to_hst(buf):
|
||||||
tree = tokenize(fd.read() + "\n")
|
"""Import content from buf and return an Hy AST."""
|
||||||
tree = process(tree)
|
return process(tokenize(buf + "\n"))
|
||||||
return tree
|
|
||||||
|
|
||||||
|
|
||||||
def import_file_to_hst(fpath):
|
def import_file_to_hst(fpath):
|
||||||
return import_buffer_to_hst(open(fpath, 'r', encoding='utf-8'))
|
"""Import content from fpath and return an Hy AST."""
|
||||||
|
with open(fpath, 'r', encoding='utf-8') as f:
|
||||||
|
return import_buffer_to_hst(f.read())
|
||||||
|
|
||||||
|
|
||||||
|
def import_buffer_to_ast(buf):
|
||||||
|
""" Import content from buf and return a Python AST."""
|
||||||
|
return hy_compile(import_buffer_to_hst(buf))
|
||||||
|
|
||||||
|
|
||||||
def import_file_to_ast(fpath):
|
def import_file_to_ast(fpath):
|
||||||
tree = import_file_to_hst(fpath)
|
"""Import content from fpath and return a Python AST."""
|
||||||
_ast = hy_compile(tree)
|
return hy_compile(import_file_to_hst(fpath))
|
||||||
return _ast
|
|
||||||
|
|
||||||
|
|
||||||
def import_string_to_ast(buff):
|
def import_file_to_module(module_name, fpath):
|
||||||
tree = import_buffer_to_hst(StringIO(buff))
|
"""Import content from fpath and puts it into a Python module.
|
||||||
_ast = hy_compile(tree)
|
|
||||||
return _ast
|
|
||||||
|
|
||||||
|
Returns the module."""
|
||||||
def import_file_to_module(name, fpath):
|
|
||||||
_ast = import_file_to_ast(fpath)
|
_ast = import_file_to_ast(fpath)
|
||||||
mod = imp.new_module(name)
|
mod = imp.new_module(module_name)
|
||||||
mod.__file__ = fpath
|
mod.__file__ = fpath
|
||||||
eval(compile_(_ast, fpath, "exec"), mod.__dict__)
|
eval(ast_compile(_ast, fpath, "exec"), mod.__dict__)
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
|
|
||||||
@ -84,7 +86,7 @@ def hy_eval(hytree, namespace):
|
|||||||
foo.end_column = 0
|
foo.end_column = 0
|
||||||
hytree.replace(foo)
|
hytree.replace(foo)
|
||||||
_ast = hy_compile(hytree, root=ast.Expression)
|
_ast = hy_compile(hytree, root=ast.Expression)
|
||||||
return eval(compile_(_ast, "<eval>", "eval"), namespace)
|
return eval(ast_compile(_ast, "<eval>", "eval"), namespace)
|
||||||
|
|
||||||
|
|
||||||
def write_hy_as_pyc(fname):
|
def write_hy_as_pyc(fname):
|
||||||
@ -96,7 +98,7 @@ def write_hy_as_pyc(fname):
|
|||||||
timestamp = long_type(st.st_mtime)
|
timestamp = long_type(st.st_mtime)
|
||||||
|
|
||||||
_ast = import_file_to_ast(fname)
|
_ast = import_file_to_ast(fname)
|
||||||
code = compile_(_ast, fname, "exec")
|
code = ast_compile(_ast, fname, "exec")
|
||||||
cfile = "%s.pyc" % fname[:-len(".hy")]
|
cfile = "%s.pyc" % fname[:-len(".hy")]
|
||||||
|
|
||||||
if sys.version_info[0] >= 3:
|
if sys.version_info[0] >= 3:
|
||||||
@ -118,7 +120,10 @@ def write_hy_as_pyc(fname):
|
|||||||
fc.write(MAGIC)
|
fc.write(MAGIC)
|
||||||
|
|
||||||
|
|
||||||
class HyFinder(object):
|
class MetaLoader(object):
|
||||||
|
def __init__(self, path):
|
||||||
|
self.path = path
|
||||||
|
|
||||||
def is_package(self, fullname):
|
def is_package(self, fullname):
|
||||||
dirpath = "/".join(fullname.split("."))
|
dirpath = "/".join(fullname.split("."))
|
||||||
for pth in sys.path:
|
for pth in sys.path:
|
||||||
@ -128,33 +133,20 @@ class HyFinder(object):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def find_on_path(self, fullname):
|
|
||||||
fls = ["%s/__init__.hy", "%s.hy"]
|
|
||||||
dirpath = "/".join(fullname.split("."))
|
|
||||||
|
|
||||||
for pth in sys.path:
|
|
||||||
pth = os.path.abspath(pth)
|
|
||||||
for fp in fls:
|
|
||||||
composed_path = fp % ("%s/%s" % (pth, dirpath))
|
|
||||||
if os.path.exists(composed_path):
|
|
||||||
return composed_path
|
|
||||||
|
|
||||||
|
|
||||||
class MetaLoader(HyFinder):
|
|
||||||
def load_module(self, fullname):
|
def load_module(self, fullname):
|
||||||
if fullname in sys.modules:
|
if fullname in sys.modules:
|
||||||
return sys.modules[fullname]
|
return sys.modules[fullname]
|
||||||
|
|
||||||
pth = self.find_on_path(fullname)
|
if not self.path:
|
||||||
if pth is None:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
sys.modules[fullname] = None
|
sys.modules[fullname] = None
|
||||||
mod = import_file_to_module(fullname, pth)
|
mod = import_file_to_module(fullname,
|
||||||
|
self.path)
|
||||||
|
|
||||||
ispkg = self.is_package(fullname)
|
ispkg = self.is_package(fullname)
|
||||||
|
|
||||||
mod.__file__ = pth
|
mod.__file__ = self.path
|
||||||
mod.__loader__ = self
|
mod.__loader__ = self
|
||||||
mod.__name__ = fullname
|
mod.__name__ = fullname
|
||||||
|
|
||||||
@ -168,12 +160,22 @@ class MetaLoader(HyFinder):
|
|||||||
return mod
|
return mod
|
||||||
|
|
||||||
|
|
||||||
class MetaImporter(HyFinder):
|
class MetaImporter(object):
|
||||||
|
def find_on_path(self, fullname):
|
||||||
|
fls = ["%s/__init__.hy", "%s.hy"]
|
||||||
|
dirpath = "/".join(fullname.split("."))
|
||||||
|
|
||||||
|
for pth in sys.path:
|
||||||
|
pth = os.path.abspath(pth)
|
||||||
|
for fp in fls:
|
||||||
|
composed_path = fp % ("%s/%s" % (pth, dirpath))
|
||||||
|
if os.path.exists(composed_path):
|
||||||
|
return composed_path
|
||||||
|
|
||||||
def find_module(self, fullname, path=None):
|
def find_module(self, fullname, path=None):
|
||||||
pth = self.find_on_path(fullname)
|
path = self.find_on_path(fullname)
|
||||||
if pth is None:
|
if path:
|
||||||
return
|
return MetaLoader(path)
|
||||||
return MetaLoader()
|
|
||||||
|
|
||||||
|
|
||||||
sys.meta_path.append(MetaImporter())
|
sys.meta_path.append(MetaImporter())
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
;;;
|
;;;
|
||||||
;;;
|
;;;
|
||||||
|
|
||||||
(import-from hy HyExpression HySymbol HyString)
|
(import [hy [HyExpression HySymbol HyString]])
|
||||||
|
|
||||||
|
|
||||||
(defn test-basic-quoting []
|
(defn test-basic-quoting []
|
||||||
|
@ -208,13 +208,8 @@ def test_ast_bad_yield():
|
|||||||
|
|
||||||
|
|
||||||
def test_ast_good_import_from():
|
def test_ast_good_import_from():
|
||||||
"Make sure AST can compile valid import-from"
|
"Make sure AST can compile valid selective import"
|
||||||
hy_compile(tokenize("(import-from x y)"))
|
hy_compile(tokenize("(import [x [y]])"))
|
||||||
|
|
||||||
|
|
||||||
def test_ast_bad_import_from():
|
|
||||||
"Make sure AST can't compile invalid import-from"
|
|
||||||
cant_compile("(import-from)")
|
|
||||||
|
|
||||||
|
|
||||||
def test_ast_good_get():
|
def test_ast_good_get():
|
||||||
@ -300,6 +295,8 @@ def test_ast_anon_fns_basics():
|
|||||||
""" Ensure anon fns work. """
|
""" Ensure anon fns work. """
|
||||||
code = hy_compile(tokenize("(fn (x) (* x x))")).body[0]
|
code = hy_compile(tokenize("(fn (x) (* x x))")).body[0]
|
||||||
assert type(code) == ast.FunctionDef
|
assert type(code) == ast.FunctionDef
|
||||||
|
code = hy_compile(tokenize("(fn (x))")).body[0]
|
||||||
|
cant_compile("(fn)")
|
||||||
|
|
||||||
|
|
||||||
def test_ast_non_decoratable():
|
def test_ast_non_decoratable():
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from hy.importer import import_file_to_module, import_string_to_ast
|
from hy.importer import import_file_to_module, import_buffer_to_ast
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
|
|
||||||
@ -10,5 +10,5 @@ def test_basics():
|
|||||||
|
|
||||||
def test_stringer():
|
def test_stringer():
|
||||||
"Make sure the basics of the importer work"
|
"Make sure the basics of the importer work"
|
||||||
_ast = import_string_to_ast("(defn square [x] (* x x))")
|
_ast = import_buffer_to_ast("(defn square [x] (* x x))")
|
||||||
assert type(_ast.body[0]) == ast.FunctionDef
|
assert type(_ast.body[0]) == ast.FunctionDef
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
;
|
;
|
||||||
|
|
||||||
(import-from tests.resources kwtest function-with-a-dash)
|
(import [tests.resources [kwtest function-with-a-dash]]
|
||||||
(import-from os.path exists isdir isfile)
|
[os.path [exists isdir isfile]]
|
||||||
(import-as sys systest)
|
[sys :as systest])
|
||||||
(import sys)
|
(import sys)
|
||||||
|
|
||||||
|
|
||||||
@ -474,7 +474,9 @@
|
|||||||
(defn test-fn-return []
|
(defn test-fn-return []
|
||||||
"NATIVE: test function return"
|
"NATIVE: test function return"
|
||||||
(setv fn-test ((fn [] (fn [] (+ 1 1)))))
|
(setv fn-test ((fn [] (fn [] (+ 1 1)))))
|
||||||
(assert (= (fn-test) 2)))
|
(assert (= (fn-test) 2))
|
||||||
|
(setv fn-test (fn []))
|
||||||
|
(assert (= (fn-test) None)))
|
||||||
|
|
||||||
|
|
||||||
(defn test-let []
|
(defn test-let []
|
||||||
|
8
tests/test_bin.py
Normal file
8
tests/test_bin.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def test_bin_hy():
|
||||||
|
p = subprocess.Popen("echo | bin/hy",
|
||||||
|
shell=True)
|
||||||
|
p.wait()
|
||||||
|
assert p.returncode == 0
|
Loading…
x
Reference in New Issue
Block a user