From 05ff9904010a488cc640637ac8255cae41b270dd Mon Sep 17 00:00:00 2001 From: R David Murray Date: Fri, 17 Jun 2011 12:54:56 -0400 Subject: #11767: use context manager to close file in __getitem__ to prevent FD leak MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Lib/mailbox.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'Lib/mailbox.py') diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 0e4f99bcaf..b96b270d68 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.""" -- cgit v1.2.1