diff --git a/tests/functional/pyocf/types/data.py b/tests/functional/pyocf/types/data.py index fad4396..e65dfaf 100644 --- a/tests/functional/pyocf/types/data.py +++ b/tests/functional/pyocf/types/data.py @@ -59,7 +59,7 @@ class Data: DATA_POISON = 0xA5 PAGE_SIZE = 4096 - _instances_ = {} + _instances_ = weakref.WeakValueDictionary() _ocf_instances_ = [] def __init__(self, byte_count: int): @@ -69,12 +69,12 @@ class Data: self.handle = cast(byref(self.buffer), c_void_p) memset(self.handle, self.DATA_POISON, self.size) - type(self)._instances_[self.handle.value] = weakref.ref(self) + type(self)._instances_[self.handle.value] = self self._as_parameter_ = self.handle @classmethod def get_instance(cls, ref): - return cls._instances_[ref]() + return cls._instances_[ref] @classmethod def get_ops(cls): diff --git a/tests/functional/pyocf/types/logger.py b/tests/functional/pyocf/types/logger.py index 9518bb0..df7e963 100644 --- a/tests/functional/pyocf/types/logger.py +++ b/tests/functional/pyocf/types/logger.py @@ -69,7 +69,7 @@ class LoggerPriv(Structure): class Logger(Structure): - _instances_ = {} + _instances_ = weakref.WeakValueDictionary() _fields_ = [("logger", c_void_p)] @@ -81,7 +81,7 @@ class Logger(Structure): ) self.priv = LoggerPriv(_log=self._log) self._as_parameter_ = cast(pointer(self.priv), c_void_p).value - self._instances_[self._as_parameter_] = weakref.ref(self) + self._instances_[self._as_parameter_] = self def get_ops(self): return self.ops @@ -92,7 +92,7 @@ class Logger(Structure): @classmethod def get_instance(cls, ctx: int): priv = OcfLib.getInstance().ocf_logger_get_priv(ctx) - return cls._instances_[priv]() + return cls._instances_[priv] @staticmethod @LoggerOps.LOG diff --git a/tests/functional/pyocf/types/queue.py b/tests/functional/pyocf/types/queue.py index eed3c5f..112a848 100644 --- a/tests/functional/pyocf/types/queue.py +++ b/tests/functional/pyocf/types/queue.py @@ -38,7 +38,7 @@ def io_queue_run(*, queue: Queue, kick: Condition, stop: Event): class Queue: - _instances_ = {} + _instances_ = weakref.WeakValueDictionary() def __init__(self, cache, name): @@ -51,7 +51,7 @@ class Queue: if status: raise OcfError("Couldn't create queue object", status) - Queue._instances_[self.handle.value] = weakref.ref(self) + Queue._instances_[self.handle.value] = self self._as_parameter_ = self.handle self.stop_event = Event() @@ -70,7 +70,7 @@ class Queue: @classmethod def get_instance(cls, ref): - return cls._instances_[ref]() + return cls._instances_[ref] @staticmethod @QueueOps.KICK_SYNC diff --git a/tests/functional/pyocf/types/volume.py b/tests/functional/pyocf/types/volume.py index 33f39e3..4fbac92 100644 --- a/tests/functional/pyocf/types/volume.py +++ b/tests/functional/pyocf/types/volume.py @@ -78,8 +78,8 @@ class Volume(Structure): VOLUME_POISON = 0x13 _fields_ = [("_storage", c_void_p)] - _instances_ = {} - _uuid_ = {} + _instances_ = weakref.WeakValueDictionary() + _uuid_ = weakref.WeakValueDictionary() props = None @@ -95,7 +95,7 @@ class Volume(Structure): else: self.uuid = str(id(self)) - type(self)._uuid_[self.uuid] = weakref.ref(self) + type(self)._uuid_[self.uuid] = self self.data = create_string_buffer(int(self.size)) memset(self.data, self.VOLUME_POISON, self.size) @@ -138,7 +138,7 @@ class Volume(Structure): @classmethod def get_instance(cls, ref): - instance = cls._instances_[ref]() + instance = cls._instances_[ref] if instance is None: print("tried to access {} but it's gone".format(ref)) @@ -146,7 +146,7 @@ class Volume(Structure): @classmethod def get_by_uuid(cls, uuid): - return cls._uuid_[uuid]() + return cls._uuid_[uuid] @staticmethod @VolumeOps.SUBMIT_IO @@ -205,7 +205,7 @@ class Volume(Structure): if volume.opened: return -OcfErrorCode.OCF_ERR_NOT_OPEN_EXC - Volume._instances_[ref] = weakref.ref(volume) + Volume._instances_[ref] = volume return volume.open()