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 b68eb77
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/python/libsmbios_c/smbios_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ def __del__(self):

@traceLog()
def __iter__(self):
cur = ctypes.POINTER(Token)()
while 1:
cur =DLL.token_table_get_next( self._tableobj, cur )
if bool(cur):
yield cur.contents
else:
raise StopIteration
self._cur = ctypes.POINTER(Token)()
return self

@traceLog()
def __next__(self):
self._cur =DLL.token_table_get_next( self._tableobj, self._cur )
if bool(self._cur):
return self._cur.contents
else:
raise StopIteration

@traceLog()
def __getitem__(self, id):
Expand Down

0 comments on commit b68eb77

Please sign in to comment.