test for absence of infinite loop
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Jul 17 08:30:41 EDT 2018
On Tue, 17 Jul 2018 10:10:49 +0100, Robin Becker wrote:
> A user reported an infinite loop in reportlab. I determined a possible
> cause and fix and would like to test for absence of the loop. Is there
> any way to check for presence/absence of an infinite loop in python? I
> imagine we could do something like call an external process and see if
> it takes too long, but that seems a bit flaky.
In general, no, it is impossible to detect infinite loops.
https://en.wikipedia.org/wiki/Halting_problem
That's not to say that either human readers or the compiler can't detect
*some* infinite loops ahead of time:
# obviously an infinite loop
while True:
pass
and then there's this:
https://www.usenix.org/legacy/publications/library/proceedings/vhll/
full_papers/koenig.a
but Python's compiler isn't capable of anything like that.
The way I sometimes deal with that sort of thing is to re-write selected
potentially-infinite loops:
while condition:
# condition may never become False
do something
to something like this:
for counter in range(1000):
if not condition: break
do something
else:
raise TooManyIterationsError
--
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson
More information about the Python-list
mailing list