Fix file descriptor leaks

This commit is contained in:
nyuszika7h 2014-01-03 17:44:01 +01:00
parent 9ff1483da2
commit 0e480b4e52
6 changed files with 23 additions and 7 deletions

View File

@ -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:

View File

@ -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]

View File

@ -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.

View File

@ -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:

View File

@ -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)

View File

@ -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):