From f16b2c8b2c8098d0794e6d1695166c45857c39f6 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sun, 20 Sep 2009 18:54:57 +0000 Subject: [PATCH] Make _unicode_module_wrapper cache wrappers and reuse them. Thanks to Marat Radchenko for this patch from bug #276813. svn path=/main/trunk/; revision=14299 --- pym/portage/__init__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py index 8837398cd..4b68cefb2 100644 --- a/pym/portage/__init__.py +++ b/pym/portage/__init__.py @@ -210,14 +210,24 @@ class _unicode_module_wrapper(object): """ Wraps a module and wraps all functions with _unicode_func_wrapper. """ - __slots__ = ('_mod', '_encoding', '_overrides') + __slots__ = ('_mod', '_encoding', '_overrides', '_cache') - def __init__(self, mod, encoding=_encodings['fs'], overrides=None): + def __init__(self, mod, encoding=_encodings['fs'], overrides=None, cache=True): object.__setattr__(self, '_mod', mod) object.__setattr__(self, '_encoding', encoding) object.__setattr__(self, '_overrides', overrides) + if cache: + cache = {} + else: + cache = None + object.__setattr__(self, '_cache', cache) def __getattribute__(self, attr): + cache = object.__getattribute__(self, '_cache') + if cache is not None: + result = cache.get(attr) + if result is not None: + return result result = getattr(object.__getattribute__(self, '_mod'), attr) encoding = object.__getattribute__(self, '_encoding') overrides = object.__getattribute__(self, '_overrides') @@ -233,6 +243,8 @@ class _unicode_module_wrapper(object): encoding=encoding, overrides=overrides) elif hasattr(result, '__call__'): result = _unicode_func_wrapper(result, encoding=encoding) + if cache is not None: + cache[attr] = result return result import os as _os -- 2.26.2