mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-10 14:54:17 +00:00
Merge pull request #1533 from PyCQA/utf8_config
always use UTF-8 encoding when reading configuration
This commit is contained in:
commit
ca573a7ccf
2 changed files with 28 additions and 3 deletions
|
|
@ -20,7 +20,7 @@ def _find_config_file(path: str) -> Optional[str]:
|
||||||
for candidate in ("setup.cfg", "tox.ini", ".flake8"):
|
for candidate in ("setup.cfg", "tox.ini", ".flake8"):
|
||||||
cfg_path = os.path.join(path, candidate)
|
cfg_path = os.path.join(path, candidate)
|
||||||
try:
|
try:
|
||||||
cfg.read(cfg_path)
|
cfg.read(cfg_path, encoding="UTF-8")
|
||||||
except (UnicodeDecodeError, configparser.ParsingError) as e:
|
except (UnicodeDecodeError, configparser.ParsingError) as e:
|
||||||
LOG.warning("ignoring unparseable config %s: %s", cfg_path, e)
|
LOG.warning("ignoring unparseable config %s: %s", cfg_path, e)
|
||||||
else:
|
else:
|
||||||
|
|
@ -61,7 +61,7 @@ def load_config(
|
||||||
|
|
||||||
cfg = configparser.RawConfigParser()
|
cfg = configparser.RawConfigParser()
|
||||||
if config is not None:
|
if config is not None:
|
||||||
if not cfg.read(config):
|
if not cfg.read(config, encoding="UTF-8"):
|
||||||
raise exceptions.ExecutionError(
|
raise exceptions.ExecutionError(
|
||||||
f"The specified config file does not exist: {config}"
|
f"The specified config file does not exist: {config}"
|
||||||
)
|
)
|
||||||
|
|
@ -72,7 +72,7 @@ def load_config(
|
||||||
# TODO: remove this and replace it with configuration modifying plugins
|
# TODO: remove this and replace it with configuration modifying plugins
|
||||||
# read the additional configs afterwards
|
# read the additional configs afterwards
|
||||||
for filename in extra:
|
for filename in extra:
|
||||||
cfg.read(filename)
|
cfg.read(filename, encoding="UTF-8")
|
||||||
|
|
||||||
return cfg, cfg_dir
|
return cfg, cfg_dir
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,31 @@ def test_load_config_append_config(tmpdir):
|
||||||
assert cfg_dir == str(tmpdir)
|
assert cfg_dir == str(tmpdir)
|
||||||
|
|
||||||
|
|
||||||
|
NON_ASCII_CONFIG = "# ☃\n[flake8]\nindent-size=8\n"
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_auto_config_utf8(tmpdir):
|
||||||
|
tmpdir.join("setup.cfg").write_text(NON_ASCII_CONFIG, encoding="UTF-8")
|
||||||
|
with tmpdir.as_cwd():
|
||||||
|
cfg, cfg_dir = config.load_config(None, [], isolated=False)
|
||||||
|
assert cfg["flake8"]["indent-size"] == "8"
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_explicit_config_utf8(tmpdir):
|
||||||
|
tmpdir.join("t.cfg").write_text(NON_ASCII_CONFIG, encoding="UTF-8")
|
||||||
|
with tmpdir.as_cwd():
|
||||||
|
cfg, cfg_dir = config.load_config("t.cfg", [], isolated=False)
|
||||||
|
assert cfg["flake8"]["indent-size"] == "8"
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_extra_config_utf8(tmpdir):
|
||||||
|
tmpdir.join("setup.cfg").write("[flake8]\nindent-size=2\n")
|
||||||
|
tmpdir.join("t.cfg").write_text(NON_ASCII_CONFIG, encoding="UTF-8")
|
||||||
|
with tmpdir.as_cwd():
|
||||||
|
cfg, cfg_dir = config.load_config(None, ["t.cfg"], isolated=False)
|
||||||
|
assert cfg["flake8"]["indent-size"] == "8"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def opt_manager():
|
def opt_manager():
|
||||||
ret = OptionManager(version="123", plugin_versions="", parents=[])
|
ret = OptionManager(version="123", plugin_versions="", parents=[])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue