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

Function nameprep

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

Source from the content-addressed store, hash-verified

12
13# This assumes query strings, so AllowUnassigned is true
14def nameprep(label): # type: (str) -> str
15 # Map
16 newlabel = []
17 for c in label:
18 if stringprep.in_table_b1(c):
19 # Map to nothing
20 continue
21 newlabel.append(stringprep.map_table_b2(c))
22 label = "".join(newlabel)
23
24 # Normalize
25 label = unicodedata.normalize("NFKC", label)
26
27 # Prohibit
28 for i, c in enumerate(label):
29 if stringprep.in_table_c12(c) or \
30 stringprep.in_table_c22(c) or \
31 stringprep.in_table_c3(c) or \
32 stringprep.in_table_c4(c) or \
33 stringprep.in_table_c5(c) or \
34 stringprep.in_table_c6(c) or \
35 stringprep.in_table_c7(c) or \
36 stringprep.in_table_c8(c) or \
37 stringprep.in_table_c9(c):
38 raise UnicodeEncodeError("idna", label, i, i+1, f"Invalid character {c!r}")
39
40 # Check bidi
41 RandAL = [stringprep.in_table_d1(x) for x in label]
42 if any(RandAL):
43 # There is a RandAL char in the string. Must perform further
44 # tests:
45 # 1) The characters in section 5.8 MUST be prohibited.
46 # This is table C.8, which was already checked
47 # 2) If a string contains any RandALCat character, the string
48 # MUST NOT contain any LCat character.
49 for i, x in enumerate(label):
50 if stringprep.in_table_d2(x):
51 raise UnicodeEncodeError("idna", label, i, i+1,
52 "Violation of BIDI requirement 2")
53 # 3) If a string contains any RandALCat character, a
54 # RandALCat character MUST be the first character of the
55 # string, and a RandALCat character MUST be the last
56 # character of the string.
57 if not RandAL[0]:
58 raise UnicodeEncodeError("idna", label, 0, 1,
59 "Violation of BIDI requirement 3")
60 if not RandAL[-1]:
61 raise UnicodeEncodeError("idna", label, len(label)-1, len(label),
62 "Violation of BIDI requirement 3")
63
64 return label
65
66def ToASCII(label): # type: (str) -> bytes
67 try:

Callers 3

test_nameprepMethod · 0.90
ToASCIIFunction · 0.85
ToUnicodeFunction · 0.85

Calls 5

enumerateFunction · 0.85
anyFunction · 0.50
appendMethod · 0.45
joinMethod · 0.45
normalizeMethod · 0.45

Tested by 1

test_nameprepMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…