Repository navigation
[bugfix] Read config files as UTF-8 - #633
Conversation
| import pytest | ||
|
|
||
| import scenedetect | ||
| import scenedetect._cli.config as config_module |
Breakthrough
left a comment
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Why do we use uft-8-sig here? Won't that be incorrect on Linux?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
I want to cut 0.7.4, will address any unresolved issues on main. Thanks for the PR!
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.cfgwith a non-ASCII output path or comment, or one saved with a byte order mark, ends with a traceback:On code pages where the bytes happen to decode (e.g. cp1252), a path in the file is silently garbled instead.
UnicodeDecodeErroris aValueError, so the(ConfigParserError, OSError)handler does not turn it into aConfigLoadFailure.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: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'sopendefaulting to cp1252 so it also fails on Linux and macOS without the fix) andtest_config_file_invalid_utf8. All 3 fail on main and pass with the fix.pytest tests/test_cli.pygives 175 passed, 1 skipped.ruff check,ruff format --checkand pyright are clean on the changed files. I also ran the CLI on Korean Windows with a config that sets a Koreanoutputdirectory, and the scene list is written there.