Update idna==3.6

This commit is contained in:
JonnyWong16 2024-03-30 15:26:50 -07:00
commit c074a961c4
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
5 changed files with 256 additions and 253 deletions

View file

@ -1,7 +1,7 @@
from .core import encode, decode, alabel, ulabel, IDNAError from .core import encode, decode, alabel, ulabel, IDNAError
import codecs import codecs
import re import re
from typing import Tuple, Optional from typing import Any, Tuple, Optional
_unicode_dots_re = re.compile('[\u002e\u3002\uff0e\uff61]') _unicode_dots_re = re.compile('[\u002e\u3002\uff0e\uff61]')
@ -26,24 +26,24 @@ class Codec(codecs.Codec):
return decode(data), len(data) return decode(data), len(data)
class IncrementalEncoder(codecs.BufferedIncrementalEncoder): class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
def _buffer_encode(self, data: str, errors: str, final: bool) -> Tuple[str, int]: # type: ignore def _buffer_encode(self, data: str, errors: str, final: bool) -> Tuple[bytes, int]:
if errors != 'strict': if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors)) raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if not data: if not data:
return "", 0 return b'', 0
labels = _unicode_dots_re.split(data) labels = _unicode_dots_re.split(data)
trailing_dot = '' trailing_dot = b''
if labels: if labels:
if not labels[-1]: if not labels[-1]:
trailing_dot = '.' trailing_dot = b'.'
del labels[-1] del labels[-1]
elif not final: elif not final:
# Keep potentially unfinished label until the next call # Keep potentially unfinished label until the next call
del labels[-1] del labels[-1]
if labels: if labels:
trailing_dot = '.' trailing_dot = b'.'
result = [] result = []
size = 0 size = 0
@ -54,18 +54,21 @@ class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
size += len(label) size += len(label)
# Join with U+002E # Join with U+002E
result_str = '.'.join(result) + trailing_dot # type: ignore result_bytes = b'.'.join(result) + trailing_dot
size += len(trailing_dot) size += len(trailing_dot)
return result_str, size return result_bytes, size
class IncrementalDecoder(codecs.BufferedIncrementalDecoder): class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
def _buffer_decode(self, data: str, errors: str, final: bool) -> Tuple[str, int]: # type: ignore def _buffer_decode(self, data: Any, errors: str, final: bool) -> Tuple[str, int]:
if errors != 'strict': if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors)) raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if not data: if not data:
return ('', 0) return ('', 0)
if not isinstance(data, str):
data = str(data, 'ascii')
labels = _unicode_dots_re.split(data) labels = _unicode_dots_re.split(data)
trailing_dot = '' trailing_dot = ''
if labels: if labels:
@ -99,14 +102,17 @@ class StreamReader(Codec, codecs.StreamReader):
pass pass
def getregentry() -> codecs.CodecInfo: def search_function(name: str) -> Optional[codecs.CodecInfo]:
# Compatibility as a search_function for codecs.register() if name != 'idna2008':
return None
return codecs.CodecInfo( return codecs.CodecInfo(
name='idna', name=name,
encode=Codec().encode, # type: ignore encode=Codec().encode,
decode=Codec().decode, # type: ignore decode=Codec().decode,
incrementalencoder=IncrementalEncoder, incrementalencoder=IncrementalEncoder,
incrementaldecoder=IncrementalDecoder, incrementaldecoder=IncrementalDecoder,
streamwriter=StreamWriter, streamwriter=StreamWriter,
streamreader=StreamReader, streamreader=StreamReader,
) )
codecs.register(search_function)

View file

@ -318,7 +318,7 @@ def uts46_remap(domain: str, std3_rules: bool = True, transitional: bool = False
status = uts46row[1] status = uts46row[1]
replacement = None # type: Optional[str] replacement = None # type: Optional[str]
if len(uts46row) == 3: if len(uts46row) == 3:
replacement = uts46row[2] # type: ignore replacement = uts46row[2]
if (status == 'V' or if (status == 'V' or
(status == 'D' and not transitional) or (status == 'D' and not transitional) or
(status == '3' and not std3_rules and replacement is None)): (status == '3' and not std3_rules and replacement is None)):
@ -338,9 +338,9 @@ def uts46_remap(domain: str, std3_rules: bool = True, transitional: bool = False
def encode(s: Union[str, bytes, bytearray], strict: bool = False, uts46: bool = False, std3_rules: bool = False, transitional: bool = False) -> bytes: def encode(s: Union[str, bytes, bytearray], strict: bool = False, uts46: bool = False, std3_rules: bool = False, transitional: bool = False) -> bytes:
if isinstance(s, (bytes, bytearray)): if not isinstance(s, str):
try: try:
s = s.decode('ascii') s = str(s, 'ascii')
except UnicodeDecodeError: except UnicodeDecodeError:
raise IDNAError('should pass a unicode string to the function rather than a byte string.') raise IDNAError('should pass a unicode string to the function rather than a byte string.')
if uts46: if uts46:
@ -372,8 +372,8 @@ def encode(s: Union[str, bytes, bytearray], strict: bool = False, uts46: bool =
def decode(s: Union[str, bytes, bytearray], strict: bool = False, uts46: bool = False, std3_rules: bool = False) -> str: def decode(s: Union[str, bytes, bytearray], strict: bool = False, uts46: bool = False, std3_rules: bool = False) -> str:
try: try:
if isinstance(s, (bytes, bytearray)): if not isinstance(s, str):
s = s.decode('ascii') s = str(s, 'ascii')
except UnicodeDecodeError: except UnicodeDecodeError:
raise IDNAError('Invalid ASCII in A-label') raise IDNAError('Invalid ASCII in A-label')
if uts46: if uts46:

View file

@ -1,6 +1,6 @@
# This file is automatically generated by tools/idna-data # This file is automatically generated by tools/idna-data
__version__ = '15.0.0' __version__ = '15.1.0'
scripts = { scripts = {
'Greek': ( 'Greek': (
0x37000000374, 0x37000000374,
@ -59,6 +59,7 @@ scripts = {
0x2b7400002b81e, 0x2b7400002b81e,
0x2b8200002cea2, 0x2b8200002cea2,
0x2ceb00002ebe1, 0x2ceb00002ebe1,
0x2ebf00002ee5e,
0x2f8000002fa1e, 0x2f8000002fa1e,
0x300000003134b, 0x300000003134b,
0x31350000323b0, 0x31350000323b0,
@ -1834,7 +1835,6 @@ codepoint_classes = {
0xa7d50000a7d6, 0xa7d50000a7d6,
0xa7d70000a7d8, 0xa7d70000a7d8,
0xa7d90000a7da, 0xa7d90000a7da,
0xa7f20000a7f5,
0xa7f60000a7f8, 0xa7f60000a7f8,
0xa7fa0000a828, 0xa7fa0000a828,
0xa82c0000a82d, 0xa82c0000a82d,
@ -1907,9 +1907,7 @@ codepoint_classes = {
0x1060000010737, 0x1060000010737,
0x1074000010756, 0x1074000010756,
0x1076000010768, 0x1076000010768,
0x1078000010786, 0x1078000010781,
0x10787000107b1,
0x107b2000107bb,
0x1080000010806, 0x1080000010806,
0x1080800010809, 0x1080800010809,
0x1080a00010836, 0x1080a00010836,
@ -2134,6 +2132,7 @@ codepoint_classes = {
0x2b7400002b81e, 0x2b7400002b81e,
0x2b8200002cea2, 0x2b8200002cea2,
0x2ceb00002ebe1, 0x2ceb00002ebe1,
0x2ebf00002ee5e,
0x300000003134b, 0x300000003134b,
0x31350000323b0, 0x31350000323b0,
), ),

View file

@ -1,2 +1,2 @@
__version__ = '3.4' __version__ = '3.6'

File diff suppressed because it is too large Load diff