Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 516dcaf

Browse files
committed
load extensions in builtin trap
ensures `get_ipython` et al. are available.
1 parent 9087bb9 commit 516dcaf

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

IPython/core/extensions.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,16 @@ def load_extension(self, module_str):
9292
return "already loaded"
9393

9494
from IPython.utils.syspathcontext import prepended_to_syspath
95-
96-
if module_str not in sys.modules:
97-
with prepended_to_syspath(self.ipython_extension_dir):
98-
__import__(module_str)
99-
mod = sys.modules[module_str]
100-
if self._call_load_ipython_extension(mod):
101-
self.loaded.add(module_str)
102-
else:
103-
return "no load function"
95+
96+
with self.shell.builtin_trap:
97+
if module_str not in sys.modules:
98+
with prepended_to_syspath(self.ipython_extension_dir):
99+
__import__(module_str)
100+
mod = sys.modules[module_str]
101+
if self._call_load_ipython_extension(mod):
102+
self.loaded.add(module_str)
103+
else:
104+
return "no load function"
104105

105106
def unload_extension(self, module_str):
106107
"""Unload an IPython extension by its module name.

IPython/core/tests/test_extension.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def load_ipython_extension(ip):
1919
print("Running ext2 load")
2020
"""
2121

22+
ext3_content = """
23+
def load_ipython_extension(ip):
24+
ip2 = get_ipython()
25+
print(ip is ip2)
26+
"""
27+
2228
def test_extension_loading():
2329
em = get_ipython().extension_manager
2430
with TemporaryDirectory() as td:
@@ -68,6 +74,23 @@ def test_extension_loading():
6874
with tt.AssertPrints("Running ext2 load"):
6975
em.reload_extension('ext2')
7076

77+
78+
def test_extension_builtins():
79+
em = get_ipython().extension_manager
80+
with TemporaryDirectory() as td:
81+
ext3 = os.path.join(td, 'ext3.py')
82+
with open(ext3, 'w') as f:
83+
f.write(ext3_content)
84+
85+
assert 'ext3' not in em.loaded
86+
87+
with prepended_to_syspath(td):
88+
# Load extension
89+
with tt.AssertPrints("True"):
90+
assert em.load_extension('ext3') is None
91+
assert 'ext3' in em.loaded
92+
93+
7194
def test_non_extension():
7295
em = get_ipython().extension_manager
7396
nt.assert_equal(em.load_extension('sys'), "no load function")

0 commit comments

Comments
 (0)