Fix DeepSource alerts
- Unguarded next inside generator
-
Calls to
next()
should be insidetry-except
block.When the iterator is exhausted,
StopIteration
exception is raised. When used inside a generator, this can cause unexpected behavior. If not handled, it will propagate out of the generator causing termination. PEP-479 has been accepted to fix this problem. It will modify the behavior ofStopIteration
in generators.Each call to
next()
should be wrapped in atry-except
block to explicitly handleStopIteration
exceptions.
-
The existing code was mostly correct, this can be seen as a false positive. However, the new code is arguably more Pythonic and more efficient because it iterates only once. Also, there was a small inconsistency in test-fortinet-login.py
, where this line tests c.value
:
if any(c.name == 'SVPNCOOKIE' and c.value for c in s.cookies):
while this one doesn't:
cookie = next(c.value for c in s.cookies if c.name == 'SVPNCOOKIE')
Partially fixes #341 (closed).