diff options
author | Zac Medico <zmedico@gentoo.org> | 2009-02-22 00:33:41 +0000 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2009-02-22 00:33:41 +0000 |
commit | 6c33a6b5314bf45de7106362b746fa86cc05a98a (patch) | |
tree | c74bf267b996a9fb8786e2623bde0e78bbbdc1d5 | |
parent | 8ad7d9d8941a53b9cb5bdb4b835dded508c7c1ce (diff) | |
download | portage-6c33a6b5314bf45de7106362b746fa86cc05a98a.tar.gz portage-6c33a6b5314bf45de7106362b746fa86cc05a98a.tar.bz2 portage-6c33a6b5314bf45de7106362b746fa86cc05a98a.zip |
Make lazy_import() skip proxy creation for modules that already exist
in sys.modules.
svn path=/main/trunk/; revision=12675
-rw-r--r-- | pym/portage/util.py | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/pym/portage/util.py b/pym/portage/util.py index d18faff2a..14481a111 100644 --- a/pym/portage/util.py +++ b/pym/portage/util.py @@ -451,6 +451,8 @@ def lazy_import(scope, *args): @type args: strings """ + modules = sys.modules + for s in args: parts = s.split(':', 1) if len(parts) == 1: @@ -463,24 +465,27 @@ def lazy_import(scope, *args): parent_scope = scope for i in xrange(len(components)): alias = components[i] - mod = parent_scope.get(alias) - if isinstance(mod, types.ModuleType): - parent_scope = mod.__dict__ - continue if i < len(components) - 1: parent_name = ".".join(components[:i+1]) __import__(parent_name) - mod = sys.modules.get(parent_name) + mod = modules.get(parent_name) if not isinstance(mod, types.ModuleType): # raise an exception __import__(name) parent_scope[alias] = mod parent_scope = mod.__dict__ continue - parent_scope[alias] = _LazyImport(parent_scope, alias, name) + + already_imported = modules.get(name) + if already_imported is not None: + parent_scope[alias] = already_imported + else: + parent_scope[alias] = \ + _LazyImport(parent_scope, alias, name) else: name, fromlist = parts + already_imported = modules.get(name) fromlist = fromlist.split(',') for s in fromlist: alias = s.split('@', 1) @@ -489,8 +494,14 @@ def lazy_import(scope, *args): orig = alias else: orig, alias = alias - scope[alias] = _LazyImportFrom(scope, alias, - name + '.' + orig) + if already_imported is not None: + try: + scope[alias] = getattr(already_imported, orig) + except AttributeError: + raise ImportError('cannot import name %s' % orig) + else: + scope[alias] = _LazyImportFrom(scope, alias, + name + '.' + orig) class _tolerant_shlex(shlex.shlex): def sourcehook(self, newfile): |