summaryrefslogtreecommitdiffstats
path: root/pym/portage/proxy
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2009-02-22 03:19:50 +0000
committerZac Medico <zmedico@gentoo.org>2009-02-22 03:19:50 +0000
commitfc6fa7855508c3480e89ded4b0a72d6f8f59860d (patch)
tree147e590b3ed7a8ebcae06ee22fe86718b2bff52a /pym/portage/proxy
parent73fb1cc811670e31fa189997b1e0735b1d264264 (diff)
downloadportage-fc6fa7855508c3480e89ded4b0a72d6f8f59860d.tar.gz
portage-fc6fa7855508c3480e89ded4b0a72d6f8f59860d.tar.bz2
portage-fc6fa7855508c3480e89ded4b0a72d6f8f59860d.zip
Make lazyimport proxies trigger automatic destruction of other proxies for
modules that have been imported. This way, destruction of a single proxy can trigger destruction of all the rest. svn path=/main/trunk/; revision=12678
Diffstat (limited to 'pym/portage/proxy')
-rw-r--r--pym/portage/proxy/lazyimport.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/pym/portage/proxy/lazyimport.py b/pym/portage/proxy/lazyimport.py
index c9eecf0c5..cf0db77ce 100644
--- a/pym/portage/proxy/lazyimport.py
+++ b/pym/portage/proxy/lazyimport.py
@@ -8,6 +8,36 @@ import sys
import types
from portage.proxy.objectproxy import ObjectProxy
+_module_proxies = {}
+
+def _register_module_proxy(name, proxy):
+ proxy_list = _module_proxies.get(name)
+ if proxy_list is None:
+ proxy_list = []
+ _module_proxies[name] = proxy_list
+ proxy_list.append(proxy)
+
+def _unregister_module_proxy(name):
+ """
+ Destroy all proxies that reference the give module name. Also, check
+ for other proxies referenced by modules that have been imported and
+ destroy those proxies too. This way, destruction of a single proxy
+ can trigger destruction of all the rest.
+ """
+ proxy_list = _module_proxies.get(name)
+ if proxy_list is not None:
+ del _module_proxies[name]
+ for proxy in proxy_list:
+ object.__getattribute__(proxy, '_get_target')()
+
+ modules = sys.modules
+ for name, proxy_list in list(_module_proxies.iteritems()):
+ if name not in modules:
+ continue
+ del _module_proxies[name]
+ for proxy in proxy_list:
+ object.__getattribute__(proxy, '_get_target')()
+
class _LazyImport(ObjectProxy):
__slots__ = ('_scope', '_alias', '_name', '_target')
@@ -17,6 +47,7 @@ class _LazyImport(ObjectProxy):
object.__setattr__(self, '_scope', scope)
object.__setattr__(self, '_alias', alias)
object.__setattr__(self, '_name', name)
+ _register_module_proxy(name, self)
def _get_target(self):
try:
@@ -29,6 +60,7 @@ class _LazyImport(ObjectProxy):
object.__setattr__(self, '_target', target)
object.__getattribute__(self, '_scope')[
object.__getattribute__(self, '_alias')] = target
+ _unregister_module_proxy(name)
return target
class _LazyImportFrom(_LazyImport):
@@ -48,6 +80,7 @@ class _LazyImportFrom(_LazyImport):
object.__setattr__(self, '_target', target)
object.__getattribute__(self, '_scope')[
object.__getattribute__(self, '_alias')] = target
+ _unregister_module_proxy(name)
return target
def lazyimport(scope, *args):