Skip to content

Commit

Permalink
Fix the TokenTable iterator implementation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
troygraben committed Jul 26, 2021
1 parent f01a217 commit 17720a7
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/python/libsmbios_c/smbios_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 17720a7

Please sign in to comment.