Fixes to function caching in targets.py

Now funnctions are looked up in the cache using a (function name,
arguments) key, which makes it possible to cache different invocations
of the functions (with different arguments).
Also applied the @cached attribute to get_target.
pull/2148/head
Jimmy Brisson 2016-07-12 12:54:44 +03:00 committed by Bogdan Marinescu
parent 4b441c9e9e
commit 50dbce9e74
1 changed files with 5 additions and 9 deletions

View File

@ -48,9 +48,9 @@ class HookError(Exception):
caches = {} caches = {}
def cached(func): def cached(func):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
if not caches.has_key(func): if not caches.has_key((func.__name__, args)):
caches[func] = func(*args, **kwargs) caches[(func.__name__, args)] = func(*args, **kwargs)
return caches[func] return caches[(func.__name__, args)]
return wrapper return wrapper
class Target: class Target:
@ -58,9 +58,6 @@ class Target:
# need to be computed differently than regular attributes # need to be computed differently than regular attributes
__cumulative_attributes = ['extra_labels', 'macros', 'device_has', 'features'] __cumulative_attributes = ['extra_labels', 'macros', 'device_has', 'features']
# {target_name: target_instance} map for all the targets in the system
__target_map = {}
# List of targets that were added dynamically using "add_py_targets" (see below) # List of targets that were added dynamically using "add_py_targets" (see below)
__py_targets = set() __py_targets = set()
@ -200,10 +197,9 @@ class Target:
# Return the target instance starting from the target name # Return the target instance starting from the target name
@staticmethod @staticmethod
@cached
def get_target(name): def get_target(name):
if not Target.__target_map.has_key(name): return Target(name)
Target.__target_map[name] = Target(name)
return Target.__target_map[name]
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name