77
88_virama_combining_class = 9
99_alabel_prefix = b'xn--'
10- _unicode_dots_re = re .compile (u'[\u002e \u3002 \uff0e \uff61 ]' )
11-
12- if sys .version_info [0 ] >= 3 :
13- unicode = str
14- unichr = chr
10+ _unicode_dots_re = re .compile ('[\u002e \u3002 \uff0e \uff61 ]' )
1511
1612class IDNAError (UnicodeError ):
1713 """ Base exception for all IDNA-encoding related problems """
@@ -34,10 +30,10 @@ class InvalidCodepointContext(IDNAError):
3430
3531
3632def _combining_class (cp ):
37- v = unicodedata .combining (unichr (cp ))
33+ v = unicodedata .combining (chr (cp ))
3834 if v == 0 :
39- if not unicodedata .name (unichr (cp )):
40- raise ValueError (" Unknown character in unicodedata" )
35+ if not unicodedata .name (chr (cp )):
36+ raise ValueError (' Unknown character in unicodedata' )
4137 return v
4238
4339def _is_script (cp , script ):
@@ -47,7 +43,7 @@ def _punycode(s):
4743 return s .encode ('punycode' )
4844
4945def _unot (s ):
50- return 'U+{0 :04X}' .format (s )
46+ return 'U+{:04X}' .format (s )
5147
5248
5349def valid_label_length (label ):
@@ -72,7 +68,7 @@ def check_bidi(label, check_ltr=False):
7268 direction = unicodedata .bidirectional (cp )
7369 if direction == '' :
7470 # String likely comes from a newer version of Unicode
75- raise IDNABidiError ('Unknown directionality in label {0 } at position {1 }' .format (repr (label ), idx ))
71+ raise IDNABidiError ('Unknown directionality in label {} at position {}' .format (repr (label ), idx ))
7672 if direction in ['R' , 'AL' , 'AN' ]:
7773 bidi_label = True
7874 if not bidi_label and not check_ltr :
@@ -85,7 +81,7 @@ def check_bidi(label, check_ltr=False):
8581 elif direction == 'L' :
8682 rtl = False
8783 else :
88- raise IDNABidiError ('First codepoint in label {0 } must be directionality L, R or AL' .format (repr (label )))
84+ raise IDNABidiError ('First codepoint in label {} must be directionality L, R or AL' .format (repr (label )))
8985
9086 valid_ending = False
9187 number_type = False
@@ -95,7 +91,7 @@ def check_bidi(label, check_ltr=False):
9591 if rtl :
9692 # Bidi rule 2
9793 if not direction in ['R' , 'AL' , 'AN' , 'EN' , 'ES' , 'CS' , 'ET' , 'ON' , 'BN' , 'NSM' ]:
98- raise IDNABidiError ('Invalid direction for codepoint at position {0 } in a right-to-left label' .format (idx ))
94+ raise IDNABidiError ('Invalid direction for codepoint at position {} in a right-to-left label' .format (idx ))
9995 # Bidi rule 3
10096 if direction in ['R' , 'AL' , 'EN' , 'AN' ]:
10197 valid_ending = True
@@ -111,7 +107,7 @@ def check_bidi(label, check_ltr=False):
111107 else :
112108 # Bidi rule 5
113109 if not direction in ['L' , 'EN' , 'ES' , 'CS' , 'ET' , 'ON' , 'BN' , 'NSM' ]:
114- raise IDNABidiError ('Invalid direction for codepoint at position {0 } in a left-to-right label' .format (idx ))
110+ raise IDNABidiError ('Invalid direction for codepoint at position {} in a left-to-right label' .format (idx ))
115111 # Bidi rule 6
116112 if direction in ['L' , 'EN' ]:
117113 valid_ending = True
@@ -212,7 +208,7 @@ def valid_contexto(label, pos, exception=False):
212208
213209 elif cp_value == 0x30fb :
214210 for cp in label :
215- if cp == u '\u30fb ' :
211+ if cp == '\u30fb ' :
216212 continue
217213 if _is_script (cp , 'Hiragana' ) or _is_script (cp , 'Katakana' ) or _is_script (cp , 'Han' ):
218214 return True
@@ -249,16 +245,16 @@ def check_label(label):
249245 elif intranges_contain (cp_value , idnadata .codepoint_classes ['CONTEXTJ' ]):
250246 try :
251247 if not valid_contextj (label , pos ):
252- raise InvalidCodepointContext ('Joiner {0 } not allowed at position {1 } in {2 }' .format (
248+ raise InvalidCodepointContext ('Joiner {} not allowed at position {} in {}' .format (
253249 _unot (cp_value ), pos + 1 , repr (label )))
254250 except ValueError :
255- raise IDNAError ('Unknown codepoint adjacent to joiner {0 } at position {1 } in {2 }' .format (
251+ raise IDNAError ('Unknown codepoint adjacent to joiner {} at position {} in {}' .format (
256252 _unot (cp_value ), pos + 1 , repr (label )))
257253 elif intranges_contain (cp_value , idnadata .codepoint_classes ['CONTEXTO' ]):
258254 if not valid_contexto (label , pos ):
259- raise InvalidCodepointContext ('Codepoint {0 } not allowed at position {1 } in {2 }' .format (_unot (cp_value ), pos + 1 , repr (label )))
255+ raise InvalidCodepointContext ('Codepoint {} not allowed at position {} in {}' .format (_unot (cp_value ), pos + 1 , repr (label )))
260256 else :
261- raise InvalidCodepoint ('Codepoint {0 } at position {1 } of {2 } not allowed' .format (_unot (cp_value ), pos + 1 , repr (label )))
257+ raise InvalidCodepoint ('Codepoint {} at position {} of {} not allowed' .format (_unot (cp_value ), pos + 1 , repr (label )))
262258
263259 check_bidi (label )
264260
@@ -277,7 +273,7 @@ def alabel(label):
277273 if not label :
278274 raise IDNAError ('No Input' )
279275
280- label = unicode (label )
276+ label = str (label )
281277 check_label (label )
282278 label = _punycode (label )
283279 label = _alabel_prefix + label
@@ -316,35 +312,35 @@ def ulabel(label):
316312def uts46_remap (domain , std3_rules = True , transitional = False ):
317313 """Re-map the characters in the string according to UTS46 processing."""
318314 from .uts46data import uts46data
319- output = u""
315+ output = ''
320316 try :
321317 for pos , char in enumerate (domain ):
322318 code_point = ord (char )
323319 uts46row = uts46data [code_point if code_point < 256 else
324- bisect .bisect_left (uts46data , (code_point , "Z" )) - 1 ]
320+ bisect .bisect_left (uts46data , (code_point , 'Z' )) - 1 ]
325321 status = uts46row [1 ]
326322 replacement = uts46row [2 ] if len (uts46row ) == 3 else None
327- if (status == "V" or
328- (status == "D" and not transitional ) or
329- (status == "3" and not std3_rules and replacement is None )):
323+ if (status == 'V' or
324+ (status == 'D' and not transitional ) or
325+ (status == '3' and not std3_rules and replacement is None )):
330326 output += char
331- elif replacement is not None and (status == "M" or
332- (status == "3" and not std3_rules ) or
333- (status == "D" and transitional )):
327+ elif replacement is not None and (status == 'M' or
328+ (status == '3' and not std3_rules ) or
329+ (status == 'D' and transitional )):
334330 output += replacement
335- elif status != "I" :
331+ elif status != 'I' :
336332 raise IndexError ()
337- return unicodedata .normalize (" NFC" , output )
333+ return unicodedata .normalize (' NFC' , output )
338334 except IndexError :
339335 raise InvalidCodepoint (
340- " Codepoint {0 } not allowed at position {1 } in {2}" .format (
336+ ' Codepoint {} not allowed at position {} in {}' .format (
341337 _unot (code_point ), pos + 1 , repr (domain )))
342338
343339
344340def encode (s , strict = False , uts46 = False , std3_rules = False , transitional = False ):
345341
346342 if isinstance (s , (bytes , bytearray )):
347- s = s .decode (" ascii" )
343+ s = s .decode (' ascii' )
348344 if uts46 :
349345 s = uts46_remap (s , std3_rules , transitional )
350346 trailing_dot = False
@@ -375,15 +371,15 @@ def encode(s, strict=False, uts46=False, std3_rules=False, transitional=False):
375371def decode (s , strict = False , uts46 = False , std3_rules = False ):
376372
377373 if isinstance (s , (bytes , bytearray )):
378- s = s .decode (" ascii" )
374+ s = s .decode (' ascii' )
379375 if uts46 :
380376 s = uts46_remap (s , std3_rules , False )
381377 trailing_dot = False
382378 result = []
383379 if not strict :
384380 labels = _unicode_dots_re .split (s )
385381 else :
386- labels = s .split (u '.' )
382+ labels = s .split ('.' )
387383 if not labels or labels == ['' ]:
388384 raise IDNAError ('Empty domain' )
389385 if not labels [- 1 ]:
@@ -396,5 +392,5 @@ def decode(s, strict=False, uts46=False, std3_rules=False):
396392 else :
397393 raise IDNAError ('Empty label' )
398394 if trailing_dot :
399- result .append (u '' )
400- return u '.' .join (result )
395+ result .append ('' )
396+ return '.' .join (result )
0 commit comments