Fix the management of _stdlib
_stdlib is a global variable, so core modules could use it, contrary to our intention, so long as they happened to be compiled after a non-core module. I've added a compiler attribute to track whether we can use _stdlib. This fix exposed some cases where hy.core.shadow tried to use a core function, so I fixed those. I've also added an `if not _stdlib` to `load_stdlib` so that we only bother to actually load _stdlib once.
This commit is contained in:
parent
370563567a
commit
e344ac1fd6
@ -52,14 +52,15 @@ _stdlib = {}
|
|||||||
|
|
||||||
def load_stdlib():
|
def load_stdlib():
|
||||||
import hy.core
|
import hy.core
|
||||||
for module in hy.core.STDLIB:
|
if not _stdlib:
|
||||||
mod = importlib.import_module(module)
|
for module in hy.core.STDLIB:
|
||||||
for e in map(ast_str, mod.EXPORTS):
|
mod = importlib.import_module(module)
|
||||||
if getattr(mod, e) is not getattr(builtins, e, ''):
|
for e in map(ast_str, mod.EXPORTS):
|
||||||
# Don't bother putting a name in _stdlib if it
|
if getattr(mod, e) is not getattr(builtins, e, ''):
|
||||||
# points to a builtin with the same name. This
|
# Don't bother putting a name in _stdlib if it
|
||||||
# prevents pointless imports.
|
# points to a builtin with the same name. This
|
||||||
_stdlib[e] = module
|
# prevents pointless imports.
|
||||||
|
_stdlib[e] = module
|
||||||
|
|
||||||
|
|
||||||
def ast_str(x, piecewise=False):
|
def ast_str(x, piecewise=False):
|
||||||
@ -355,8 +356,12 @@ class HyASTCompiler(object):
|
|||||||
self.imports = defaultdict(set)
|
self.imports = defaultdict(set)
|
||||||
self.module_name = module_name
|
self.module_name = module_name
|
||||||
self.temp_if = None
|
self.temp_if = None
|
||||||
if not module_name.startswith("hy.core"):
|
self.can_use_stdlib = (
|
||||||
# everything in core needs to be explicit.
|
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()
|
load_stdlib()
|
||||||
|
|
||||||
def get_anon_var(self):
|
def get_anon_var(self):
|
||||||
@ -1698,7 +1703,7 @@ class HyASTCompiler(object):
|
|||||||
attr=ast_str(local),
|
attr=ast_str(local),
|
||||||
ctx=ast.Load())
|
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))
|
self.imports[_stdlib[ast_str(symbol)]].add(ast_str(symbol))
|
||||||
|
|
||||||
return asty.Name(symbol, id=ast_str(symbol), ctx=ast.Load())
|
return asty.Name(symbol, id=ast_str(symbol), ctx=ast.Load())
|
||||||
|
@ -5,8 +5,10 @@
|
|||||||
;;;; Hy shadow functions
|
;;;; Hy shadow functions
|
||||||
|
|
||||||
(import operator)
|
(import operator)
|
||||||
(import [hy._compat [PY35]])
|
(import [hy._compat [PY3 PY35]])
|
||||||
|
|
||||||
|
(if PY3
|
||||||
|
(import [functools [reduce]]))
|
||||||
|
|
||||||
(defn + [&rest args]
|
(defn + [&rest args]
|
||||||
"Shadowed `+` operator adds `args`."
|
"Shadowed `+` operator adds `args`."
|
||||||
@ -14,7 +16,7 @@
|
|||||||
(= (len args) 0)
|
(= (len args) 0)
|
||||||
0
|
0
|
||||||
(= (len args) 1)
|
(= (len args) 1)
|
||||||
(+ (first args))
|
(+ (get args 0))
|
||||||
; else
|
; else
|
||||||
(reduce operator.add args)))
|
(reduce operator.add args)))
|
||||||
|
|
||||||
@ -30,7 +32,7 @@
|
|||||||
(= (len args) 0)
|
(= (len args) 0)
|
||||||
1
|
1
|
||||||
(= (len args) 1)
|
(= (len args) 1)
|
||||||
(first args)
|
(get args 0)
|
||||||
; else
|
; else
|
||||||
(reduce operator.mul args)))
|
(reduce operator.mul args)))
|
||||||
|
|
||||||
@ -81,7 +83,7 @@
|
|||||||
(= (len args) 0)
|
(= (len args) 0)
|
||||||
0
|
0
|
||||||
(= (len args) 1)
|
(= (len args) 1)
|
||||||
(first args)
|
(get args 0)
|
||||||
; else
|
; else
|
||||||
(reduce operator.or_ args)))
|
(reduce operator.or_ args)))
|
||||||
|
|
||||||
@ -130,7 +132,7 @@
|
|||||||
(= (len args) 0)
|
(= (len args) 0)
|
||||||
True
|
True
|
||||||
(= (len args) 1)
|
(= (len args) 1)
|
||||||
(first args)
|
(get args 0)
|
||||||
; else
|
; else
|
||||||
(reduce (fn [x y] (and x y)) args)))
|
(reduce (fn [x y] (and x y)) args)))
|
||||||
|
|
||||||
@ -140,7 +142,7 @@
|
|||||||
(= (len args) 0)
|
(= (len args) 0)
|
||||||
None
|
None
|
||||||
(= (len args) 1)
|
(= (len args) 1)
|
||||||
(first args)
|
(get args 0)
|
||||||
; else
|
; else
|
||||||
(reduce (fn [x y] (or x y)) args)))
|
(reduce (fn [x y] (or x y)) args)))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user