diff --git a/docs/cli/config_file.rst b/docs/cli/config_file.rst index c7ba1ceb..98f37151 100644 --- a/docs/cli/config_file.rst +++ b/docs/cli/config_file.rst @@ -17,7 +17,7 @@ Specify a config file with ``-c`` / ``--config``. If you do not pass ``-c``, PyS * macOS: * ``~/Library/Application Support/PySceneDetect/scenedetect.cfg`` -Run ``scenedetect --help`` to see the exact path on your system. Command-line options override values in the config file. Most CLI options can be set in a config file, and some options are config-file only. The :ref:`Sections ` reference below lists every key, its default value, and its matching CLI option. Lines starting with ``#`` are comments and are ignored. +Run ``scenedetect --help`` to see the exact path on your system. Command-line options override values in the config file. Most CLI options can be set in a config file, and some options are config-file only. The :ref:`Sections ` reference below lists every key, its default value, and its matching CLI option. Lines starting with ``#`` are comments and are ignored. The file must be saved as UTF-8. The syntax of a config file is: diff --git a/scenedetect/_cli/config.py b/scenedetect/_cli/config.py index 6dea3251..2f62f9c8 100644 --- a/scenedetect/_cli/config.py +++ b/scenedetect/_cli/config.py @@ -804,10 +804,10 @@ def _load_from_disk(self, path=None): # Try to load and parse the config file at `path`. config = ConfigParser() try: - with open(path) as config_file: + with open(path, encoding="utf-8-sig") as config_file: config_file_contents = config_file.read() config.read_string(config_file_contents, source=path) - except (ConfigParserError, OSError) as ex: + except (ConfigParserError, OSError, UnicodeDecodeError) as ex: if DEBUG_MODE: raise raise ConfigLoadFailure(self._init_log, reason=ex) from None diff --git a/tests/test_cli.py b/tests/test_cli.py index 4144feaf..826da938 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -38,6 +38,7 @@ import pytest import scenedetect +import scenedetect._cli.config as config_module from scenedetect._cli.config import ConfigLoadFailure, ConfigRegistry from scenedetect._cli.context import CliContext from scenedetect.output import is_ffmpeg_available, is_mkvmerge_available @@ -511,6 +512,38 @@ def test_cli_kernel_size_config_file_rejects_one(tmp_path: Path, command: str): assert any("odd integer >= 3" in message for _, message in ex.value.init_log) +def _default_to_cp1252(monkeypatch: pytest.MonkeyPatch): + """Make the config module open text files as cp1252 when no encoding is given, so reads that + depend on the locale fail on every platform.""" + + def cp1252_open(file, mode="r", *args, **kwargs): + if "b" not in mode: + kwargs.setdefault("encoding", "cp1252") + return open(file, mode, *args, **kwargs) + + monkeypatch.setattr(config_module, "open", cp1252_open, raising=False) + + +@pytest.mark.parametrize("encoding", ["utf-8", "utf-8-sig"]) +def test_config_file_read_as_utf8(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, encoding: str): + """Config files are UTF-8 (with or without a byte order mark) regardless of the locale.""" + _default_to_cp1252(monkeypatch) + config_path = tmp_path.joinpath("config.cfg") + config_path.write_text("[global]\noutput = Łukasz ✨\n", encoding=encoding) + config = ConfigRegistry(str(config_path)) + assert config.get_value("global", "output") == "Łukasz ✨" + + +def test_config_file_invalid_utf8(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): + """A config file that is not valid UTF-8 must be reported as a config error.""" + monkeypatch.setattr(config_module, "DEBUG_MODE", False) # Debug mode re-raises the cause. + config_path = tmp_path.joinpath("config.cfg") + config_path.write_bytes(b"[global]\noutput = \xff\n") + with pytest.raises(ConfigLoadFailure) as ex: + ConfigRegistry(str(config_path)) + assert isinstance(ex.value.reason, UnicodeDecodeError) + + @pytest.mark.parametrize("command", ["detect-content", "detect-adaptive"]) def test_cli_weights_rejects_all_zero(command: str): """All-zero weights must give a usage error instead of NaN frame scores (#612)."""