Skip to content

Commit 2f6c160

Browse files
authored
Do not add Importer again after it was initially added (#213)
1 parent dd7738d commit 2f6c160

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/basilisp/importer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def get_filename(self, fullname: str) -> str:
5959

6060
def create_module(self, spec: importlib.machinery.ModuleSpec):
6161
mod = types.ModuleType(spec.name)
62+
mod.__file__ = spec.loader_state["filename"]
6263
mod.__loader__ = spec.loader
6364
mod.__package__ = spec.parent
6465
mod.__spec__ = spec
@@ -103,4 +104,6 @@ def hook_imports():
103104
104105
Once this is called, Basilisp code may be called from within Python code
105106
using standard `import module.submodule` syntax."""
107+
if any([isinstance(o, BasilispImporter) for o in sys.meta_path]):
108+
return
106109
sys.meta_path.insert(0, BasilispImporter()) # pylint:disable=abstract-class-instantiated

tests/basilisp/importer_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
from unittest.mock import patch
3+
4+
import basilisp.importer as importer
5+
6+
7+
def importer_counter():
8+
return sum([isinstance(o, importer.BasilispImporter) for o in sys.meta_path])
9+
10+
11+
def test_hook_imports():
12+
with patch('sys.meta_path',
13+
new=[]):
14+
assert 0 == importer_counter()
15+
importer.hook_imports()
16+
assert 1 == importer_counter()
17+
importer.hook_imports()
18+
assert 1 == importer_counter()
19+
20+
with patch('sys.meta_path',
21+
new=[importer.BasilispImporter()]):
22+
assert 1 == importer_counter()
23+
importer.hook_imports()
24+
assert 1 == importer_counter()

0 commit comments

Comments
 (0)