From 49e3e32c8c643cee67758e208e9c0e9023e7f020 Mon Sep 17 00:00:00 2001 From: Vilnis Termanis Date: Thu, 12 May 2016 15:49:22 +0100 Subject: [PATCH] Guarantee file closing of input files in optionfiles module --- lib/mysql/connector/optionfiles.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/mysql/connector/optionfiles.py b/lib/mysql/connector/optionfiles.py index 4334481..e9ae04d 100644 --- a/lib/mysql/connector/optionfiles.py +++ b/lib/mysql/connector/optionfiles.py @@ -239,19 +239,20 @@ def read(self, filenames): # pylint: disable=W0221 for priority, filename in enumerate(filenames): try: out_file = io.StringIO() - for line in codecs.open(filename, encoding='utf-8'): - line = line.strip() - match_obj = self.OPTCRE.match(line) - if not self.SECTCRE.match(line) and match_obj: - optname, delimiter, optval = match_obj.group('option', - 'vi', - 'value') - if optname and not optval and not delimiter: - out_file.write(line + "=\n") + with codecs.open(filename, encoding='utf-8') as in_file: + for line in in_file(filename, encoding='utf-8'): + line = line.strip() + match_obj = self.OPTCRE.match(line) + if not self.SECTCRE.match(line) and match_obj: + optname, delimiter, optval = match_obj.group('option', + 'vi', + 'value') + if optname and not optval and not delimiter: + out_file.write(line + "=\n") + else: + out_file.write(line + '\n') else: out_file.write(line + '\n') - else: - out_file.write(line + '\n') out_file.seek(0) except IOError: continue