Remove the import-as and import-from builtins

The new and improved (import) can handle all cases import-as and
import-from did, so drop the latter two from the language. To do this,
the import builtin had to be changed a little: if there's a single
import statement to return, return it as-is, otherwise return a list of
imports.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
This commit is contained in:
Gergely Nagy 2013-04-20 16:06:32 +02:00
parent aadf47ed99
commit 494bf0e8ad
12 changed files with 24 additions and 50 deletions

4
bin/hy
View File

@ -98,7 +98,7 @@ def koan_macro(tree):
return HyExpression([HySymbol('print'),
HyString("""
=> (import-from sh figlet)
=> (import [sh [figlet]])
=> (figlet "Hi, Hy!")
_ _ _ _ _ _
| | | (_) | | | |_ _| |
@ -113,7 +113,7 @@ def koan_macro(tree):
;;; this one plays with command line bits
(import-from sh cat grep)
(import [sh [cat grep]])
(-> (cat "/usr/share/dict/words") (grep "-E" "bro$"))

View File

@ -1,6 +1,6 @@
#!/usr/bin/env hy
(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))

View File

@ -392,7 +392,7 @@ a pipe:
.. code-block:: clj
=> (import-from sh cat grep wc)
=> (import [sh [cat grep wc]])
=> (-> (cat "/usr/share/dict/words") (grep "-E" "^hy") (wc "-l"))
210

View File

@ -35,6 +35,6 @@
(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)))
(print "The archive has version" (get archive-block "Version") "of" source)

View File

@ -1,4 +1,4 @@
(import-from gevent.server StreamServer)
(import [gevent.server [StreamServer]])
(defn handle [socket address]

View File

@ -1,7 +1,6 @@
(import-from concurrent.futures ThreadPoolExecutor as-completed)
(import-from random randint)
(import-from sh sleep)
(import [concurrent.futures [ThreadPoolExecutor as-completed]]
[random [randint]]
[sh [sleep]])
(defn task-to-do [] (sleep (randint 1 5)))

View File

@ -1,5 +1,5 @@
;; python-sh from hy
(import-from sh cat grep)
(import [sh [cat grep]])
(print "Words that end with `tag`:")
(print (-> (cat "/usr/share/dict/words") (grep "-E" "tag$")))

View File

@ -4,8 +4,8 @@
; the source.
(import sys)
(import-from sunlight openstates)
(import-from collections Counter)
(import [sunlight [openstates]]
[collections [Counter]])
(def *state* (get sys.argv 1))

View File

@ -567,29 +567,10 @@ class HyASTCompiler(object):
raise TypeError("Unknown entry (`%s`) in the HyList" % (entry))
return rimports
@builds("import_as")
def compile_import_as_expression(self, expr):
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)
if len(rimports) == 1:
return rimports[0]
else:
return rimports
@builds("get")
@checkargs(2)
@ -1171,9 +1152,8 @@ def hy_compile(tree, root=None):
imported.add(entry)
imports.append(HyExpression([
HySymbol("import_from"),
HySymbol(package),
HySymbol(entry)
HySymbol("import"),
HyList([HySymbol(package), HyList([HySymbol(entry)])])
]).replace(replace))
_ast = compiler.compile(imports) + _ast

View File

@ -1,7 +1,7 @@
;;;
;;;
(import-from hy HyExpression HySymbol HyString)
(import [hy [HyExpression HySymbol HyString]])
(defn test-basic-quoting []

View File

@ -208,13 +208,8 @@ def test_ast_bad_yield():
def test_ast_good_import_from():
"Make sure AST can compile valid import-from"
hy_compile(tokenize("(import-from x y)"))
def test_ast_bad_import_from():
"Make sure AST can't compile invalid import-from"
cant_compile("(import-from)")
"Make sure AST can compile valid selective import"
hy_compile(tokenize("(import [x [y]])"))
def test_ast_good_get():

View File

@ -1,8 +1,8 @@
;
(import-from tests.resources kwtest function-with-a-dash)
(import-from os.path exists isdir isfile)
(import-as sys systest)
(import [tests.resources [kwtest function-with-a-dash]]
[os.path [exists isdir isfile]]
[sys :as systest])
(import sys)