From 3efff81a5c74586dba7420ad143db2cc6bb693a5 Mon Sep 17 00:00:00 2001 From: Marc Bonnici Date: Tue, 22 May 2018 16:17:10 +0100 Subject: [PATCH] utils/exec_control: Fix issue with `once_per_instance` decorator Previously the `once_per_instance` used the output of `__repr__` to identify the class of where the decorated method was called from. For classes that override this method to produce the same output for different instances of the same class this caused different instances to be mistakenly treated as one. Now use the hash of the containing class instead of the string representation and update the tests to catch this error. --- tests/test_exec_control.py | 3 +++ wa/utils/exec_control.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_exec_control.py b/tests/test_exec_control.py index 73484fad..7c619f6e 100644 --- a/tests/test_exec_control.py +++ b/tests/test_exec_control.py @@ -46,6 +46,9 @@ class TestClass(object): def initilize_once_per_instance(self): self.count += 1 + def __repr__(self): + return '{}: Called={}'.format(self.__class__.__name__, self.called) + class SubClass(TestClass): diff --git a/wa/utils/exec_control.py b/wa/utils/exec_control.py index ef2fb3e3..b98e8a06 100644 --- a/wa/utils/exec_control.py +++ b/wa/utils/exec_control.py @@ -57,7 +57,7 @@ def once_per_instance(method): def wrapper(*args, **kwargs): if __active_environment is None: activate_environment('default') - func_id = repr(method.__hash__()) + repr(args[0]) + func_id = repr(method.__hash__()) + repr(args[0].__hash__()) if func_id in __environments[__active_environment]: return else: