From 0e480b4e52b99090aba7b714a5e61f7a2c9913f2 Mon Sep 17 00:00:00 2001 From: nyuszika7h Date: Fri, 3 Jan 2014 17:44:01 +0100 Subject: [PATCH] Fix file descriptor leaks --- src/dbi.py | 4 ++++ src/httpserver.py | 8 ++++++-- src/i18n.py | 9 ++++++--- src/shlex.py | 3 ++- src/utils/file.py | 5 ++++- src/utils/transaction.py | 1 + 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/dbi.py b/src/dbi.py index dee742297..b353d7ecc 100644 --- a/src/dbi.py +++ b/src/dbi.py @@ -121,6 +121,8 @@ class DirMapping(MappingInterface): exn = NoRecordError(id) exn.realException = e raise exn + finally: + fd.close() def set(self, id, s): fd = open(self._makeFilename(id), 'w') @@ -158,6 +160,8 @@ class FlatfileMapping(MappingInterface): self.maxSize = int(math.log10(maxSize)) self.currentId = 0 self._incrementCurrentId() + finally: + fd.close() def _canonicalId(self, id): if id is not None: diff --git a/src/httpserver.py b/src/httpserver.py index 028809c26..787ae840f 100644 --- a/src/httpserver.py +++ b/src/httpserver.py @@ -157,10 +157,12 @@ set_default_templates(DEFAULT_TEMPLATES) def get_template(filename): path = conf.supybot.directories.data.web.dirize(filename) if os.path.isfile(path): - return open(path, 'r').read() + with open(path, 'r') as fd: + return fd.read() else: assert os.path.isfile(path + '.example'), path + '.example' - return open(path + '.example', 'r').read() + with open(path + '.example', 'r') as fd: + return fd.read() class RealSupyHTTPServer(HTTPServer): # TODO: make this configurable @@ -360,6 +362,8 @@ class Favicon(SupyHTTPServerCallback): found = True except IOError: pass + finally: + icon.close() if found: response = icon.read() filename = file_path.rsplit(os.sep, 1)[1] diff --git a/src/i18n.py b/src/i18n.py index 73e203f22..f1c72da9b 100644 --- a/src/i18n.py +++ b/src/i18n.py @@ -58,9 +58,10 @@ def getLocaleFromRegistryFilename(filename): """Called by the 'supybot' script. Gets the locale name before conf is loaded.""" global currentLocale - for line in open(filename, 'r'): - if line.startswith('supybot.language: '): - currentLocale = line[len('supybot.language: '):] + with open(filename, 'r') as fd: + for line in fd: + if line.startswith('supybot.language: '): + currentLocale = line[len('supybot.language: '):] def import_conf(): """Imports the conf into this module""" @@ -163,6 +164,8 @@ class _PluginInternationalization: self._parse(translationFile) except (IOError, PluginNotFound): # The translation is unavailable pass + finally: + translationFile.close() def _parse(self, translationFile): """A .po files parser. diff --git a/src/shlex.py b/src/shlex.py index eb447a725..dcfbbe658 100644 --- a/src/shlex.py +++ b/src/shlex.py @@ -208,7 +208,8 @@ if __name__ == '__main__': lexer = shlex() else: file = sys.argv[1] - lexer = shlex(open(file), file) + with open(file) as fd: + lexer = shlex(fd, file) while 1: tt = lexer.get_token() if tt: diff --git a/src/utils/file.py b/src/utils/file.py index eb3901bad..16981db5a 100644 --- a/src/utils/file.py +++ b/src/utils/file.py @@ -38,7 +38,8 @@ from . import crypt from .iter import ifilter def contents(filename): - return open(filename).read() + with open(filename) as fd: + return fd.read() def open_mkdir(filename, mode='wb', *args, **kwargs): """filename -> file object. @@ -62,6 +63,8 @@ def copy(src, dst): srcfd = open(src) dstfd = open_mkdir(dst, 'wb') shutil.copyfileobj(srcfd, dstfd) + srcfd.close() + dstfd.close() def writeLine(fd, line): fd.write(line) diff --git a/src/utils/transaction.py b/src/utils/transaction.py index 4f88f39b7..b91f14783 100644 --- a/src/utils/transaction.py +++ b/src/utils/transaction.py @@ -88,6 +88,7 @@ class TransactionMixin(python.Object): (command, rest) = line.split(None, 1) args = rest.split() yield (command, args) + journal.close() class Transaction(TransactionMixin):