Skip to content

Commit

Permalink
❇️ Introducing fallback match for ASCII and UTF-8 (last resort only)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ousret committed Jul 15, 2021
1 parent d8f0bf4 commit ec95f7f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
28 changes: 28 additions & 0 deletions charset_normalizer/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ def from_bytes(
tested_but_hard_failure = [] # type: List[str]
tested_but_soft_failure = [] # type: List[str]

fallback_ascii = None # type: Optional[CharsetMatch]
fallback_u8 = None # type: Optional[CharsetMatch]

single_byte_hard_failure_count = 0 # type: int
single_byte_soft_failure_count = 0 # type: int

Expand Down Expand Up @@ -251,6 +254,20 @@ def from_bytes(
encoding_iana,
early_stop_count,
round(mean_mess_ratio * 100, ndigits=3))
# Preparing those fallbacks in case we got nothing.
if encoding_iana in ["ascii", "utf_8"]:
fallback_entry = CharsetMatch(
sequences,
encoding_iana,
threshold,
False,
[],
decoded_payload
)
if encoding_iana == "ascii":
fallback_ascii = fallback_entry
else:
fallback_u8 = fallback_entry
continue

logger.info(
Expand Down Expand Up @@ -314,6 +331,17 @@ def from_bytes(
results[-1]._languages
)

if len(results) == 0:
if fallback_u8 or fallback_ascii:
logger.warning("Nothing got out of the detection process. Using ASCII/UTF-8 fallback.")

if fallback_u8 and fallback_u8.fingerprint != fallback_ascii.fingerprint:
logger.warning("utf_8 will be used as a fallback match")
results.append(fallback_u8)
elif fallback_ascii:
logger.warning("ascii will be used as a fallback match")
results.append(fallback_ascii)

return results


Expand Down
15 changes: 15 additions & 0 deletions tests/test_on_byte.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ def test_empty_bytes(self):
len(r.alphabets)
)

def test_ensure_ascii_fallback(self):
payload = b"AbAdZ pOoooOlDl mmlDoDkA lldDkeEkddA mpAlkDF"
r = from_bytes(payload).best()

self.assertIsNotNone(
r,
msg="Fallback ASCII detection has failed. You clearly have tempered with it. Testing with {}".format(payload)
)

self.assertEqual(
r.encoding,
"ascii",
msg="Fallback ASCII miss-detection. You clearly have tempered with it. Testing with {}".format(payload)
)

def test_ensure_ascii(self):

for payload in [
Expand Down

0 comments on commit ec95f7f

Please sign in to comment.