[New-bugs-announce] [issue12514] timeit disables garbage collection if timed code raises an exception

Gareth Rees report at bugs.python.org
Thu Jul 7 16:58:53 CEST 2011


New submission from Gareth Rees <gdr at garethrees.org>:

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()

----------
components: Library (Lib)
messages: 139978
nosy: Gareth.Rees
priority: normal
severity: normal
status: open
title: timeit disables garbage collection if timed code raises an exception
type: behavior
versions: Python 2.7, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12514>
_______________________________________


More information about the New-bugs-announce mailing list