diff options
author | Giampaolo Rodola' <g.rodola@gmail.com> | 2013-02-12 01:04:27 (GMT) |
---|---|---|
committer | Giampaolo Rodola' <g.rodola@gmail.com> | 2013-02-12 01:04:27 (GMT) |
commit | 2f50aaf2ff427fb713e82699a6dcbeeb038b10c2 (patch) | |
tree | 0e2c24897b8918f19d8504915ccebd466c0bbd92 /Lib/mailbox.py | |
parent | fd6e6cfa29b2289e711dc7f57f36897c78899ee7 (diff) | |
download | cpython-2f50aaf2ff427fb713e82699a6dcbeeb038b10c2.zip cpython-2f50aaf2ff427fb713e82699a6dcbeeb038b10c2.tar.gz cpython-2f50aaf2ff427fb713e82699a6dcbeeb038b10c2.tar.bz2 |
modernize some modules' code by using with statement around open()
Diffstat (limited to 'Lib/mailbox.py')
-rw-r--r-- | Lib/mailbox.py | 18 |
1 files changed, 4 insertions, 14 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 6d320ed..ab20ff9 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -366,14 +366,11 @@ class Maildir(Mailbox): def get_message(self, key): """Return a Message representation or raise a KeyError.""" subpath = self._lookup(key) - f = open(os.path.join(self._path, subpath), 'rb') - try: + with open(os.path.join(self._path, subpath), 'rb') as f: if self._factory: msg = self._factory(f) else: msg = MaildirMessage(f) - finally: - f.close() subdir, name = os.path.split(subpath) msg.set_subdir(subdir) if self.colon in name: @@ -383,11 +380,8 @@ class Maildir(Mailbox): def get_bytes(self, key): """Return a bytes representation or raise a KeyError.""" - f = open(os.path.join(self._path, self._lookup(key)), 'rb') - try: + with open(os.path.join(self._path, self._lookup(key)), 'rb') as f: return f.read().replace(linesep, b'\n') - finally: - f.close() def get_file(self, key): """Return a file-like representation or raise a KeyError.""" @@ -1033,7 +1027,7 @@ class MH(Mailbox): raise KeyError('No message with key: %s' % key) else: raise - try: + with f: if self._locked: _lock_file(f) try: @@ -1041,8 +1035,6 @@ class MH(Mailbox): finally: if self._locked: _unlock_file(f) - finally: - f.close() for name, key_list in self.get_sequences().items(): if key in key_list: msg.add_sequence(name) @@ -1060,7 +1052,7 @@ class MH(Mailbox): raise KeyError('No message with key: %s' % key) else: raise - try: + with f: if self._locked: _lock_file(f) try: @@ -1068,8 +1060,6 @@ class MH(Mailbox): finally: if self._locked: _unlock_file(f) - finally: - f.close() def get_file(self, key): """Return a file-like representation or raise a KeyError.""" |