[New-bugs-announce] [issue7944] Use the 'with' statement in conjunction with 'open' throughout test modules

Dave Fugate report at bugs.python.org
Tue Feb 16 18:43:51 CET 2010

New submission from Dave Fugate <dfugate at microsoft.com>:

Sprinkled throughout CPython's test modules are snippets of code such as the following taken from 2.7A3's test_old_mailbox.py (line 141):
    box = mailbox.UnixMailbox(open(self._path, 'r'))

The key thing to observe here is the file being opened yet never has it's 'close' method explicitly called.  While this is fine for CPython's rather predictable garbage collections, it's quite possible that in alternate implementations of Python this file object won't be destroyed until well after line 141.  This can result in seemingly random failures of CPython's tests under alternate implementations of Python.

The solution to this problem would be to consistently use the 'with' statement (or alternatively close all open file objects) throughout all CPython test modules.

I've gone ahead and submitted an updated (2.7A3) version of test_old_mailbox.py which addresses this.  We can find other places where this is going on as well, but for the most part the tests already seem to be doing the right thing.

components: Tests
files: test_old_mailbox.py
messages: 99425
nosy: midnightdf
severity: normal
status: open
title: Use the 'with' statement in conjunction with 'open' throughout test modules
type: feature request
versions: Python 2.6, Python 2.7
Added file: http://bugs.python.org/file16235/test_old_mailbox.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list