From 490428630786a5a2dd52aa2759096b2742137a6b Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Thu, 29 Sep 2022 17:45:51 +0900 Subject: [PATCH] config: wrap UnicodeDecodeErrors on load --- dvc/config.py | 5 ++++- tests/unit/test_config.py | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/dvc/config.py b/dvc/config.py index 7e455f6b5..0e468ced7 100644 --- a/dvc/config.py +++ b/dvc/config.py @@ -176,7 +176,10 @@ class Config(dict): if fs.exists(filename): with fs.open(filename) as fobj: - conf_obj = ConfigObj(fobj) + try: + conf_obj = ConfigObj(fobj) + except UnicodeDecodeError as exc: + raise ConfigError(str(exc)) from exc else: conf_obj = ConfigObj() return _parse_named(_lower_keys(conf_obj.dict())) diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index e15586fac..bcae90e75 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -3,7 +3,7 @@ import textwrap import pytest -from dvc.config import Config +from dvc.config import Config, ConfigError from dvc.fs import LocalFileSystem @@ -70,3 +70,14 @@ def test_s3_ssl_verify(tmp_dir, dvc): ssl_verify = /path/to/custom/cabundle.pem """ ) + + +def test_load_unicode_error(tmp_dir, dvc, mocker): + config = Config(validate=False) + mocker.patch( + "configobj.ConfigObj", + side_effect=UnicodeDecodeError("", b"", 0, 0, ""), + ) + with pytest.raises(ConfigError): + with config.edit(): + pass