182def valid_contexto(label: str, pos: int, exception: bool =
False) -> bool:
183 cp_value =
ord(label[pos])
185 if cp_value == 0x00b7:
186 if 0 < pos <
len(label)-1:
187 if ord(label[pos - 1]) == 0x006c
and ord(label[pos + 1]) == 0x006c:
191 elif cp_value == 0x0375:
192 if pos <
len(label)-1
and len(label) > 1:
196 elif cp_value == 0x05f3
or cp_value == 0x05f4:
201 elif cp_value == 0x30fb:
209 elif 0x660 <= cp_value <= 0x669:
211 if 0x6f0 <=
ord(cp) <= 0x06f9:
215 elif 0x6f0 <= cp_value <= 0x6f9:
217 if 0x660 <=
ord(cp) <= 0x0669:
224def check_label(label: Union[str, bytes, bytearray]) ->
None:
231 check_hyphen_ok(label)
232 check_initial_combiner(label)
240 if not valid_contextj(label, pos):
242 _unot(cp_value), pos+1, repr(label)))
244 raise IDNAError(
'Unknown codepoint adjacent to joiner {} at position {} in {}'.format(
245 _unot(cp_value), pos+1, repr(label)))
247 if not valid_contexto(label, pos):
250 raise InvalidCodepoint(
'Codepoint {} at position {} of {} not allowed'.format(
_unot(cp_value), pos+1, repr(label)))
308def uts46_remap(domain: str, std3_rules: bool =
True, transitional: bool =
False) -> str:
309 """Re-map the characters in the string according to UTS46 processing."""
310 from .uts46data
import uts46data
314 code_point =
ord(char)
316 uts46row = uts46data[code_point
if code_point < 256
else
320 if len(uts46row) == 3:
321 replacement = uts46row[2]
323 (status ==
'D' and not transitional)
or
324 (status ==
'3' and not std3_rules
and replacement
is None)):
326 elif replacement
is not None and (status ==
'M' or
327 (status ==
'3' and not std3_rules)
or
328 (status ==
'D' and transitional)):
329 output += replacement
334 'Codepoint {} not allowed at position {} in {}'.format(
335 _unot(code_point), pos + 1, repr(domain)))
340def encode(s: Union[str, bytes, bytearray], strict: bool =
False, uts46: bool =
False, std3_rules: bool =
False, transitional: bool =
False) -> bytes:
344 except UnicodeDecodeError:
345 raise IDNAError(
'should pass a unicode string to the function rather than a byte string.')
347 s = uts46_remap(s, std3_rules, transitional)
354 if not labels
or labels == [
'']:
367 s = b
'.'.join(result)
368 if not valid_string_length(s, trailing_dot):