diff options
| author | Emile Anclin <emile.anclin@logilab.fr> | 2010-10-27 11:06:02 +0200 |
|---|---|---|
| committer | Emile Anclin <emile.anclin@logilab.fr> | 2010-10-27 11:06:02 +0200 |
| commit | 3b2a642a2d7a438312f6d0bd30ce7a109bcac240 (patch) | |
| tree | 0f15fd5feeb3d94b4c585b0a8799e150bdd4c63c /manager.py | |
| parent | c7d1bb49f8268e6781289396bbb39def4c0a3134 (diff) | |
| download | astroid-git-3b2a642a2d7a438312f6d0bd30ce7a109bcac240.tar.gz | |
fix zipe-safe egg import: check if the imported module is a package
add a "zip_import_data" method to make "astng_from_module_name" more readable
Diffstat (limited to 'manager.py')
| -rw-r--r-- | manager.py | 47 |
1 files changed, 25 insertions, 22 deletions
@@ -67,20 +67,6 @@ def safe_repr(obj): except: return '???' -def zip_import_data(filepath): - if zipimport is None: - return None, None - for ext in ('.zip', '.egg'): - try: - eggpath, resource = filepath.split(ext + '/', 1) - except ValueError: - continue - try: - importer = zipimport.zipimporter(eggpath + ext) - return importer.get_source(resource), resource.replace('/', '.') - except: - continue - return None, None class ASTNGManager(OptionsProviderMixIn): @@ -161,14 +147,9 @@ class ASTNGManager(OptionsProviderMixIn): 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 + 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) @@ -182,6 +163,28 @@ class ASTNGManager(OptionsProviderMixIn): finally: os.chdir(old_cwd) + def zip_import_data(self, filepath): + if zipimport is None: + return None + from logilab.astng.builder import ASTNGBuilder + builder = ASTNGBuilder(self) + for ext in ('.zip', '.egg'): + try: + eggpath, resource = filepath.rsplit(ext + '/', 1) + except ValueError: + continue + try: + importer = zipimport.zipimporter(eggpath + ext) + zmodname = resource.replace('/', '.') + if importer.is_package(resource): + zmodname = zmodname + '.__init__' + module = builder.string_build(importer.get_source(resource), + zmodname, filepath) + return module + except: + continue + return None + def file_from_module_name(self, modname, contextfile): try: value = self._mod_file_cache[(modname, contextfile)] |
