Esempio n. 1
0
def test_import_dotted_library(
    capsys: CaptureFixture,
    caplog: LogCaptureFixture,
) -> None:
    caplog.set_level(logging.INFO)
    original_module = sys.modules.pop("xml.etree.ElementTree")
    expected_out = "INFO (TEST): Welcome to cElementTree!"
    expected_err = "WARNING (TEST): Monkey-patched version of cElementTree"

    def function_with_stdout_and_stderr(expected_out, expected_err):
        def mocked_function(*args, **kwargs):
            print(f"{expected_out} args={args} kwargs={kwargs}")
            print(expected_err, file=sys.stderr)

        return mocked_function

    try:
        with unittest.mock.patch(
                "importlib.import_module",
                side_effect=function_with_stdout_and_stderr(
                    expected_out, expected_err),
        ):
            modutils.load_module_from_name("xml.etree.ElementTree")

        out, err = capsys.readouterr()
        assert expected_out in caplog.text
        assert expected_err in caplog.text
        assert not out
        assert not err
    finally:
        sys.modules["xml.etree.ElementTree"] = original_module
Esempio n. 2
0
    def ast_from_module_name(self, modname, context_file=None):
        """given a module name, return the astroid object"""
        if modname in self.astroid_cache:
            return self.astroid_cache[modname]
        if modname == '__main__':
            return self._build_stub_module(modname)
        old_cwd = os.getcwd()
        if context_file:
            os.chdir(os.path.dirname(context_file))
        try:
            found_spec = self.file_from_module_name(modname, context_file)
            # pylint: disable=no-member
            if found_spec.type == spec.ModuleType.PY_ZIPMODULE:
                # pylint: disable=no-member
                module = self.zip_import_data(found_spec.location)
                if module is not None:
                    return module

            elif found_spec.type in (spec.ModuleType.C_BUILTIN,
                                     spec.ModuleType.C_EXTENSION):
                # pylint: disable=no-member
                if (found_spec.type == spec.ModuleType.C_EXTENSION
                        and not self._can_load_extension(modname)):
                    return self._build_stub_module(modname)
                try:
                    module = modutils.load_module_from_name(modname)
                except Exception as ex: # pylint: disable=broad-except
                    util.reraise(exceptions.AstroidImportError(
                        'Loading {modname} failed with:\n{error}',
                        modname=modname, path=found_spec.location, error=ex))
                return self.ast_from_module(module, modname)

            elif found_spec.type == spec.ModuleType.PY_COMPILED:
                raise exceptions.AstroidImportError(
                    "Unable to load compiled module {modname}.",
                    # pylint: disable=no-member
                    modname=modname, path=found_spec.location)

            elif found_spec.type == spec.ModuleType.PY_NAMESPACE:
                return self._build_namespace_module(modname,
                                                    # pylint: disable=no-member
                                                    found_spec.submodule_search_locations)

            # pylint: disable=no-member
            if found_spec.location is None:
                raise exceptions.AstroidImportError(
                    "Can't find a file for module {modname}.",
                    modname=modname)

            # pylint: disable=no-member
            return self.ast_from_file(found_spec.location, modname, fallback=False)
        except exceptions.AstroidBuildingError as e:
            for hook in self._failed_import_hooks:
                try:
                    return hook(modname)
                except exceptions.AstroidBuildingError:
                    pass
            raise e
        finally:
            os.chdir(old_cwd)
Esempio n. 3
0
 def _load_reporter_class(self):
     qname = self._reporter_name
     module = modutils.load_module_from_name(
         modutils.get_module_part(qname))
     class_name = qname.split(".")[-1]
     reporter_class = getattr(module, class_name)
     return reporter_class
Esempio n. 4
0
    def ast_from_module_name(self, modname, context_file=None):
        """given a module name, return the astroid object"""
        if modname in self.astroid_cache:
            return self.astroid_cache[modname]
        if modname == "__main__":
            return self._build_stub_module(modname)
        old_cwd = os.getcwd()
        if context_file:
            os.chdir(os.path.dirname(context_file))
        try:
            found_spec = self.file_from_module_name(modname, context_file)
            if found_spec.type == spec.ModuleType.PY_ZIPMODULE:
                module = self.zip_import_data(found_spec.location)
                if module is not None:
                    return module

            elif found_spec.type in (
                    spec.ModuleType.C_BUILTIN,
                    spec.ModuleType.C_EXTENSION,
            ):
                if (found_spec.type == spec.ModuleType.C_EXTENSION
                        and not self._can_load_extension(modname)):
                    return self._build_stub_module(modname)
                try:
                    module = modutils.load_module_from_name(modname)
                except Exception as ex:
                    raise exceptions.AstroidImportError(
                        "Loading {modname} failed with:\n{error}",
                        modname=modname,
                        path=found_spec.location,
                    ) from ex
                return self.ast_from_module(module, modname)

            elif found_spec.type == spec.ModuleType.PY_COMPILED:
                raise exceptions.AstroidImportError(
                    "Unable to load compiled module {modname}.",
                    modname=modname,
                    path=found_spec.location,
                )

            elif found_spec.type == spec.ModuleType.PY_NAMESPACE:
                return self._build_namespace_module(
                    modname, found_spec.submodule_search_locations)

            if found_spec.location is None:
                raise exceptions.AstroidImportError(
                    "Can't find a file for module {modname}.", modname=modname)

            return self.ast_from_file(found_spec.location,
                                      modname,
                                      fallback=False)
        except exceptions.AstroidBuildingError as e:
            for hook in self._failed_import_hooks:
                try:
                    return hook(modname)
                except exceptions.AstroidBuildingError:
                    pass
            raise e
        finally:
            os.chdir(old_cwd)
Esempio n. 5
0
 def ast_from_module_name(self, modname, context_file=None):
     """given a module name, return the astroid object"""
     if modname in self.astroid_cache:
         return self.astroid_cache[modname]
     if modname == '__main__':
         from astroid.builder import AstroidBuilder
         return AstroidBuilder(self).string_build('', modname)
     old_cwd = os.getcwd()
     if context_file:
         os.chdir(dirname(context_file))
     try:
         filepath = self.file_from_module_name(modname, context_file)
         if filepath is not None and not is_python_source(filepath):
             module = self.zip_import_data(filepath)
             if module is not None:
                 return module
         if filepath is None or not is_python_source(filepath):
             try:
                 module = load_module_from_name(modname)
             except Exception as ex:
                 msg = 'Unable to load module %s (%s)' % (modname, ex)
                 raise AstroidBuildingException(msg)
             return self.ast_from_module(module, modname)
         return self.ast_from_file(filepath, modname, fallback=False)
     finally:
         os.chdir(old_cwd)
Esempio n. 6
0
 def _load_reporter(self):
     name = self._reporter_name.lower()
     if name in self._reporters:
         self.set_reporter(self._reporters[name]())
     else:
         qname = self._reporter_name
         module = load_module_from_name(get_module_part(qname))
         class_name = qname.split('.')[-1]
         reporter_class = getattr(module, class_name)
         self.set_reporter(reporter_class())
Esempio n. 7
0
 def load_plugin_modules(self, modnames):
     """take a list of module names which are pylint plugins and load
     and register them
     """
     for modname in modnames:
         if modname in self._dynamic_plugins:
             continue
         self._dynamic_plugins.add(modname)
         module = load_module_from_name(modname)
         module.register(self)
Esempio n. 8
0
 def load_plugin_modules(self, modnames):
     """take a list of module names which are pylint plugins and load
     and register them
     """
     for modname in modnames:
         if modname in self._dynamic_plugins:
             continue
         self._dynamic_plugins.add(modname)
         module = load_module_from_name(modname)
         module.register(self)
Esempio n. 9
0
 def _load_reporter(self):
     name = self._reporter_name.lower()
     if name in self._reporters:
         self.set_reporter(self._reporters[name]())
     else:
         qname = self._reporter_name
         module = load_module_from_name(get_module_part(qname))
         class_name = qname.split('.')[-1]
         reporter_class = getattr(module, class_name)
         self.set_reporter(reporter_class())
Esempio n. 10
0
    def load_plugin_configuration(self):
        """Call the configuration hook for plugins

        This walks through the list of plugins, grabs the "load_configuration"
        hook, if exposed, and calls it to allow plugins to configure specific
        settings.
        """
        for modname in self._dynamic_plugins:
            module = modutils.load_module_from_name(modname)
            if hasattr(module, "load_configuration"):
                module.load_configuration(self)
Esempio n. 11
0
 def ast_from_module_name(self, modname, context_file=None):
     """given a module name, return the astroid object"""
     if modname in self.astroid_cache:
         return self.astroid_cache[modname]
     if modname == '__main__':
         return self._build_stub_module(modname)
     old_cwd = os.getcwd()
     if context_file:
         os.chdir(os.path.dirname(context_file))
     try:
         filepath, mp_type = self.file_from_module_name(
             modname, context_file)
         if mp_type == modutils.PY_ZIPMODULE:
             module = self.zip_import_data(filepath)
             if module is not None:
                 return module
         elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION):
             if mp_type == imp.C_EXTENSION and not self._can_load_extension(
                     modname):
                 return self._build_stub_module(modname)
             try:
                 module = modutils.load_module_from_name(modname)
             except Exception as ex:  # pylint: disable=broad-except
                 util.reraise(
                     exceptions.AstroidImportError(
                         'Loading {modname} failed with:\n{error}',
                         modname=modname,
                         path=filepath,
                         error=ex))
             return self.ast_from_module(module, modname)
         elif mp_type == imp.PY_COMPILED:
             raise exceptions.AstroidImportError(
                 "Unable to load compiled module {modname}.",
                 modname=modname,
                 path=filepath)
         if filepath is None:
             raise exceptions.AstroidImportError(
                 "Can't find a file for module {modname}.", modname=modname)
         return self.ast_from_file(filepath, modname, fallback=False)
     except exceptions.AstroidBuildingError as e:
         for hook in self._failed_import_hooks:
             try:
                 return hook(modname)
             except exceptions.AstroidBuildingError:
                 pass
         raise e
     finally:
         os.chdir(old_cwd)
Esempio n. 12
0
 def ast_from_module_name(self, modname, context_file=None):
     """given a module name, return the astroid object"""
     if modname in self.astroid_cache:
         return self.astroid_cache[modname]
     if modname == '__main__':
         return self._build_stub_module(modname)
     old_cwd = os.getcwd()
     if context_file:
         os.chdir(os.path.dirname(context_file))
     try:
         filepath, mp_type = self.file_from_module_name(modname, context_file)
         if mp_type == modutils.PY_ZIPMODULE:
             module = self.zip_import_data(filepath)
             if module is not None:
                 return module
         elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION):
             if mp_type == imp.C_EXTENSION and not self._can_load_extension(modname):
                 return self._build_stub_module(modname)
             try:
                 module = modutils.load_module_from_name(modname)
             except Exception as ex: # pylint: disable=broad-except
                 util.reraise(exceptions.AstroidImportError(
                     'Loading {modname} failed with:\n{error}',
                     modname=modname, path=filepath, error=ex))
             return self.ast_from_module(module, modname)
         elif mp_type == imp.PY_COMPILED:
             raise exceptions.AstroidImportError(
                 "Unable to load compiled module {modname}.",
                 modname=modname, path=filepath)
         if filepath is None:
             raise exceptions.AstroidImportError(
                 "Can't find a file for module {modname}.",
                 modname=modname)
         return self.ast_from_file(filepath, modname, fallback=False)
     except exceptions.AstroidBuildingError as e:
         for hook in self._failed_import_hooks:
             try:
                 return hook(modname)
             except exceptions.AstroidBuildingError:
                 pass
         raise e
     finally:
         os.chdir(old_cwd)
Esempio n. 13
0
 def ast_from_module_name(self, modname, context_file=None):
     """given a module name, return the astroid object"""
     if modname in self.astroid_cache:
         return self.astroid_cache[modname]
     if modname == '__main__':
         return self._build_stub_module(modname)
     old_cwd = os.getcwd()
     if context_file:
         os.chdir(dirname(context_file))
     try:
         filepath, mp_type = self.file_from_module_name(
             modname, context_file)
         if mp_type == modutils.PY_ZIPMODULE:
             module = self.zip_import_data(filepath)
             if module is not None:
                 return module
         elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION):
             if mp_type == imp.C_EXTENSION and not self._can_load_extension(
                     modname):
                 return self._build_stub_module(modname)
             try:
                 module = modutils.load_module_from_name(modname)
             except Exception as ex:
                 msg = 'Unable to load module %s (%s)' % (modname, ex)
                 raise AstroidBuildingException(msg)
             return self.ast_from_module(module, modname)
         elif mp_type == imp.PY_COMPILED:
             raise AstroidBuildingException(
                 "Unable to load compiled module %s" % (modname, ))
         if filepath is None:
             raise AstroidBuildingException("Unable to load module %s" %
                                            (modname, ))
         return self.ast_from_file(filepath, modname, fallback=False)
     except AstroidBuildingException as e:
         for hook in self._failed_import_hooks:
             try:
                 return hook(modname)
             except AstroidBuildingException:
                 pass
         raise e
     finally:
         os.chdir(old_cwd)
Esempio n. 14
0
 def ast_from_module_name(self, modname, context_file=None):
     """given a module name, return the astroid object"""
     if modname in self.astroid_cache:
         return self.astroid_cache[modname]
     if modname == '__main__':
         return self._build_stub_module(modname)
     old_cwd = os.getcwd()
     if context_file:
         os.chdir(os.path.dirname(context_file))
     try:
         filepath, mp_type = self.file_from_module_name(modname, context_file)
         if mp_type == modutils.PY_ZIPMODULE:
             module = self.zip_import_data(filepath)
             if module is not None:
                 return module
         elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION):
             if mp_type == imp.C_EXTENSION and not self._can_load_extension(modname):
                 return self._build_stub_module(modname)
             try:
                 module = modutils.load_module_from_name(modname)
             except Exception as ex:
                 msg = 'Unable to load module %s (%s)' % (modname, ex)
                 raise exceptions.AstroidBuildingException(msg)
             return self.ast_from_module(module, modname)
         elif mp_type == imp.PY_COMPILED:
             msg = "Unable to load compiled module %s" % (modname,)
             raise exceptions.AstroidBuildingException(msg)
         if filepath is None:
             msg = "Unable to load module %s" % (modname,)
             raise exceptions.AstroidBuildingException(msg)
         return self.ast_from_file(filepath, modname, fallback=False)
     except exceptions.AstroidBuildingException as e:
         for hook in self._failed_import_hooks:
             try:
                 return hook(modname)
             except exceptions.AstroidBuildingException:
                 pass
         raise e
     finally:
         os.chdir(old_cwd)
Esempio n. 15
0
 def ast_from_module_name(self, modname, context_file=None):
     """given a module name, return the astroid object"""
     if modname in self.astroid_cache:
         return self.astroid_cache[modname]
     if modname == '__main__':
         from astroid.builder import AstroidBuilder
         return AstroidBuilder(self).string_build('', modname)
     old_cwd = os.getcwd()
     if context_file:
         os.chdir(dirname(context_file))
     try:
         filepath = self.file_from_module_name(modname, context_file)
         if filepath is not None and not is_python_source(filepath):
             module = self.zip_import_data(filepath)
             if module is not None:
                 return module
         if filepath is None or not is_python_source(filepath):
             try:
                 module = load_module_from_name(modname)
             except Exception, ex:
                 msg = 'Unable to load module %s (%s)' % (modname, ex)
                 raise AstroidBuildingException(msg)
             return self.ast_from_module(module, modname)
         return self.ast_from_file(filepath, modname, fallback=False)
 def test_known_values_load_module_from_name_1(self) -> None:
     self.assertEqual(modutils.load_module_from_name("sys"), sys)
 def test_knownValues_load_module_from_name_2(self):
     self.assertEqual(modutils.load_module_from_name('os.path'), os.path)
 def test_knownValues_load_module_from_name_1(self):
     self.assertEqual(modutils.load_module_from_name('sys'), sys)
Esempio n. 19
0
    def ast_from_module_name(self, modname, context_file=None):
        """given a module name, return the astroid object"""
        if modname in self.astroid_cache:
            return self.astroid_cache[modname]
        if modname == '__main__':
            return self._build_stub_module(modname)
        old_cwd = os.getcwd()
        if context_file:
            os.chdir(os.path.dirname(context_file))
        try:
            found_spec = self.file_from_module_name(modname, context_file)
            # pylint: disable=no-member
            if found_spec.type == spec.ModuleType.PY_ZIPMODULE:
                # pylint: disable=no-member
                module = self.zip_import_data(found_spec.location)
                if module is not None:
                    return module

            elif found_spec.type in (spec.ModuleType.C_BUILTIN,
                                     spec.ModuleType.C_EXTENSION):
                # pylint: disable=no-member
                if (found_spec.type == spec.ModuleType.C_EXTENSION
                        and not self._can_load_extension(modname)):
                    return self._build_stub_module(modname)
                try:
                    module = modutils.load_module_from_name(modname)
                except Exception as ex:  # pylint: disable=broad-except
                    util.reraise(
                        exceptions.AstroidImportError(
                            'Loading {modname} failed with:\n{error}',
                            modname=modname,
                            path=spec.location,
                            error=ex))
                return self.ast_from_module(module, modname)

            elif found_spec.type == spec.ModuleType.PY_COMPILED:
                raise exceptions.AstroidImportError(
                    "Unable to load compiled module {modname}.",
                    # pylint: disable=no-member
                    modname=modname,
                    path=found_spec.location)

            elif found_spec.type == spec.ModuleType.PY_NAMESPACE:
                return self._build_namespace_module(
                    modname,
                    # pylint: disable=no-member
                    found_spec.submodule_search_locations)

            # pylint: disable=no-member
            if found_spec.location is None:
                raise exceptions.AstroidImportError(
                    "Can't find a file for module {modname}.", modname=modname)

            # pylint: disable=no-member
            return self.ast_from_file(found_spec.location,
                                      modname,
                                      fallback=False)
        except exceptions.AstroidBuildingError as e:
            for hook in self._failed_import_hooks:
                try:
                    return hook(modname)
                except exceptions.AstroidBuildingError:
                    pass
            # pylint: disable=raising-bad-type; https://github.com/PyCQA/pylint/issues/157
            raise e
        finally:
            os.chdir(old_cwd)
Esempio n. 20
0
 def test_knownValues_load_module_from_name_2(self):
     self.assertEqual(modutils.load_module_from_name('os.path'), os.path)
Esempio n. 21
0
 def test_knownValues_load_module_from_name_1(self):
     self.assertEqual(modutils.load_module_from_name('sys'), sys)
 def test_known_values_load_module_from_name_2(self) -> None:
     self.assertEqual(modutils.load_module_from_name("os.path"), os.path)
Esempio n. 23
0
    def ast_from_module_name(
        self,
        modname: str | None,
        context_file: str | None = None,
        use_cache: bool = True,
    ) -> nodes.Module:
        """Given a module name, return the astroid object."""
        # Sometimes we don't want to use the cache. For example, when we're
        # importing a module with the same name as the file that is importing
        # we want to fallback on the import system to make sure we get the correct
        # module.
        if modname in self.astroid_cache and use_cache:
            return self.astroid_cache[modname]
        if modname == "__main__":
            return self._build_stub_module(modname)
        if context_file:
            old_cwd = os.getcwd()
            os.chdir(os.path.dirname(context_file))
        try:
            found_spec = self.file_from_module_name(modname, context_file)
            if found_spec.type == spec.ModuleType.PY_ZIPMODULE:
                module = self.zip_import_data(found_spec.location)
                if module is not None:
                    return module

            elif found_spec.type in (
                    spec.ModuleType.C_BUILTIN,
                    spec.ModuleType.C_EXTENSION,
            ):
                if (found_spec.type == spec.ModuleType.C_EXTENSION
                        and not self._can_load_extension(modname)):
                    return self._build_stub_module(modname)
                try:
                    module = load_module_from_name(modname)
                except Exception as e:
                    raise AstroidImportError(
                        "Loading {modname} failed with:\n{error}",
                        modname=modname,
                        path=found_spec.location,
                    ) from e
                return self.ast_from_module(module, modname)

            elif found_spec.type == spec.ModuleType.PY_COMPILED:
                raise AstroidImportError(
                    "Unable to load compiled module {modname}.",
                    modname=modname,
                    path=found_spec.location,
                )

            elif found_spec.type == spec.ModuleType.PY_NAMESPACE:
                return self._build_namespace_module(
                    modname, found_spec.submodule_search_locations)
            elif found_spec.type == spec.ModuleType.PY_FROZEN:
                if found_spec.location is None:
                    return self._build_stub_module(modname)
                # For stdlib frozen modules we can determine the location and
                # can therefore create a module from the source file
                return self.ast_from_file(found_spec.location,
                                          modname,
                                          fallback=False)

            if found_spec.location is None:
                raise AstroidImportError(
                    "Can't find a file for module {modname}.", modname=modname)

            return self.ast_from_file(found_spec.location,
                                      modname,
                                      fallback=False)
        except AstroidBuildingError as e:
            for hook in self._failed_import_hooks:
                try:
                    return hook(modname)
                except AstroidBuildingError:
                    pass
            raise e
        finally:
            if context_file:
                os.chdir(old_cwd)