Strange interaction between timeit and recursion

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun May 3 10:42:04 EDT 2009


I'm seeing a strange interaction between timeit and recursion.


>>> sys.getrecursionlimit()
1000
>>> from timeit import Timer
>>> setup = """def test(n=1):
...     if n < 999: return test(n+1)
...     return None
... """
>>> exec setup
>>> test() is None
True
>>>
>>> Timer('test()', setup).repeat(number=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/timeit.py", line 188, in repeat
    t = self.timeit(number)
  File "/usr/lib/python2.5/timeit.py", line 161, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 9, in inner
  File "<timeit-src>", line 4, in test
  File "<timeit-src>", line 4, in test
  File "<timeit-src>", line 4, in test
  ...
  File "<timeit-src>", line 4, in test



I don't understand why my recursive function hits the recursion limit 
inside the timeit.Timer when it works outside of it.

Is there any way to see the current recursion depth at a particular 
moment?



-- 
Steven



More information about the Python-list mailing list