示例#1
0
 def astng_from_module_name(self, modname, context_file=None):
     """given a module name, return the astng object"""
     if modname in self._cache:
         return self._cache[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):
             data, zmodname = zip_import_data(filepath)
             if data is not None:
                 from logilab.astng.builder import ASTNGBuilder
                 try:
                     return ASTNGBuilder(self).string_build(
                         data, zmodname, filepath)
                 except (SyntaxError, KeyboardInterrupt, SystemExit):
                     raise
         if filepath is None or not is_python_source(filepath):
             try:
                 module = load_module_from_name(modname)
             # catch SystemError as well, we may get that on badly
             # initialized C-module
             except (SystemError, ImportError), ex:
                 msg = 'Unable to load module %s (%s)' % (modname, ex)
                 raise ASTNGBuildingException(msg)
             return self.astng_from_module(module, modname)
         return self.astng_from_file(filepath, modname, fallback=False)
示例#2
0
 def astng_from_module_name(self, modname, context_file=None):
     """given a module name, return the astng object"""
     if modname in self.astng_cache:
         return self.astng_cache[modname]
     if modname == '__main__':
         from logilab.astng.builder import ASTNGBuilder
         return ASTNGBuilder(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 ASTNGBuildingException(msg)
             return self.astng_from_module(module, modname)
         return self.astng_from_file(filepath, modname, fallback=False)
     finally:
         os.chdir(old_cwd)
示例#3
0
 def astng_from_module_name(self, modname, context_file=None):
     """given a module name, return the astng object"""
     if modname in self._cache:
         return self._cache[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):
             data, zmodname = zip_import_data(filepath)
             if data is not None:
                 from logilab.astng.builder import ASTNGBuilder
                 try:
                     return ASTNGBuilder(self).string_build(data, zmodname,
                                                            filepath)
                 except (SyntaxError, KeyboardInterrupt, SystemExit):
                     raise
         if filepath is None or not is_python_source(filepath):
             try:
                 module = load_module_from_name(modname)
             # catch SystemError as well, we may get that on badly
             # initialized C-module
             except (SystemError, ImportError), ex:
                 msg = 'Unable to load module %s (%s)' % (modname, ex)
                 raise ASTNGBuildingException(msg)
             return self.astng_from_module(module, modname)
         return self.astng_from_file(filepath, modname, fallback=False)
示例#4
0
 def astng_from_module_name(self, modname, context_file=None):
     """given a module name, return the astng object"""
     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):
             try:
                 return self._cache[filepath]
             except KeyError:
                 data, zmodname = zip_import_data(filepath)
                 if data is not None:
                     from logilab.astng.builder import ASTNGBuilder
                     try:
                         astng = ASTNGBuilder(self).string_build(data, zmodname, filepath)
                     except (SyntaxError, KeyboardInterrupt, SystemExit):
                         raise
                     self._cache[filepath] = astng
                     return astng
         if filepath is None or not is_python_source(filepath):
             try:
                 module = load_module_from_name(modname)
             except ImportError, ex:
                 msg = 'Unable to load module %s (%s)' % (modname, ex)
                 raise ASTNGBuildingException(msg)
             return self.astng_from_module(module, modname)
         return self.astng_from_file(filepath, modname, fallback=False)
示例#5
0
 def astng_from_module(self, module, modname=None):
     """given an imported module, return the astng object"""
     modname = modname or module.__name__
     if modname in self._cache:
         return self._cache[modname]
     try:
         # some builtin modules don't have __file__ attribute
         filepath = module.__file__
         if is_python_source(filepath):
             return self.astng_from_file(filepath, modname)
     except AttributeError:
         pass
     from logilab.astng.builder import ASTNGBuilder
     return ASTNGBuilder(self).module_build(module, modname)
示例#6
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)
示例#7
0
 def astng_from_module(self, module, modname=None):
     """given an imported module, return the astng object"""
     modname = modname or module.__name__
     filepath = modname
     try:
         # some builtin modules don't have __file__ attribute
         filepath = module.__file__
         if is_python_source(filepath):
             return self.astng_from_file(filepath, modname)
     except AttributeError:
         pass
     try:
         return self._cache[filepath]
     except KeyError:
         from logilab.astng.builder import ASTNGBuilder
         astng = ASTNGBuilder(self).module_build(module, modname)
         # update caches (filepath and astng.file are not necessarily  the
         # same (.pyc pb))
         self._cache[filepath] = self._cache[astng.file] = astng
         return astng