diff options
author | R David Murray <rdmurray@bitdance.com> | 2011-06-17 16:54:56 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2011-06-17 16:54:56 (GMT) |
commit | 05ff9904010a488cc640637ac8255cae41b270dd (patch) | |
tree | 4dcec34c3a4b2c246bd0b299e96c0e93dae3b9c8 /Lib/mailbox.py | |
parent | 26de69dff824a9474ea0044afcbff2d30f109e1e (diff) | |
download | cpython-05ff9904010a488cc640637ac8255cae41b270dd.zip cpython-05ff9904010a488cc640637ac8255cae41b270dd.tar.gz cpython-05ff9904010a488cc640637ac8255cae41b270dd.tar.bz2 |
#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.
Diffstat (limited to 'Lib/mailbox.py')
-rw-r--r-- | Lib/mailbox.py | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 0e4f99b..b96b270 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -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.""" |