# HG changeset patch # User Pascal Volk # Date 1352652832 0 # Node ID 4cd9d0a9f42f7bc575b3aa95119a8ac55027b39c # Parent 724ac0a4068df0fa025fe77469c4f8c8fb427ee4 Use the with statement for file operations. diff -r 724ac0a4068d -r 4cd9d0a9f42f VirtualMailManager/cli/config.py --- a/VirtualMailManager/cli/config.py Tue Nov 06 23:23:55 2012 +0000 +++ b/VirtualMailManager/cli/config.py Sun Nov 11 16:53:52 2012 +0000 @@ -89,8 +89,7 @@ def _save_changes(self): """Writes changes to the configuration file.""" copy2(self._cfg_filename, self._cfg_filename + '.bak') - self._cfg_file = open(self._cfg_filename, 'w') - self.write(self._cfg_file) - self._cfg_file.close() + with open(self._cfg_filename, 'w') as self._cfg_file: + self.write(self._cfg_file) del _ diff -r 724ac0a4068d -r 4cd9d0a9f42f VirtualMailManager/config.py --- a/VirtualMailManager/config.py Tue Nov 06 23:23:55 2012 +0000 +++ b/VirtualMailManager/config.py Sun Nov 11 16:53:52 2012 +0000 @@ -357,14 +357,11 @@ Raises a ConfigError if the configuration syntax is invalid. """ - try: - self._cfg_file = open(self._cfg_filename, 'r') - self.readfp(self._cfg_file) - except (MissingSectionHeaderError, ParsingError), err: - raise ConfigError(str(err), CONF_ERROR) - finally: - if self._cfg_file and not self._cfg_file.closed: - self._cfg_file.close() + with open(self._cfg_filename, 'r') as self._cfg_file: + try: + self.readfp(self._cfg_file) + except (MissingSectionHeaderError, ParsingError), err: + raise ConfigError(str(err), CONF_ERROR) def check(self): """Performs a configuration check. diff -r 724ac0a4068d -r 4cd9d0a9f42f VirtualMailManager/mailbox.py --- a/VirtualMailManager/mailbox.py Tue Nov 06 23:23:55 2012 +0000 +++ b/VirtualMailManager/mailbox.py Sun Nov 11 16:53:52 2012 +0000 @@ -206,11 +206,9 @@ """Writes all created mailboxes to the subscriptions file.""" if not self._boxes: return - subscriptions = open('subscriptions', 'w') - subscriptions.write('\n'.join(self._boxes)) - subscriptions.write('\n') - subscriptions.flush() - subscriptions.close() + with open('subscriptions', 'w') as subscriptions: + subscriptions.write('\n'.join(self._boxes)) + subscriptions.write('\n') os.chown('subscriptions', self._user.uid, self._user.gid) os.chmod('subscriptions', self.__class__.FILE_MODE) del self._boxes[:]