Skip to content

Commit 806cec3

Browse files
committed
BUG#23324748: Guarantee file closing of input files in optionfiles
This patch adds a context manager to open files in the optionfiles module to be closed correctly. Thanks for the contribution.
1 parent 0cd5c41 commit 806cec3

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ v8.0.29
2020
- BUG#33729842: Character set 'utf8mb3' support
2121
- BUG#28877987: Return bytes or bytearray if decoding fails
2222
- BUG#23338623: Add support for Decimal parsing in protocol.py
23+
- BUG#23324748: Guarantee file closing of input files in optionfile
2324
- BUG#21528553: Fix API inconsistency when using consume_results=True
2425
- BUG#21498719: Fix conversion of Python bytearray (c-ext)
2526
- BUG#20065830: NaN is not supported

lib/mysql/connector/optionfiles.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
1+
# Copyright (c) 2014, 2022, Oracle and/or its affiliates.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -224,23 +224,24 @@ def read(self, filenames): # pylint: disable=W0221
224224
for priority, filename in enumerate(filenames):
225225
try:
226226
out_file = io.StringIO()
227-
for line in codecs.open(filename, encoding='utf-8'):
228-
line = line.strip()
229-
# Skip lines that begin with "!includedir" or "!include"
230-
if line.startswith('!include'):
231-
continue
232-
233-
match_obj = self.OPTCRE.match(line)
234-
if not self.SECTCRE.match(line) and match_obj:
235-
optname, delimiter, optval = match_obj.group('option',
236-
'vi',
237-
'value')
238-
if optname and not optval and not delimiter:
239-
out_file.write(line + "=\n")
227+
with codecs.open(filename, encoding="utf-8") as in_file:
228+
for line in in_file:
229+
line = line.strip()
230+
# Skip lines that begin with "!includedir" or "!include"
231+
if line.startswith("!include"):
232+
continue
233+
234+
match_obj = self.OPTCRE.match(line)
235+
if not self.SECTCRE.match(line) and match_obj:
236+
optname, delimiter, optval = match_obj.group(
237+
"option", "vi", "value"
238+
)
239+
if optname and not optval and not delimiter:
240+
out_file.write(f"{line}=\n")
241+
else:
242+
out_file.write(f"{line}\n")
240243
else:
241-
out_file.write(line + '\n')
242-
else:
243-
out_file.write(line + '\n')
244+
out_file.write(f"{line}\n")
244245
out_file.seek(0)
245246
except IOError:
246247
continue

0 commit comments

Comments
 (0)