示例#1
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)
示例#2
0
 def ast_from_module(self, module, modname=None):
     """given an imported module, return the astroid object"""
     modname = modname or module.__name__
     if modname in self.astroid_cache:
         return self.astroid_cache[modname]
     try:
         # some builtin modules don't have __file__ attribute
         filepath = module.__file__
         if is_python_source(filepath):
             return self.ast_from_file(filepath, modname)
     except AttributeError:
         pass
     from astroid.builder import AstroidBuilder
     return AstroidBuilder(self).module_build(module, modname)
示例#3
0
 def ast_from_module(self, module, modname=None):
     """given an imported module, return the astroid object"""
     modname = modname or module.__name__
     if modname in self.astroid_cache:
         return self.astroid_cache[modname]
     try:
         # some builtin modules don't have __file__ attribute
         filepath = module.__file__
         if modutils.is_python_source(filepath):
             return self.ast_from_file(filepath, modname)
     except AttributeError:
         pass
     from astroid.builder import AstroidBuilder
     return AstroidBuilder(self).module_build(module, modname)
示例#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__':
         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)
示例#5
0
    def ast_from_module(self, module: types.ModuleType, modname: str = None):
        """given an imported module, return the astroid object"""
        modname = modname or module.__name__
        if modname in self.astroid_cache:
            return self.astroid_cache[modname]
        try:
            # some builtin modules don't have __file__ attribute
            filepath = module.__file__
            if is_python_source(filepath):
                return self.ast_from_file(filepath, modname)
        except AttributeError:
            pass

        # pylint: disable=import-outside-toplevel; circular import
        from astroid.builder import AstroidBuilder

        return AstroidBuilder(self).module_build(module, modname)