Re: test for absence of infinite loop

2018-07-17 Thread dieter
Robin Becker  writes:
> 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.

On some systems (among them Linux), you can limit (some) resources
used by a process, among them the CPU time. If a process exceeds
the limit, it is killed by the operating system.
The "bash" makes this available with the "ulimit" command.

On Linux, there is also the "timeout" command (with a similar effect).

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: test for absence of infinite loop

2018-07-17 Thread Cameron Simpson

On 17Jul2018 12:39, Robin Becker  wrote:

On 17/07/2018 12:16, Cameron Simpson wrote:

On 17Jul2018 10:10, 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.


While others have kindly pointed you at the halting problem 
(unsolvable in the general case) you can usually verify that the 
specific problem you fixed is fixed. Can you figure out how long the 
task should run with your fix in a test case? Not as time, but in 
loop iterations? Put a counter in the loop and check that its value 
doesn't exceed that.


well I understand the problem about not halting. However as you point 
out in a fixed case I know that the test should take fractions of a 
second to complete. I certainly don't want to put instrumentation into 
the source code. It's relatively easy to imagine polling termination 
of a separate thread/process, but that's not particularly reliable. I 
don't know if there is a way to ask a python interpeter how many instructions 
it has carried out.


Hmm. You can set a hard timeout with signal.alarm. Something like:

 import signal
 def test_timeout(maxtime):
   signal.alarm(maxtime)
   your_looping_function(...)
   signal.alarm(0)

The OS (if UNIX) will send SIGALRM after that number of seconds, which should 
abort the program by raising an exception. So any test suite will fail the 
test, or a bare test run will terminate anyway without running forever.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: test for absence of infinite loop

2018-07-17 Thread Terry Reedy

On 7/17/2018 7:39 AM, Robin Becker wrote:

well I understand the problem about not halting. However as you point 
out in a fixed case I know that the test should take fractions of a 
second to complete.


If nothing else, you can easily add

def test_xyz_completes(self):
xyz(args)  # Former infinite loop

I assume that your test runner has some time limit at some level of 
granularity.  You may be able to add a timeout for a particular test.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: test for absence of infinite loop

2018-07-17 Thread Alister via Python-list
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.

google halting problem & you will see that this is an impossible request



-- 
I hate quotations.
-- Ralph Waldo Emerson
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: test for absence of infinite loop

2018-07-17 Thread Steven D'Aprano
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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: test for absence of infinite loop

2018-07-17 Thread Robin Becker

On 17/07/2018 12:16, Cameron Simpson wrote:

On 17Jul2018 10:10, 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.


While others have kindly pointed you at the halting problem (unsolvable in the general case) you can usually verify that the 
specific problem you fixed is fixed. Can you figure out how long the task should run with your fix in a test case? Not as time, 
but in loop iterations? Put a counter in the loop and check that its value doesn't exceed that.


well I understand the problem about not halting. However as you point out in a fixed case I know that the test should take 
fractions of a second to complete. I certainly don't want to put instrumentation into the source code. It's relatively easy to 
imagine polling termination of a separate thread/process, but that's not particularly reliable. I don't know if there is a way to 
ask a python interpeter how many instructions it has carried out.

--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list


Re: test for absence of infinite loop

2018-07-17 Thread Cameron Simpson

On 17Jul2018 10:10, 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.


While others have kindly pointed you at the halting problem (unsolvable in the 
general case) you can usually verify that the specific problem you fixed is 
fixed. Can you figure out how long the task should run with your fix in a test 
case? Not as time, but in loop iterations? Put a counter in the loop and check 
that its value doesn't exceed that.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: test for absence of infinite loop

2018-07-17 Thread Robin Becker

On 17/07/2018 10:32, Chris Angelico wrote:
..


All you gotta do is solve the halting problem...

https://en.wikipedia.org/wiki/Halting_problem

ChrisA



ah so it's easy :)
--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list


Re: test for absence of infinite loop

2018-07-17 Thread Chris Angelico
On Tue, Jul 17, 2018 at 7:10 PM, 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.

All you gotta do is solve the halting problem...

https://en.wikipedia.org/wiki/Halting_problem

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


test for absence of infinite loop

2018-07-17 Thread Robin Becker
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.

--
Robin Becker

--
https://mail.python.org/mailman/listinfo/python-list