From 02a3ed493ab428317294bec4f5d8ba759aacfaf9 Mon Sep 17 00:00:00 2001 From: Fabian Groffen Date: Sun, 8 Mar 2009 21:06:09 +0000 Subject: [PATCH] Merged from trunk -r12771:12779 | 12772 | Fix LazyItemsDict.update() to create new _SingletonWrapper | | zmedico | instances that reference self. | | 12773 | Simplify LazyItemsDict internals. | | zmedico | | | 12774 | Allow unicode type values in config.__setitem__(). Thanks to | | zmedico | Arfrever Frehtes Taifersar Arahesis for this patch. | | 12775 | Always use basestring instead of str with isinstance(). | | zmedico | | | 12776 | Fix __getitem__ to convert mtime to long, and remove | | zmedico | unreachable code from _parse_data(). | | 12777 | Fix an issue with incomplete deepcopy of _LazyItem instances | | zmedico | inside LazyItemsDict.__deepcopy__(). Thanks to Arfrever for | | | reporting. | | 12778 | Remove unnecessary list() from previous commit. | | zmedico | | | 12779 | Use slice instead of startwith in stack_* incremental | | zmedico | processing. | svn path=/main/branches/prefix/; revision=12787 --- pym/_emerge/__init__.py | 2 +- pym/portage/__init__.py | 2 +- pym/portage/cache/flat_hash.py | 18 +++++----- pym/portage/cache/template.py | 2 +- pym/portage/mail.py | 2 +- pym/portage/process.py | 2 +- pym/portage/util.py | 63 +++++++++++++++++----------------- 7 files changed, 47 insertions(+), 44 deletions(-) diff --git a/pym/_emerge/__init__.py b/pym/_emerge/__init__.py index b8d31a09e..58dc1dba2 100644 --- a/pym/_emerge/__init__.py +++ b/pym/_emerge/__init__.py @@ -9769,7 +9769,7 @@ class JobStatusDisplay(object): object.__setattr__(self, "_term_codes", term_codes) encoding = sys.getdefaultencoding() for k, v in self._term_codes.items(): - if not isinstance(v, str): + if not isinstance(v, basestring): self._term_codes[k] = v.decode(encoding, 'replace') def _init_term(self): diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py index 4ac8bffc2..4ed25e280 100644 --- a/pym/portage/__init__.py +++ b/pym/portage/__init__.py @@ -2991,7 +2991,7 @@ class config(object): def __setitem__(self,mykey,myvalue): "set a value; will be thrown away at reset() time" - if not isinstance(myvalue, str): + if not isinstance(myvalue, basestring): raise ValueError("Invalid type being used as a value: '%s': '%s'" % (str(mykey),str(myvalue))) self.modifying() self.modifiedkeys += [mykey] diff --git a/pym/portage/cache/flat_hash.py b/pym/portage/cache/flat_hash.py index dda380e1c..f6bf6d078 100644 --- a/pym/portage/cache/flat_hash.py +++ b/pym/portage/cache/flat_hash.py @@ -33,6 +33,16 @@ class database(fs_template.FsBased): """Backward compatibility with old cache that uses mtime mangling.""" d["_mtime_"] = long(os.fstat(myf.fileno()).st_mtime) + mtime = d.get('_mtime_') + if mtime is None: + raise cache_errors.CacheCorruption(cpv, + '_mtime_ field is missing') + try: + mtime = long(mtime) + except ValueError: + raise cache_errors.CacheCorruption(cpv, + '_mtime_ conversion to long failed: %s' % (mtime,)) + d['_mtime_'] = mtime return d finally: myf.close() @@ -52,14 +62,6 @@ class database(fs_template.FsBased): else: d["_eclasses_"] = {} return d - - for x in self._known_keys: - if x not in d: - d[x] = '' - - - return d - def _setitem(self, cpv, values): # import pdb;pdb.set_trace() diff --git a/pym/portage/cache/template.py b/pym/portage/cache/template.py index 7fce8ef17..e78216338 100644 --- a/pym/portage/cache/template.py +++ b/pym/portage/cache/template.py @@ -169,7 +169,7 @@ class database(object): for key,match in match_dict.iteritems(): # XXX this sucks. try: - if isinstance(match, str): + if isinstance(match, basestring): restricts[key] = re.compile(match).match else: restricts[key] = re.compile(match[0],match[1]).match diff --git a/pym/portage/mail.py b/pym/portage/mail.py index cf6ebd134..72b411264 100644 --- a/pym/portage/mail.py +++ b/pym/portage/mail.py @@ -17,7 +17,7 @@ def create_message(sender, recipient, subject, body, attachments=None): for x in attachments: if isinstance(x, BaseMessage): mymessage.attach(x) - elif isinstance(x, str): + elif isinstance(x, basestring): mymessage.attach(TextMessage(x)) else: raise portage.exception.PortageException("Can't handle type of attachment: %s" % type(x)) diff --git a/pym/portage/process.py b/pym/portage/process.py index 7c0fb342f..6f449c3d6 100644 --- a/pym/portage/process.py +++ b/pym/portage/process.py @@ -175,7 +175,7 @@ def spawn(mycommand, env={}, opt_name=None, fd_pipes=None, returnpid=False, """ # mycommand is either a str or a list - if isinstance(mycommand, str): + if isinstance(mycommand, basestring): mycommand = mycommand.split() # If an absolute path to an executable file isn't given diff --git a/pym/portage/util.py b/pym/portage/util.py index 9461512e2..2d965355e 100644 --- a/pym/portage/util.py +++ b/pym/portage/util.py @@ -178,7 +178,7 @@ def stack_dictlist(original_dicts, incremental=0, incrementals=[], ignore_none=0 if thing == "-*": final_dict[y] = [] continue - elif thing.startswith("-"): + elif thing[:1] == '-': try: final_dict[y].remove(thing[1:]) except ValueError: @@ -224,7 +224,7 @@ def stack_lists(lists, incremental=1): if incremental: if y == "-*": new_list.clear() - elif y.startswith("-"): + elif y[:1] == '-': new_list.pop(y[1:], None) else: new_list[y] = True @@ -1072,19 +1072,18 @@ class LazyItemsDict(dict): def addLazyItem(self, item_key, value_callable, *pargs, **kwargs): """Add a lazy item for the given key. When the item is requested, value_callable will be called with *pargs and **kwargs arguments.""" - if not pargs: - pargs = None - if not kwargs: - kwargs = None - self.lazy_items[item_key] = (value_callable, pargs, kwargs) + self.lazy_items[item_key] = \ + self._LazyItem(value_callable, pargs, kwargs, False) # make it show up in self.keys(), etc... dict.__setitem__(self, item_key, None) + 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.""" - self.addLazyItem(item_key, - self._SingletonWrapper(self, item_key, value_callable, - *pargs, **kwargs)) + self.lazy_items[item_key] = \ + self._LazyItem(value_callable, pargs, kwargs, True) + # make it show up in self.keys(), etc... + dict.__setitem__(self, item_key, None) def update(self, *args, **kwargs): if len(args) > 1: @@ -1111,14 +1110,21 @@ class LazyItemsDict(dict): def __getitem__(self, item_key): if item_key in self.lazy_items: - value_callable, pargs, kwargs = self.lazy_items[item_key] + lazy_item = self.lazy_items[item_key] + pargs = lazy_item.pargs if pargs is None: pargs = () + kwargs = lazy_item.kwargs if kwargs is None: kwargs = {} - return value_callable(*pargs, **kwargs) + result = lazy_item.func(*pargs, **kwargs) + if lazy_item.singleton: + self[item_key] = result + return result + else: return dict.__getitem__(self, item_key) + def __setitem__(self, item_key, value): if item_key in self.lazy_items: del self.lazy_items[item_key] @@ -1167,35 +1173,30 @@ class LazyItemsDict(dict): k_copy = deepcopy(k, memo) if k in self.lazy_items: lazy_item = self.lazy_items[k] - try: - result.lazy_items[k_copy] = deepcopy(lazy_item, memo) - except TypeError: - # If deepcopy fails for a lazy singleton, try to - # evaluate the singleton and deepcopy the result. - if not isinstance(lazy_item[0], self._SingletonWrapper): - raise + if lazy_item.singleton: dict.__setitem__(result, k_copy, deepcopy(self[k], memo)) else: + result.lazy_items[k_copy] = deepcopy(lazy_item, memo) dict.__setitem__(result, k_copy, None) else: dict.__setitem__(result, k_copy, deepcopy(self[k], memo)) return result - class _SingletonWrapper(object): + class _LazyItem(object): + + __slots__ = ('func', 'pargs', 'kwargs', 'singleton') - __slots__ = ('_parent', '_key', '_callable', '_pargs', '_kwargs') + def __init__(self, func, pargs, kwargs, singleton): - 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 + if not pargs: + pargs = None + if not kwargs: + kwargs = None - def __call__(self): - value = self._callable(*self._pargs, **self._kwargs) - self._parent[self._key] = value - return value + self.func = func + self.pargs = pargs + self.kwargs = kwargs + self.singleton = singleton class ConfigProtect(object): def __init__(self, myroot, protect_list, mask_list): -- 2.26.2