Fix literal "None" output when charset detection fails in PlainTextConverter and CsvConverter#2222
Open
sohumt123 wants to merge 1 commit into
Open
Fix literal "None" output when charset detection fails in PlainTextConverter and CsvConverter#2222sohumt123 wants to merge 1 commit into
sohumt123 wants to merge 1 commit into
Conversation
…and CSV converters When no charset hint is available, PlainTextConverter and CsvConverter decode via str(from_bytes(data).best()). charset_normalizer's best() returns None for byte sequences it cannot classify (e.g. a corrupt or truncated UTF-16 file), so str(None) silently replaced the entire document with the 4-character string "None" — a .txt converted to the markdown 'None' and a .csv to the bogus table '| None |', with no error raised. Guard the best() result: when it is None, fall back to decoding as UTF-8 with errors="replace", which preserves whatever is decodable and never fabricates content. Add a regression test covering both converters.
|
@sohumt123 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When no charset hint is available,
PlainTextConverterandCsvConverterdecode file content via:charset_normalizer.from_bytes(...).best()isOptional[CharsetMatch]— it returnsNonefor byte sequences it cannot classify (e.g. a corrupt/truncated UTF-16 file such asb"\xff\xfe\xff\xff\x00"). Wrapping that instr()turns the entire document into the 4-character stringNone, reported as a successful conversion:.txt→ markdown output'None'.csv→ the bogus table'| None |\n| --- |'No error or warning is raised, so this is silent data corruption. The magika pre-guess doesn't prevent it: for such content magika returns
unknown, so the extension-based guess (withcharset=None) reaches the converter and takes thefrom_bytesfallback path.Repro on current main:
Fix
In both converters, capture the
best()result and guard it: if it isNone, fall back todata.decode("utf-8", errors="replace"), which preserves whatever is decodable and never fabricates content; otherwise keep the existingstr(best_guess)behavior. All existing behavior for detectable inputs is unchanged.Testing
test_undetectable_charset_does_not_become_none) that converts the undetectable byte sequence through both the.txtand.csvpaths; it fails without the fix and passes with it.cd packages/markitdown; hatch test→ 333 passed, 4 skipped.Related work
Open PRs #1938 / #1954 / #1956 / #2015 / #1540 address a different failure mode (a wrong charset hint raising
UnicodeDecodeError) and all keep the unguardedstr(from_bytes(...).best()), so this bug persists with any of them; happy to rebase if one of those lands first.