Skip to content

Commit d4a28ef

Browse files
committed
Exit gracefully if config file cannot be decoded (re bpython#735)
1 parent 5698a9e commit d4a28ef

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

bpython/config.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,19 @@ def loadini(struct, configfile):
137137
in iteritems(defaults['keyboard']))
138138

139139
fill_config_with_default_values(config, defaults)
140-
if not config.read(config_path):
141-
# No config file. If the user has it in the old place then complain
142-
if os.path.isfile(os.path.expanduser('~/.bpython.ini')):
143-
sys.stderr.write("Error: It seems that you have a config file at "
144-
"~/.bpython.ini. Please move your config file to "
145-
"%s\n" % default_config_path())
146-
sys.exit(1)
140+
try:
141+
if not config.read(config_path):
142+
# No config file. If the user has it in the old place then complain
143+
if os.path.isfile(os.path.expanduser('~/.bpython.ini')):
144+
sys.stderr.write("Error: It seems that you have a config file at "
145+
"~/.bpython.ini. Please move your config file to "
146+
"%s\n" % default_config_path())
147+
sys.exit(1)
148+
except UnicodeDecodeError as e:
149+
sys.stderr.write("Error: Unable to parse config file at '{}' due to an "
150+
"encoding issue. Please make sure to fix the encoding "
151+
"of the file or remove it and then try again.\n".format(config_path))
152+
sys.exit(1)
147153

148154
def get_key_no_doublebind(command):
149155
default_commands_to_keys = defaults['keyboard']

0 commit comments

Comments
 (0)