fix: file-type detection crash on non-UTF-8 text starting with "{" or "["

2S
2mo ago
+45-1

Fixes #4446

Summary

detect_filetype() and partition() raised UnicodeDecodeError on any non-UTF-8 plain-text file (cp1252, latin-1, …) whose first non-whitespace character is { or [ — log files with [timestamp] lines, INI-style configs, or legacy-encoded JSON on systems where libmagic reports it as text/plain. _TextFileDifferentiator._is_json parses the raw byte stream with json.load(), which auto-detects only the UTF-8/16/32 family (the JSON interchange encodings, RFC 8259), and caught only json.JSONDecodeError.

Fix

Keep the json.load() raw-stream parse first (preserving JSON-spec UTF-16/32 auto-detection exactly), and on UnicodeDecodeError/UnicodeError retry once decoding with the declared encoding — so an explicitly-declared legacy-encoded JSON file still classifies as JSON — otherwise classify as not-JSON instead of raising. This is a regression of the encoding-aware detection from #707 (issue #705), reintroduced when detection was rewritten into the _TextFileDifferentiator architecture.

Testing

Two new parametrized _is_json cases (cp1252 JSON-lookalike, latin-1 INI) plus a file-path test and a declared-encoding-JSON test — all four fail with the exact UnicodeDecodeError without the fix and pass with it. Full test_filetype.py: 277 passed. ruff check + ruff format --check clean on touched files. CHANGELOG entry and version bump (0.26.5 — note: open PR #4444 claims 0.26.4; whichever merges second has a trivial adjacent-line CHANGELOG/version conflict, happy to renumber) included per repo convention.

Disclosure: prepared with AI assistance (Claude Code); I reviewed the change and take responsibility for it.