Merge pull request #534 from nyuszika7h/fix/fd-leak

Fix file descriptor leaks
This commit is contained in:
Valentin Lorentz 2014-01-03 09:14:29 -08:00
commit 492a5ac04c
6 changed files with 23 additions and 7 deletions

View File

@ -121,6 +121,8 @@ class DirMapping(MappingInterface):
exn = NoRecordError(id) exn = NoRecordError(id)
exn.realException = e exn.realException = e
raise exn raise exn
finally:
fd.close()
def set(self, id, s): def set(self, id, s):
fd = open(self._makeFilename(id), 'w') fd = open(self._makeFilename(id), 'w')
@ -158,6 +160,8 @@ class FlatfileMapping(MappingInterface):
self.maxSize = int(math.log10(maxSize)) self.maxSize = int(math.log10(maxSize))
self.currentId = 0 self.currentId = 0
self._incrementCurrentId() self._incrementCurrentId()
finally:
fd.close()
def _canonicalId(self, id): def _canonicalId(self, id):
if id is not None: if id is not None:

View File

@ -157,10 +157,12 @@ set_default_templates(DEFAULT_TEMPLATES)
def get_template(filename): def get_template(filename):
path = conf.supybot.directories.data.web.dirize(filename) path = conf.supybot.directories.data.web.dirize(filename)
if os.path.isfile(path): if os.path.isfile(path):
return open(path, 'r').read() with open(path, 'r') as fd:
return fd.read()
else: else:
assert os.path.isfile(path + '.example'), path + '.example' 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): class RealSupyHTTPServer(HTTPServer):
# TODO: make this configurable # TODO: make this configurable
@ -364,6 +366,8 @@ class Favicon(SupyHTTPServerCallback):
found = True found = True
except IOError: except IOError:
pass pass
finally:
icon.close()
if found: if found:
response = icon.read() response = icon.read()
filename = file_path.rsplit(os.sep, 1)[1] filename = file_path.rsplit(os.sep, 1)[1]

View File

@ -58,7 +58,8 @@ def getLocaleFromRegistryFilename(filename):
"""Called by the 'supybot' script. Gets the locale name before conf is """Called by the 'supybot' script. Gets the locale name before conf is
loaded.""" loaded."""
global currentLocale global currentLocale
for line in open(filename, 'r'): with open(filename, 'r') as fd:
for line in fd:
if line.startswith('supybot.language: '): if line.startswith('supybot.language: '):
currentLocale = line[len('supybot.language: '):] currentLocale = line[len('supybot.language: '):]
@ -163,6 +164,8 @@ class _PluginInternationalization:
self._parse(translationFile) self._parse(translationFile)
except (IOError, PluginNotFound): # The translation is unavailable except (IOError, PluginNotFound): # The translation is unavailable
pass pass
finally:
translationFile.close()
def _parse(self, translationFile): def _parse(self, translationFile):
"""A .po files parser. """A .po files parser.

View File

@ -208,7 +208,8 @@ if __name__ == '__main__':
lexer = shlex() lexer = shlex()
else: else:
file = sys.argv[1] file = sys.argv[1]
lexer = shlex(open(file), file) with open(file) as fd:
lexer = shlex(fd, file)
while 1: while 1:
tt = lexer.get_token() tt = lexer.get_token()
if tt: if tt:

View File

@ -38,7 +38,8 @@ from . import crypt
from .iter import ifilter from .iter import ifilter
def contents(filename): def contents(filename):
return open(filename).read() with open(filename) as fd:
return fd.read()
def open_mkdir(filename, mode='wb', *args, **kwargs): def open_mkdir(filename, mode='wb', *args, **kwargs):
"""filename -> file object. """filename -> file object.
@ -62,6 +63,8 @@ def copy(src, dst):
srcfd = open(src) srcfd = open(src)
dstfd = open_mkdir(dst, 'wb') dstfd = open_mkdir(dst, 'wb')
shutil.copyfileobj(srcfd, dstfd) shutil.copyfileobj(srcfd, dstfd)
srcfd.close()
dstfd.close()
def writeLine(fd, line): def writeLine(fd, line):
fd.write(line) fd.write(line)

View File

@ -88,6 +88,7 @@ class TransactionMixin(python.Object):
(command, rest) = line.split(None, 1) (command, rest) = line.split(None, 1)
args = rest.split() args = rest.split()
yield (command, args) yield (command, args)
journal.close()
class Transaction(TransactionMixin): class Transaction(TransactionMixin):