In LazyItemsDict, when a singleton is instantiated, replace the wrapper with
authorZac Medico <zmedico@gentoo.org>
Wed, 11 Mar 2009 06:05:17 +0000 (06:05 -0000)
committerZac Medico <zmedico@gentoo.org>
Wed, 11 Mar 2009 06:05:17 +0000 (06:05 -0000)
the singleton since the wrapper is no longer needed at this point.
(trunk r12672)

svn path=/main/branches/2.1.6/; revision=12932

pym/portage/util.py

index 9a59445105768cf863c0365b559c0be5a8db9a5d..33a61a26bb02a72144dad324e187fec4426a8957 100644 (file)
@@ -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