libsmbios icon indicating copy to clipboard operation
libsmbios copied to clipboard

Fix the TokenTable iterator implementation

Open troygraben opened this issue 4 years ago • 6 comments

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.

troygraben avatar Jul 26 '21 21:07 troygraben

Cc @srinivasgowda and @goperry

superm1 avatar Jul 26 '21 21:07 superm1

This is actually the cause of issue #86 (which has been closed for some reason). The issue arise because of PEP 0479. Since python 3.7 raising a StopIteration exception explicitely inside a generator is deprecated and now raises a RuntimeException.

I think a much simpler fix for this could be to turn the raise StopIteration into a return. In its current state, this PR would disallow concurrent iterations of the TokenTable. (Moreover, calling __next__ on a iterator that already eached the end would call still token_table_get_next, which seems pretty surprising.)

Celelibi avatar Jul 31 '21 13:07 Celelibi

@Celelibi Your approach sounds much more sensible to me. It seems that many docs online still reference using StopIteration even though this PEP 0479 is has been in place for many years.

troygraben avatar Jul 31 '21 15:07 troygraben

Cc @dell-client-linux

superm1 avatar Jul 31 '21 15:07 superm1

This change fixes the problem for me as well. Please merge :)

kalvdans avatar May 05 '23 12:05 kalvdans

Hi, still experiencing this issue.

4oijq342 avatar Sep 03 '23 07:09 4oijq342