-
Notifications
You must be signed in to change notification settings - Fork 65
CM-60540: remove binaryornot dep #397
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
gotbadger
merged 1 commit into
main
from
CM-60540-warning-from-cycode-cli-about-dependencies
Mar 5, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| _CONTROL_CHARS = b'\n\r\t\f\b' | ||
| _PRINTABLE_ASCII = _CONTROL_CHARS + bytes(range(32, 127)) | ||
| _PRINTABLE_HIGH_ASCII = bytes(range(127, 256)) | ||
|
|
||
| # BOM signatures for encodings that legitimately contain null bytes | ||
| _BOM_ENCODINGS = ( | ||
| (b'\xff\xfe\x00\x00', 'utf-32-le'), | ||
| (b'\x00\x00\xfe\xff', 'utf-32-be'), | ||
| (b'\xff\xfe', 'utf-16-le'), | ||
| (b'\xfe\xff', 'utf-16-be'), | ||
| ) | ||
|
|
||
|
|
||
| def _has_bom_encoding(bytes_to_check: bytes) -> bool: | ||
| """Check if bytes start with a BOM and can be decoded as that encoding.""" | ||
| for bom, encoding in _BOM_ENCODINGS: | ||
| if bytes_to_check.startswith(bom): | ||
| try: | ||
| bytes_to_check.decode(encoding) | ||
| return True | ||
| except (UnicodeDecodeError, LookupError): | ||
| pass | ||
| return False | ||
|
|
||
|
|
||
| def _is_decodable_as_utf8(bytes_to_check: bytes) -> bool: | ||
| """Try to decode bytes as UTF-8.""" | ||
| try: | ||
| bytes_to_check.decode('utf-8') | ||
| return True | ||
| except UnicodeDecodeError: | ||
| return False | ||
|
|
||
|
|
||
| def is_binary_string(bytes_to_check: bytes) -> bool: | ||
| """Check if a chunk of bytes appears to be binary content. | ||
|
|
||
| Uses a simplified version of the Perl detection algorithm, matching | ||
| the structure of binaryornot's is_binary_string. | ||
| """ | ||
| if not bytes_to_check: | ||
| return False | ||
|
|
||
| # Binary if control chars are > 30% of the string | ||
| low_chars = bytes_to_check.translate(None, _PRINTABLE_ASCII) | ||
| nontext_ratio1 = len(low_chars) / len(bytes_to_check) | ||
|
|
||
| # Binary if high ASCII chars are < 5% of the string | ||
| high_chars = bytes_to_check.translate(None, _PRINTABLE_HIGH_ASCII) | ||
| nontext_ratio2 = len(high_chars) / len(bytes_to_check) | ||
|
|
||
| is_likely_binary = (nontext_ratio1 > 0.3 and nontext_ratio2 < 0.05) or ( | ||
| nontext_ratio1 > 0.8 and nontext_ratio2 > 0.8 | ||
| ) | ||
|
|
||
| # BOM-marked UTF-16/32 files legitimately contain null bytes. | ||
| # Check this first so they aren't misdetected as binary. | ||
| if _has_bom_encoding(bytes_to_check): | ||
| return False | ||
|
|
||
| has_null_or_xff = b'\x00' in bytes_to_check or b'\xff' in bytes_to_check | ||
|
|
||
| if is_likely_binary: | ||
| # Only let UTF-8 rescue data that doesn't contain null bytes. | ||
| # Null bytes are valid UTF-8 but almost never appear in real text files, | ||
| # whereas binary formats (e.g. .DS_Store) are full of them. | ||
| if has_null_or_xff: | ||
| return True | ||
| return not _is_decodable_as_utf8(bytes_to_check) | ||
|
|
||
| # Null bytes or 0xff in otherwise normal-looking data indicate binary | ||
| return bool(has_null_or_xff) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,42 @@ | ||
| import pytest | ||
|
|
||
| from cycode.cli.utils.binary_utils import is_binary_string | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ('data', 'expected'), | ||
| [ | ||
| # Empty / None-ish | ||
| (b'', False), | ||
| (None, False), | ||
| # Plain ASCII text | ||
| (b'Hello, world!', False), | ||
| (b'print("hello")\nfor i in range(10):\n pass\n', False), | ||
| # Whitespace-heavy text (tabs, newlines) is not binary | ||
| (b'\t\t\n\n\r\n some text\n', False), | ||
| # UTF-8 multibyte text (accented, CJK, emoji) | ||
| ('café résumé naïve'.encode(), False), | ||
| ('日本語テキスト'.encode(), False), | ||
| ('🎉🚀💻'.encode(), False), | ||
| # BOM-marked UTF-16/32 text is not binary | ||
| ('\ufeffHello UTF-16'.encode('utf-16-le'), False), | ||
| ('\ufeffHello UTF-16'.encode('utf-16-be'), False), | ||
| ('\ufeffHello UTF-32'.encode('utf-32-le'), False), | ||
| ('\ufeffHello UTF-32'.encode('utf-32-be'), False), | ||
| # Null bytes → binary | ||
| (b'\x00', True), | ||
| (b'hello\x00world', True), | ||
| (b'\x00\x01\x02\x03', True), | ||
| # 0xff in otherwise normal data → binary | ||
| (b'hello\xffworld', True), | ||
| # Mostly control chars + invalid UTF-8 → binary | ||
| (b'\x01\x02\x03\x04\x05\x06\x07\x0e\x0f\x10' * 10 + b'\x80', True), | ||
| # Real binary format headers | ||
| (b'\x89PNG\r\n\x1a\n' + b'\x00' * 100, True), | ||
| (b'\x7fELF' + b'\x00' * 100, True), | ||
| # DS_Store-like: null-byte-heavy valid UTF-8 → still binary | ||
| (b'\x00\x00\x00\x01Bud1' + b'\x00' * 100, True), | ||
| ], | ||
| ) | ||
| def test_is_binary_string(data: bytes, expected: bool) -> None: | ||
| assert is_binary_string(data) is expected |
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.