Skip to content

Commit 88f46f1

Browse files
committed
Improve the handling of non-ASCII config files
Config files are now assumed to be encoded as utf-8, just like Python 3 source files. If decoding does not work, we also try with latin-1, mostly to support legacy configuration files. Also moved reading the config file to a separate method that can be overridden when needed.
1 parent 260edf1 commit 88f46f1

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

webware/MiscUtils/Configurable.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def userConfig(self):
132132
if not filename:
133133
return {}
134134
try:
135-
with open(filename) as f:
136-
contents = f.read()
135+
contents = self.readConfig(filename)
137136
except IOError as e:
138137
print('WARNING: Config file', filename, 'not loaded:', e.strerror)
139138
print()
@@ -158,6 +157,23 @@ def userConfig(self):
158157
f'Invalid configuration file, {filename} ({e}).')
159158
return config
160159

160+
@staticmethod
161+
def readConfig(filename):
162+
"""Read the configuration from the file with the given name.
163+
164+
Raises an UIError if the configuration cannot be read.
165+
166+
This implementation assumes the file is stored in utf-8 encoding with
167+
possible BOM at the start, but also tries to read as latin-1 if it
168+
cannot be decoded as utf-8. Subclasses can override this behavior.
169+
"""
170+
try:
171+
with open(filename, encoding='utf-8-sig') as f:
172+
return f.read()
173+
except UnicodeDecodeError:
174+
with open(filename, encoding='latin-1') as f:
175+
return f.read()
176+
161177
def printConfig(self, dest=None):
162178
"""Print the configuration to the given destination.
163179

0 commit comments

Comments
 (0)