-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Do not convert an undecodable file to the word "None" #2418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
afourney
merged 3 commits into
microsoft:main
from
kevin9327:fix/undetectable-charset-not-none
Sep 9, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #!/usr/bin/env python3 -m pytest | ||
| """Tests for bytes no charset decodes. | ||
|
|
||
| ``charset_normalizer.from_bytes(data).best()`` returns ``None`` when nothing | ||
| decodes the bytes, and ``str(None)`` is the four-character string ``"None"``, | ||
| so a binary file handed over with a text extension became a document whose | ||
| entire content was the word "None" -- four characters that were never in the | ||
| file. | ||
| """ | ||
|
|
||
| import io | ||
|
|
||
| from charset_normalizer import from_bytes | ||
|
|
||
| from markitdown import MarkItDown, StreamInfo | ||
|
|
||
| # Random bytes: no charset claims them, which is what makes `best()` answer None. | ||
| # Fixed rather than generated, so the test does not depend on chance. | ||
| UNDECODABLE = bytes((7 * i * i + 251 * i + 193) % 256 for i in range(4096)) | ||
|
|
||
|
|
||
|
|
||
| def test_the_fixture_really_is_undecodable() -> None: | ||
| """Guards the guard: if some charset claimed these bytes the rest would pass | ||
| for the wrong reason.""" | ||
| assert from_bytes(UNDECODABLE).best() is None | ||
|
|
||
|
|
||
| def test_binary_with_a_text_extension_is_not_the_word_none() -> None: | ||
| markitdown = MarkItDown() | ||
| for extension in (".txt", ".md"): | ||
| result = markitdown.convert_stream( | ||
| io.BytesIO(UNDECODABLE), file_extension=extension | ||
| ) | ||
| assert result.markdown != "None" | ||
|
|
||
|
|
||
| def test_binary_with_a_csv_extension_is_not_a_table_of_none() -> None: | ||
| markitdown = MarkItDown() | ||
| result = markitdown.convert_stream(io.BytesIO(UNDECODABLE), file_extension=".csv") | ||
| assert result.markdown != "| None |\n| --- |" | ||
|
|
||
|
|
||
| def test_ordinary_text_is_unchanged() -> None: | ||
| """The fallback only runs when detection fails; everything else is as it was.""" | ||
| markitdown = MarkItDown() | ||
|
|
||
| text = markitdown.convert_stream( | ||
| io.BytesIO(b"hello, world\n"), file_extension=".txt" | ||
| ) | ||
| assert text.markdown == "hello, world\n" | ||
|
|
||
| table = markitdown.convert_stream(io.BytesIO(b"a,b\n1,2\n"), file_extension=".csv") | ||
| assert table.markdown == "| a | b |\n| --- | --- |\n| 1 | 2 |" | ||
|
|
||
|
|
||
| def test_text_containing_the_word_none_survives() -> None: | ||
| """The bug was a whole document reading "None", not the word appearing in | ||
| one.""" | ||
| markitdown = MarkItDown() | ||
| result = markitdown.convert_stream( | ||
| io.BytesIO(b"value,note\nNone,missing\n"), file_extension=".csv" | ||
| ) | ||
| assert result.markdown == "| value | note |\n| --- | --- |\n| None | missing |" | ||
|
|
||
|
|
||
| def test_a_declared_charset_still_wins() -> None: | ||
| """`stream_info.charset` short-circuits detection and is untouched.""" | ||
| markitdown = MarkItDown() | ||
| body = "名称,代码\n浦发银行,600000\n".encode("gb18030") | ||
| result = markitdown.convert_stream( | ||
| io.BytesIO(body), | ||
| stream_info=StreamInfo(extension=".csv", charset="gb18030"), | ||
| ) | ||
| assert "浦发银行" in result.markdown | ||
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.
Uh oh!
There was an error while loading. Please reload this page.