def load_extension(self, module_str): """Load an yap_ipython extension by its module name. Returns the string "already loaded" if the extension is already loaded, "no load function" if the module doesn't have a load_ipython_extension function, or None if it succeeded. """ if module_str in self.loaded: return "already loaded" from yap_ipython.utils.syspathcontext import prepended_to_syspath with self.shell.builtin_trap: if module_str not in sys.modules: with prepended_to_syspath(self.ipython_extension_dir): mod = import_module(module_str) if mod.__file__.startswith(self.ipython_extension_dir): print(( "Loading extensions from {dir} is deprecated. " "We recommend managing extensions like any " "other Python packages, in site-packages.").format( dir=compress_user(self.ipython_extension_dir))) mod = sys.modules[module_str] if self._call_load_ipython_extension(mod): self.loaded.add(module_str) else: return "no load function"
def test_changing_py_file(self): """Traceback produced if the line where the error occurred is missing? https://github.com/ipython/ipython/issues/1456 """ with TemporaryDirectory() as td: fname = os.path.join(td, "foo.py") with open(fname, "w") as f: f.write(file_1) with prepended_to_syspath(td): ip.run_cell("import foo") with tt.AssertPrints("ZeroDivisionError"): ip.run_cell("foo.f()") # Make the file shorter, so the line of the error is missing. with open(fname, "w") as f: f.write(file_2) # For some reason, this was failing on the *second* call after # changing the file, so we call f() twice. with tt.AssertNotPrints("Internal Python error", channel='stderr'): with tt.AssertPrints("ZeroDivisionError"): ip.run_cell("foo.f()") with tt.AssertPrints("ZeroDivisionError"): ip.run_cell("foo.f()")
def load_extension(self, module_str): """Load an yap_ipython extension by its module name. Returns the string "already loaded" if the extension is already loaded, "no load function" if the module doesn't have a load_ipython_extension function, or None if it succeeded. """ if module_str in self.loaded: return "already loaded" from yap_ipython.utils.syspathcontext import prepended_to_syspath with self.shell.builtin_trap: if module_str not in sys.modules: with prepended_to_syspath(self.ipython_extension_dir): mod = import_module(module_str) if mod.__file__.startswith(self.ipython_extension_dir): print(("Loading extensions from {dir} is deprecated. " "We recommend managing extensions like any " "other Python packages, in site-packages.").format( dir=compress_user(self.ipython_extension_dir))) mod = sys.modules[module_str] if self._call_load_ipython_extension(mod): self.loaded.add(module_str) else: return "no load function"
def test_nonascii_path(self): # Non-ascii directory name as well. with TemporaryDirectory(suffix=u'é') as td: fname = os.path.join(td, u"fooé.py") with open(fname, "w") as f: f.write(file_1) with prepended_to_syspath(td): ip.run_cell("import foo") with tt.AssertPrints("ZeroDivisionError"): ip.run_cell("foo.f()")
def test_iso8859_5(self): with TemporaryDirectory() as td: fname = os.path.join(td, 'dfghjkl.py') with io.open(fname, 'w', encoding='iso-8859-5') as f: f.write(iso_8859_5_file) with prepended_to_syspath(td): ip.run_cell("from dfghjkl import fail") with tt.AssertPrints("ZeroDivisionError"): with tt.AssertPrints(u'дбИЖ', suppress=False): ip.run_cell('fail()')
def test_extension_builtins(): em = get_ipython().extension_manager with TemporaryDirectory() as td: ext3 = os.path.join(td, 'ext3.py') with open(ext3, 'w') as f: f.write(ext3_content) assert 'ext3' not in em.loaded with prepended_to_syspath(td): # Load extension with tt.AssertPrints("True"): assert em.load_extension('ext3') is None assert 'ext3' in em.loaded
def test_extension_loading(): em = get_ipython().extension_manager with TemporaryDirectory() as td: ext1 = os.path.join(td, 'ext1.py') with open(ext1, 'w') as f: f.write(ext1_content) ext2 = os.path.join(td, 'ext2.py') with open(ext2, 'w') as f: f.write(ext2_content) with prepended_to_syspath(td): assert 'ext1' not in em.loaded assert 'ext2' not in em.loaded # Load extension with tt.AssertPrints("Running ext1 load"): assert em.load_extension('ext1') is None assert 'ext1' in em.loaded # Should refuse to load it again with tt.AssertNotPrints("Running ext1 load"): assert em.load_extension('ext1') == 'already loaded' # Reload with tt.AssertPrints("Running ext1 unload"): with tt.AssertPrints("Running ext1 load", suppress=False): em.reload_extension('ext1') # Unload with tt.AssertPrints("Running ext1 unload"): assert em.unload_extension('ext1') is None # Can't unload again with tt.AssertNotPrints("Running ext1 unload"): assert em.unload_extension('ext1') == 'not loaded' assert em.unload_extension('ext2') == 'not loaded' # Load extension 2 with tt.AssertPrints("Running ext2 load"): assert em.load_extension('ext2') is None # Can't unload this assert em.unload_extension('ext2') == 'no unload function' # But can reload it with tt.AssertPrints("Running ext2 load"): em.reload_extension('ext2')
def reload_extension(self, module_str): """Reload an yap_ipython extension by calling reload. If the module has not been loaded before, :meth:`InteractiveShell.load_extension` is called. Otherwise :func:`reload` is called and then the :func:`load_ipython_extension` function of the module, if it exists is called. """ from yap_ipython.utils.syspathcontext import prepended_to_syspath if (module_str in self.loaded) and (module_str in sys.modules): self.unload_extension(module_str) mod = sys.modules[module_str] with prepended_to_syspath(self.ipython_extension_dir): reload(mod) if self._call_load_ipython_extension(mod): self.loaded.add(module_str) else: self.load_extension(module_str)