MCPcopy Index your code
hub / github.com/python/cpython / ToASCII

Function ToASCII

Lib/encodings/idna.py:66–115  ·  view source on GitHub ↗
(label)

Source from the content-addressed store, hash-verified

64 return label
65
66def ToASCII(label): # type: (str) -> bytes
67 try:
68 # Step 1: try ASCII
69 label_ascii = label.encode("ascii")
70 except UnicodeEncodeError:
71 pass
72 else:
73 # Skip to step 3: UseSTD3ASCIIRules is false, so
74 # Skip to step 8.
75 if 0 < len(label_ascii) < 64:
76 return label_ascii
77 if len(label) == 0:
78 raise UnicodeEncodeError("idna", label, 0, 1, "label empty")
79 else:
80 raise UnicodeEncodeError("idna", label, 0, len(label), "label too long")
81
82 # Step 2: nameprep
83 label = nameprep(label)
84
85 # Step 3: UseSTD3ASCIIRules is false
86 # Step 4: try ASCII
87 try:
88 label_ascii = label.encode("ascii")
89 except UnicodeEncodeError:
90 pass
91 else:
92 # Skip to step 8.
93 if 0 < len(label) < 64:
94 return label_ascii
95 if len(label) == 0:
96 raise UnicodeEncodeError("idna", label, 0, 1, "label empty")
97 else:
98 raise UnicodeEncodeError("idna", label, 0, len(label), "label too long")
99
100 # Step 5: Check ACE prefix
101 if label.lower().startswith(sace_prefix):
102 raise UnicodeEncodeError(
103 "idna", label, 0, len(sace_prefix), "Label starts with ACE prefix")
104
105 # Step 6: Encode with PUNYCODE
106 label_ascii = label.encode("punycode")
107
108 # Step 7: Prepend ACE prefix
109 label_ascii = ace_prefix + label_ascii
110
111 # Step 8: Check size
112 # do not check for empty as we prepend ace_prefix.
113 if len(label_ascii) < 64:
114 return label_ascii
115 raise UnicodeEncodeError("idna", label, 0, len(label), "label too long")
116
117def ToUnicode(label):
118 if len(label) > 1024:

Callers 3

ToUnicodeFunction · 0.85
encodeMethod · 0.85
_buffer_encodeMethod · 0.85

Calls 4

nameprepFunction · 0.85
encodeMethod · 0.45
startswithMethod · 0.45
lowerMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…