From: Zac Medico Date: Sat, 21 Feb 2009 19:09:05 +0000 (-0000) Subject: In LazyItemsDict, when a singleton is instantiated, replace the wrapper with X-Git-Tag: v2.2_rc24~141 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=e708e87e1a5d4e1e2411241382020dbcf4753494;p=portage.git In LazyItemsDict, when a singleton is instantiated, replace the wrapper with the singleton since the wrapper is no longer needed at this point. svn path=/main/trunk/; revision=12672 --- diff --git a/pym/portage/util.py b/pym/portage/util.py index 9a5944510..33a61a26b 100644 --- a/pym/portage/util.py +++ b/pym/portage/util.py @@ -1199,6 +1199,9 @@ class LazyItemsDict(dict): """A mapping object that behaves like a standard dict except that it allows for lazy initialization of values via callable objects. Lazy items can be overwritten and deleted just as normal items.""" + + __slots__ = ('lazy_items',) + def __init__(self, initial_items=None): dict.__init__(self) self.lazy_items = {} @@ -1213,18 +1216,9 @@ class LazyItemsDict(dict): def addLazySingleton(self, item_key, value_callable, *pargs, **kwargs): """This is like addLazyItem except value_callable will only be called a maximum of 1 time and the result will be cached for future requests.""" - class SingletonItem(object): - def __init__(self, value_callable, *pargs, **kwargs): - self._callable = value_callable - self._pargs = pargs - self._kwargs = kwargs - self._called = False - def __call__(self): - if not self._called: - self._called = True - self._value = self._callable(*self._pargs, **self._kwargs) - return self._value - self.addLazyItem(item_key, SingletonItem(value_callable, *pargs, **kwargs)) + self.addLazyItem(item_key, + self._SingletonWrapper(self, item_key, value_callable, + *pargs, **kwargs)) def update(self, map_obj): if isinstance(map_obj, LazyItemsDict): for k in map_obj: @@ -1250,6 +1244,22 @@ class LazyItemsDict(dict): del self.lazy_items[item_key] dict.__delitem__(self, item_key) + class _SingletonWrapper(object): + + __slots__ = ('_parent', '_key', '_callable', '_pargs', '_kwargs') + + def __init__(self, parent, key, value_callable, *pargs, **kwargs): + self._parent = parent + self._key = key + self._callable = value_callable + self._pargs = pargs + self._kwargs = kwargs + + def __call__(self): + value = self._callable(*self._pargs, **self._kwargs) + self._parent[self._key] = value + return value + class ConfigProtect(object): def __init__(self, myroot, protect_list, mask_list): self.myroot = myroot