Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/cli/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <config_file-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 <config_file-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:

Expand Down
4 changes: 2 additions & 2 deletions scenedetect/_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -511,6 +512,38 @@
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)."""
Expand Down
Loading