From fc6fa7855508c3480e89ded4b0a72d6f8f59860d Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sun, 22 Feb 2009 03:19:50 +0000 Subject: [PATCH] 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 --- pym/portage/proxy/lazyimport.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) 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): -- 2.26.2