1# This Source Code Form is subject to the terms of the Mozilla Public 2# License, v. 2.0. If a copy of the MPL was not distributed with this file, 3# You can obtain one at http://mozilla.org/MPL/2.0/. 4 5# Imported from 6# https://searchfox.org/mozilla-central/rev/c3ebaf6de2d481c262c04bb9657eaf76bf47e2ac/python/mozbuild/mozbuild/util.py#923-949 7 8 9import functools 10 11 12class memoize(dict): 13 """A decorator to memoize the results of function calls depending 14 on its arguments. 15 Both functions and instance methods are handled, although in the 16 instance method case, the results are cache in the instance itself. 17 """ 18 19 def __init__(self, func): 20 self.func = func 21 functools.update_wrapper(self, func) 22 23 def __call__(self, *args): 24 if args not in self: 25 self[args] = self.func(*args) 26 return self[args] 27 28 def method_call(self, instance, *args): 29 name = "_%s" % self.func.__name__ 30 if not hasattr(instance, name): 31 setattr(instance, name, {}) 32 cache = getattr(instance, name) 33 if args not in cache: 34 cache[args] = self.func(instance, *args) 35 return cache[args] 36 37 def __get__(self, instance, cls): 38 return functools.update_wrapper( 39 functools.partial(self.method_call, instance), self.func 40 ) 41