Merge pull request #1619 from Kodiologist/stdlib-fix

Fix the management of _stdlib
This commit is contained in:
Ryan Gonzalez 2018-05-27 16:26:57 -05:00 committed by GitHub
commit e9e27c7a4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 17 deletions

View File

@ -52,14 +52,15 @@ _stdlib = {}
def load_stdlib():
import hy.core
for module in hy.core.STDLIB:
mod = importlib.import_module(module)
for e in map(ast_str, mod.EXPORTS):
if getattr(mod, e) is not getattr(builtins, e, ''):
# Don't bother putting a name in _stdlib if it
# points to a builtin with the same name. This
# prevents pointless imports.
_stdlib[e] = module
if not _stdlib:
for module in hy.core.STDLIB:
mod = importlib.import_module(module)
for e in map(ast_str, mod.EXPORTS):
if getattr(mod, e) is not getattr(builtins, e, ''):
# Don't bother putting a name in _stdlib if it
# points to a builtin with the same name. This
# prevents pointless imports.
_stdlib[e] = module
def ast_str(x, piecewise=False):
@ -355,8 +356,12 @@ class HyASTCompiler(object):
self.imports = defaultdict(set)
self.module_name = module_name
self.temp_if = None
if not module_name.startswith("hy.core"):
# everything in core needs to be explicit.
self.can_use_stdlib = (
not module_name.startswith("hy.core")
or module_name == "hy.core.macros")
# Everything in core needs to be explicit (except for
# the core macros, which are built with the core functions).
if self.can_use_stdlib:
load_stdlib()
def get_anon_var(self):
@ -1698,7 +1703,7 @@ class HyASTCompiler(object):
attr=ast_str(local),
ctx=ast.Load())
if ast_str(symbol) in _stdlib:
if self.can_use_stdlib and ast_str(symbol) in _stdlib:
self.imports[_stdlib[ast_str(symbol)]].add(ast_str(symbol))
return asty.Name(symbol, id=ast_str(symbol), ctx=ast.Load())

View File

@ -5,8 +5,10 @@
;;;; Hy shadow functions
(import operator)
(import [hy._compat [PY35]])
(import [hy._compat [PY3 PY35]])
(if PY3
(import [functools [reduce]]))
(defn + [&rest args]
"Shadowed `+` operator adds `args`."
@ -14,7 +16,7 @@
(= (len args) 0)
0
(= (len args) 1)
(+ (first args))
(+ (get args 0))
; else
(reduce operator.add args)))
@ -30,7 +32,7 @@
(= (len args) 0)
1
(= (len args) 1)
(first args)
(get args 0)
; else
(reduce operator.mul args)))
@ -81,7 +83,7 @@
(= (len args) 0)
0
(= (len args) 1)
(first args)
(get args 0)
; else
(reduce operator.or_ args)))
@ -130,7 +132,7 @@
(= (len args) 0)
True
(= (len args) 1)
(first args)
(get args 0)
; else
(reduce (fn [x y] (and x y)) args)))
@ -140,7 +142,7 @@
(= (len args) 0)
None
(= (len args) 1)
(first args)
(get args 0)
; else
(reduce (fn [x y] (or x y)) args)))