From 17720a7df33f2f7e12a46eb5d2aa5fc2ee03ef3e Mon Sep 17 00:00:00 2001 From: troy <70726977+troygraben@users.noreply.github.com> Date: Mon, 26 Jul 2021 17:05:35 -0400 Subject: [PATCH] Fix the TokenTable iterator implementation The __iter__ function must perform initialization and return the iterator object itself. The __next__ function must return the next item or raise StopIteration when no more elements are available. This fixes the StopIteration exception being uncaught by the for loop when the iterator reaches the end of the collection. This could be encountered when calling smbios-token-ctl --dump-tokens. --- src/python/libsmbios_c/smbios_token.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/python/libsmbios_c/smbios_token.py b/src/python/libsmbios_c/smbios_token.py index c15c2deb..4a8cd43a 100644 --- a/src/python/libsmbios_c/smbios_token.py +++ b/src/python/libsmbios_c/smbios_token.py @@ -125,11 +125,15 @@ def __del__(self): @traceLog() def __iter__(self): - cur = ctypes.POINTER(Token)() + self._cur = ctypes.POINTER(Token)() + return self + + @traceLog() + def __next__(self): while 1: - cur =DLL.token_table_get_next( self._tableobj, cur ) - if bool(cur): - yield cur.contents + self._cur =DLL.token_table_get_next( self._tableobj, self._cur ) + if bool(self._cur): + return self._cur.contents else: raise StopIteration