#11767: use context manager to close file in __getitem__ to prevent FD leak

All of the other methods in mailbox that create message objects take care to
close the file descriptors they use, so it seems to make sense to have
__getitem__ do so as well.

Patch by Filip Gruszczyński.
This commit is contained in:
R David Murray 2011-06-17 12:54:56 -04:00
parent 26de69dff8
commit 05ff990401
3 changed files with 37 additions and 2 deletions

View file

@ -20,6 +20,7 @@ import email
import email.message
import email.generator
import io
import contextlib
try:
if sys.platform == 'os2emx':
# OS/2 EMX fcntl() not adequate
@ -76,7 +77,8 @@ class Mailbox:
if not self._factory:
return self.get_message(key)
else:
return self._factory(self.get_file(key))
with contextlib.closing(self.get_file(key)) as file:
return self._factory(file)
def get_message(self, key):
"""Return a Message representation or raise a KeyError."""