Message139978
If you call timeit.timeit and the timed code raises an exception, then garbage collection is disabled. I have verified this in Python 2.7 and 3.2. Here's an interaction with Python 3.2:
Python 3.2 (r32:88445, Jul 7 2011, 15:52:49)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit, gc
>>> gc.isenabled()
True
>>> timeit.timeit('raise Exception')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/timeit.py", line 228, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/timeit.py", line 194, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
Exception
>>> gc.isenabled()
False
The problem is with the following code in Lib/timeit.py (lines 192–196):
gcold = gc.isenabled()
gc.disable()
timing = self.inner(it, self.timer)
if gcold:
gc.enable()
This should be changed to something like this:
gcold = gc.isenabled()
gc.disable()
try:
timing = self.inner(it, self.timer)
finally:
if gcold:
gc.enable() |
|
Date |
User |
Action |
Args |
2011-07-07 14:58:53 | gdr@garethrees.org | set | recipients:
+ gdr@garethrees.org |
2011-07-07 14:58:53 | gdr@garethrees.org | set | messageid: <1310050733.66.0.963908910352.issue12514@psf.upfronthosting.co.za> |
2011-07-07 14:58:52 | gdr@garethrees.org | link | issue12514 messages |
2011-07-07 14:58:52 | gdr@garethrees.org | create | |
|