Skip to content

[bugfix] Read config files as UTF-8 - #633

Merged
Breakthrough merged 1 commit into
Breakthrough:mainfrom
Alpenj:config-file-utf8
Nov 29, 2026
Merged

Breakthrough merged 1 commit into
Breakthrough:mainfrom
Alpenj:config-file-utf8

Conversation

@Alpenj

@Alpenj Alpenj commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Config files are opened without an encoding, so on Windows they are decoded with the ANSI code page instead of UTF-8. On Korean Windows (cp949), a UTF-8 scenedetect.cfg with a non-ASCII output path or comment, or one saved with a byte order mark, ends with a traceback:

> scenedetect -c scenedetect.cfg -i testvideo.mp4 detect-content list-scenes
  ...
    config_file_contents = config_file.read()
UnicodeDecodeError: 'cp949' codec can't decode byte 0xec in position 130: illegal multibyte sequence

On code pages where the bytes happen to decode (e.g. cp1252), a path in the file is silently garbled instead. UnicodeDecodeError is a ValueError, so the (ConfigParserError, OSError) handler does not turn it into a ConfigLoadFailure.

The fix reads config files as utf-8-sig (UTF-8 with or without a byte order mark) and reports a file that is not valid UTF-8 as a config error:

[PySceneDetect] Error: 'utf-8' codec can't decode byte 0xc7 in position 2: invalid continuation byte
[PySceneDetect] Error processing configuration file.

Behavior change: on Windows, a config file saved in the ANSI code page with non-ASCII text used to load and now gives the error above. I added a sentence to the config file docs saying the file must be UTF-8.

Tests: added test_config_file_read_as_utf8 (2 cases, with the module's open defaulting to cp1252 so it also fails on Linux and macOS without the fix) and test_config_file_invalid_utf8. All 3 fail on main and pass with the fix. pytest tests/test_cli.py gives 175 passed, 1 skipped. ruff check, ruff format --check and pyright are clean on the changed files. I also ran the CLI on Korean Windows with a config that sets a Korean output directory, and the scene list is written there.

@Breakthrough
Breakthrough self-requested a review November 29, 2026 01:51
Comment thread tests/test_cli.py
import pytest

import scenedetect
import scenedetect._cli.config as config_module

@Breakthrough Breakthrough left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, this seems like a reasonable direction to go. Could you add a changelog entry as well?

I think we should standardize on UTF8 without a BoM, thoughts?

config = ConfigParser()
try:
with open(path) as config_file:
with open(path, encoding="utf-8-sig") as config_file:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use uft-8-sig here? Won't that be incorrect on Linux?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry I looked into this and this is correct, thanks we should keep this. Maybe just keep it as a constant at the top of the module if possible and call it _CONFIG_FILE_ENCODING?

@Breakthrough Breakthrough added this to the 0.7.4 milestone Nov 29, 2026

@Breakthrough Breakthrough left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see your encoding you set is correct, thanks. Let's make sure to update the changelog entry, and also call out that this may break a small subset of users.

This will disallow users who saved a config file on Windows previously with non-ascii text saved as ANSI and not UTF8. The fix should be easy enough though, and now Notepad allows re-saving the config file with specific encoding. Maybe just call out in the changelog that existing config files need to be re-saved as UTF-8 if they fail to load?

@Breakthrough Breakthrough left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to cut 0.7.4, will address any unresolved issues on main. Thanks for the PR!

@Breakthrough
Breakthrough merged commit 9d0aa29 into Breakthrough:main Nov 29, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants