From 1a0148617077868d35ac775449a88fde7d69749e Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Thu, 26 Aug 2021 13:33:31 -0700 Subject: [PATCH] auto-t: hwsim.py: turn Hwsim into singleton (per-namespace) This prevents reallocation of new Hwsim classes on each call if one already exists. This is a bit more efficient on memory. --- autotests/util/hwsim.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/autotests/util/hwsim.py b/autotests/util/hwsim.py index db106dcb..cc62495c 100755 --- a/autotests/util/hwsim.py +++ b/autotests/util/hwsim.py @@ -3,6 +3,7 @@ import dbus import sys import collections +from weakref import WeakValueDictionary from abc import ABCMeta, abstractmethod from enum import Enum @@ -297,7 +298,25 @@ class RadioList(collections.Mapping): return obj class Hwsim(iwd.AsyncOpAbstract): + _instances = WeakValueDictionary() + + def __new__(cls, namespace=ctx): + name = namespace.name + + if name not in cls._instances.keys(): + obj = object.__new__(cls) + obj._initialized = False + + cls._instances[name] = obj + + return cls._instances[name] + def __init__(self, namespace=ctx): + if self._initialized: + return + + self._initialized = True + self._bus = namespace.get_bus() self._rule_manager_if = dbus.Interface(