Exemplo n.º 1
0
def test_deepreload():
    "Test that dreload does deep reloads and skips excluded modules."
    with TemporaryDirectory() as tmpdir:
        with prepended_to_syspath(tmpdir):
            tmpdirpath = Path(tmpdir)
            with open(tmpdirpath / "A.py", "w") as f:
                f.write("class Object:\n    pass\nok = True\n")
            with open(tmpdirpath / "B.py", "w") as f:
                f.write("import A\nassert A.ok, 'we are fine'\n")
            import A
            import B

            # Test that A is not reloaded.
            obj = A.Object()
            dreload(B, exclude=["A"])
            assert isinstance(obj, A.Object) is True

            # Test that an import failure will not blow-up us.
            A.ok = False
            with pytest.raises(AssertionError, match="we are fine"):
                dreload(B, exclude=["A"])
            assert len(modules_reloading) == 0
            assert not A.ok

            # Test that A is reloaded.
            obj = A.Object()
            A.ok = False
            dreload(B)
            assert A.ok
            assert isinstance(obj, A.Object) is False
Exemplo n.º 2
0
    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()")
Exemplo n.º 3
0
    def load_extension(self, module_str):
        """Load an 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 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"
Exemplo n.º 4
0
    def load_extension(self, module_str):
        """Load an 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 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"
Exemplo n.º 5
0
 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 importfile(self, parameter_s=''):
        """
        Import module given a file.

        This command tries to import a given file as a module.
        Following methods are applied in order:

        1. If the absolute path of a given file starts with one of the
           path in `sys.path`, the given file is imported as a normal
           module.
        2. If there is ``__init__.py`` is in each sub-directory from
           the current working directory and to the file, the given
           file is imported as a normal module.
        3. If there is `setup.py` in one of the parent directory of
           the given file, the file is imported as a module in a
           package located at the location where `setup.py` is.
        4. If file is a valid python module name, the file is imported
           as a stand alone module.
        5. If none of above matches, the file is imported using
           '%run -n' magic command.

        """
        args = parse_argstring(self.importfile, parameter_s)
        abspath = os.path.abspath(os.path.expanduser(args.path))

        for method in [self._method_sys_path,
                       self._method_init,
                       self._method_setup_py,
                       self._method_stand_alone]:
            rootpath = method(abspath)
            if rootpath:
                break
        else:
            # Given path is not a valid module path.  Use %run -n.
            if args.verbose:
                print "%run -n {0}".format(args.path)
            self.shell.run_line_magic('run', "-n {0}".format(args.path))
            return

        modulepath = self._construct_modulepath(abspath, rootpath)
        commands = ["import {0}".format(modulepath)]
        if args.reload:
            commands.append("reload({0})".format(modulepath))

        if args.star:
            commands.append("from {0} import *".format(modulepath))
        elif args.names:
            commands.append("from {0} import {1}".format(
                modulepath, ", ".join(args.names)))

        code = "\n".join(commands)
        if args.verbose:
            print code

        with prepended_to_syspath(rootpath):
            self.shell.ex(code)
Exemplo n.º 7
0
    def importfile(self, parameter_s=''):
        """
        Import module given a file.

        This command tries to import a given file as a module.
        Following methods are applied in order:

        1. If the absolute path of a given file starts with one of the
           path in `sys.path`, the given file is imported as a normal
           module.
        2. If there is ``__init__.py`` is in each sub-directory from
           the current working directory and to the file, the given
           file is imported as a normal module.
        3. If there is `setup.py` in one of the parent directory of
           the given file, the file is imported as a module in a
           package located at the location where `setup.py` is.
        4. If file is a valid python module name, the file is imported
           as a stand alone module.
        5. If none of above matches, the file is imported using
           '%run -n' magic command.

        """
        args = parse_argstring(self.importfile, parameter_s)
        abspath = os.path.abspath(os.path.expanduser(args.path))

        for method in [
                self._method_sys_path, self._method_init,
                self._method_setup_py, self._method_stand_alone
        ]:
            rootpath = method(abspath)
            if rootpath:
                break
        else:
            # Given path is not a valid module path.  Use %run -n.
            if args.verbose:
                print "%run -n {0}".format(args.path)
            self.shell.run_line_magic('run', "-n {0}".format(args.path))
            return

        modulepath = self._construct_modulepath(abspath, rootpath)
        commands = ["import {0}".format(modulepath)]
        if args.reload:
            commands.append("reload({0})".format(modulepath))

        if args.star:
            commands.append("from {0} import *".format(modulepath))
        elif args.names:
            commands.append("from {0} import {1}".format(
                modulepath, ", ".join(args.names)))

        code = "\n".join(commands)
        if args.verbose:
            print code

        with prepended_to_syspath(rootpath):
            self.shell.ex(code)
Exemplo n.º 8
0
    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()")
Exemplo n.º 9
0
 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()")
Exemplo n.º 10
0
    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()')
Exemplo n.º 11
0
    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()')
Exemplo n.º 12
0
    def load_extension(self, module_str):
        """Load an IPython extension by its module name.

        If :func:`load_ipython_extension` returns anything, this function
        will return that object.
        """
        from IPython.utils.syspathcontext import prepended_to_syspath

        if module_str not in sys.modules:
            with prepended_to_syspath(self.ipython_extension_dir):
                __import__(module_str)
        mod = sys.modules[module_str]
        return self._call_load_ipython_extension(mod)
Exemplo n.º 13
0
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
Exemplo n.º 14
0
def test_extension_builtins():
    em = get_ipython().extension_manager
    with TemporaryDirectory() as td:
        ext3 = os.path.join(td, "ext3.py")
        with open(ext3, "w", encoding="utf-8") 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
Exemplo n.º 15
0
def test_lazy_magics():
    with pytest.raises(UsageError):
        ip.run_line_magic("lazy_line", "")

    startdir = os.getcwd()

    with TemporaryDirectory() as tmpdir:
        with prepended_to_syspath(tmpdir):
            ptempdir = Path(tmpdir)
            tf = ptempdir / "lazy_magic_module.py"
            tf.write_text(MINIMAL_LAZY_MAGIC)
            ip.magics_manager.register_lazy("lazy_line",
                                            Path(tf.name).name[:-3])
            with tt.AssertPrints("Lazy Line"):
                ip.run_line_magic("lazy_line", "")
Exemplo n.º 16
0
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')
Exemplo n.º 17
0
def test_extension_loading():
    em = get_ipython().extension_manager
    with TemporaryDirectory() as td:
        ext1 = os.path.join(td, "ext1.py")
        with open(ext1, "w", encoding="utf-8") as f:
            f.write(ext1_content)

        ext2 = os.path.join(td, "ext2.py")
        with open(ext2, "w", encoding="utf-8") 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')
Exemplo n.º 18
0
    def reload_extension(self, module_str):
        """Reload an 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 IPython.utils.syspathcontext import prepended_to_syspath

        with prepended_to_syspath(self.ipython_extension_dir):
            if module_str in sys.modules:
                mod = sys.modules[module_str]
                reload(mod)
                self._call_load_ipython_extension(mod)
            else:
                self.load_extension(module_str)
Exemplo n.º 19
0
def test_deepreload():
    "Test that dreload does deep reloads and skips excluded modules."
    with TemporaryDirectory() as tmpdir:
        with prepended_to_syspath(tmpdir):
            with open(os.path.join(tmpdir, "A.py"), "w") as f:
                f.write("class Object(object):\n    pass\n")
            with open(os.path.join(tmpdir, "B.py"), "w") as f:
                f.write("import A\n")
            import A
            import B

            # Test that A is not reloaded.
            obj = A.Object()
            dreload(B, exclude=["A"])
            nt.assert_true(isinstance(obj, A.Object))

            # Test that A is reloaded.
            obj = A.Object()
            dreload(B)
            nt.assert_false(isinstance(obj, A.Object))
Exemplo n.º 20
0
def test_deepreload():
    "Test that dreload does deep reloads and skips excluded modules."
    with TemporaryDirectory() as tmpdir:
        with prepended_to_syspath(tmpdir):
            with open(os.path.join(tmpdir, "A.py"), "w") as f:
                f.write("class Object(object):\n    pass\n")
            with open(os.path.join(tmpdir, "B.py"), "w") as f:
                f.write("import A\n")
            import A
            import B

            # Test that A is not reloaded.
            obj = A.Object()
            dreload(B, exclude=["A"])
            nt.assert_true(isinstance(obj, A.Object))

            # Test that A is reloaded.
            obj = A.Object()
            dreload(B)
            nt.assert_false(isinstance(obj, A.Object))
Exemplo n.º 21
0
    def load_extension(self, module_str):
        """Load an 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 IPython.utils.syspathcontext import prepended_to_syspath

        if module_str not in sys.modules:
            with prepended_to_syspath(self.ipython_extension_dir):
                __import__(module_str)
        mod = sys.modules[module_str]
        if self._call_load_ipython_extension(mod):
            self.loaded.add(module_str)
        else:
            return "no load function"
Exemplo n.º 22
0
def load_extension(self, module_str):
    """Load an 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 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):
                __import__(module_str)
        mod = sys.modules[module_str]
        if self._call_load_ipython_extension(mod):
            self.loaded.add(module_str)
        else:
            return "no load function"
Exemplo n.º 23
0
    def _load_extension(self, module_str: str):
        if module_str in self.loaded:
            return "already loaded"

        from 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"
Exemplo n.º 24
0
def test_deepreload():
    "Test that dreload does deep reloads and skips excluded modules."
    with TemporaryDirectory() as tmpdir:
        with prepended_to_syspath(tmpdir):
            tmpdirpath = Path(tmpdir)
            with open(tmpdirpath / "A.py", "w") as f:
                f.write("class Object(object):\n    pass\n")
            with open(tmpdirpath / "B.py", "w") as f:
                f.write("import A\n")
            import A
            import B

            # Test that A is not reloaded.
            obj = A.Object()
            dreload(B, exclude=["A"])
            assert isinstance(obj, A.Object) is True

            # Test that A is reloaded.
            obj = A.Object()
            dreload(B)
            assert isinstance(obj, A.Object) is False
Exemplo n.º 25
0
    def reload_extension(self, module_str: str):
        """Reload an 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 IPython.utils.syspathcontext import prepended_to_syspath

        if BUILTINS_EXTS.get(module_str, False) is True:
            module_str = "IPython.extensions." + module_str

        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)