Skip to content

Commit

Permalink
Catch UnicodeDecodeError while parsing config files
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Oct 9, 2017
1 parent 2e12a20 commit bbe8d6d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/flake8/options/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,21 @@ def __init__(self, program_name, args, extra_config_files):
@staticmethod
def _read_config(files):
config = configparser.RawConfigParser()
try:
found_files = config.read(files)
except configparser.ParsingError:
LOG.exception("There was an error trying to parse a config "
"file. The files we were attempting to parse "
"were: %r", files)
found_files = []
if isinstance(files, (str, type(u''))):
files = [files]

found_files = []
for filename in files:
try:
found_files.extend(config.read(filename))
except UnicodeDecodeError:
LOG.exception("There was an error decoding a config file."
"The file with a problem was %s.",
filename)
except configparser.ParsingError:
LOG.exception("There was an error trying to parse a config "
"file. The file with a problem was %s.",
filename)
return (config, found_files)

def cli_config(self, files):
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/test_config_file_finder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""Tests for the ConfigFileFinder."""
import configparser
import os
Expand Down Expand Up @@ -135,4 +136,14 @@ def test_local_configs_double_read():
])
def test_read_config_catches_broken_config_files(files):
"""Verify that we do not allow the exception to bubble up."""
assert config.ConfigFileFinder._read_config(files)[1] == []
_, parsed = config.ConfigFileFinder._read_config(files)
assert BROKEN_CONFIG_PATH not in parsed


def test_read_config_catches_decoding_errors(tmpdir):
"""Verify that we do not allow the exception to bubble up."""
setup_cfg = tmpdir.join('setup.cfg')
# pick an encoding that's unlikely to be a default
setup_cfg.write_binary(u'[x]\ny = €'.encode('cp1252'))
_, parsed = config.ConfigFileFinder._read_config(setup_cfg.strpath)
assert parsed == []

0 comments on commit bbe8d6d

Please sign in to comment.