Skip to content

Commit ee0750a

Browse files
authored
Don't create non-existent namespaces in Var.find (#211)
* Don't create non-existent namespace in Var.find * Add a test
1 parent be6332e commit ee0750a

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/basilisp/lang/runtime.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ def intern_unbound(ns: sym.Symbol,
175175
def find_in_ns(ns_sym: sym.Symbol, name_sym: sym.Symbol) -> "Optional[Var]":
176176
"""Return the value current bound to the name `name_sym` in the namespace
177177
specified by `ns_sym`."""
178-
ns = Namespace.get_or_create(ns_sym)
179-
return ns.find(name_sym)
178+
ns = Namespace.get(ns_sym)
179+
if ns:
180+
return ns.find(name_sym)
181+
return None
180182

181183
@staticmethod
182184
def find(ns_qualified_sym: sym.Symbol) -> "Optional[Var]":
@@ -419,6 +421,12 @@ def get_or_create(cls, name: sym.Symbol, module: types.ModuleType = None) -> "Na
419421
Return the namespace."""
420422
return cls._NAMESPACES.swap(Namespace.__get_or_create, name, module=module)[name]
421423

424+
@classmethod
425+
def get(cls, name: sym.Symbol) -> "Optional[Namespace]":
426+
"""Get the namespace bound to the symbol `name` in the global namespace
427+
cache. Return the namespace if it exists or None otherwise.."""
428+
return cls._NAMESPACES.deref().entry(name, None)
429+
422430
@classmethod
423431
def remove(cls, name: sym.Symbol) -> Optional["Namespace"]:
424432
"""Remove the namespace bound to the symbol `name` in the global

tests/basilisp/namespace_test.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def test_create_ns(ns_sym: sym.Symbol, ns_cache: patch):
4646

4747

4848
@pytest.fixture
49-
def ns_cache_with_existing_ns(ns_sym: sym.Symbol, core_ns_sym: sym.Symbol,
49+
def ns_cache_with_existing_ns(ns_sym: sym.Symbol,
50+
core_ns_sym: sym.Symbol,
5051
core_ns: Namespace) -> patch:
5152
"""Patch the Namespace cache with a test fixture with an existing namespace."""
5253
return patch('basilisp.lang.runtime.Namespace._NAMESPACES',
@@ -67,6 +68,26 @@ def test_get_existing_ns(ns_sym: sym.Symbol,
6768
assert len(cache.deref().keys()) == 2
6869

6970

71+
def test_get_existing_ns_with_get(ns_sym: sym.Symbol,
72+
ns_cache_with_existing_ns: patch):
73+
with ns_cache_with_existing_ns as cache:
74+
assert len(cache.deref().keys()) == 2
75+
ns = Namespace.get(ns_sym)
76+
assert isinstance(ns, Namespace)
77+
assert ns.name == ns_sym.name
78+
assert len(cache.deref().keys()) == 2
79+
80+
81+
def test_get_nil_for_non_existent_ns(ns_sym: sym.Symbol):
82+
with patch('basilisp.lang.runtime.Namespace._NAMESPACES',
83+
atom.Atom(
84+
lmap.map({}))) as cache:
85+
assert len(cache.deref().keys()) == 0
86+
ns = Namespace.get(ns_sym)
87+
assert ns is None
88+
assert len(cache.deref().keys()) == 0
89+
90+
7091
def test_remove_ns(ns_sym: sym.Symbol, ns_cache_with_existing_ns: patch):
7192
with ns_cache_with_existing_ns as cache:
7293
assert len(cache.deref().keys()) == 2

0 commit comments

Comments
 (0)