Update idna-3.3

This commit is contained in:
JonnyWong16 2021-10-14 20:59:44 -07:00
parent 2f1a08009f
commit a3bfabb5f6
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
9 changed files with 7591 additions and 6113 deletions

View file

@ -1,41 +1,40 @@
from .core import encode, decode, alabel, ulabel, IDNAError
import codecs
import re
from typing import Tuple, Optional
_unicode_dots_re = re.compile(u'[\u002e\u3002\uff0e\uff61]')
_unicode_dots_re = re.compile('[\u002e\u3002\uff0e\uff61]')
class Codec(codecs.Codec):
def encode(self, data, errors='strict'):
def encode(self, data: str, errors: str = 'strict') -> Tuple[bytes, int]:
if errors != 'strict':
raise IDNAError("Unsupported error handling \"{0}\"".format(errors))
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if not data:
return "", 0
return b"", 0
return encode(data), len(data)
def decode(self, data, errors='strict'):
def decode(self, data: bytes, errors: str = 'strict') -> Tuple[str, int]:
if errors != 'strict':
raise IDNAError("Unsupported error handling \"{0}\"".format(errors))
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if not data:
return u"", 0
return '', 0
return decode(data), len(data)
class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
def _buffer_encode(self, data, errors, final):
def _buffer_encode(self, data: str, errors: str, final: bool) -> Tuple[str, int]: # type: ignore
if errors != 'strict':
raise IDNAError("Unsupported error handling \"{0}\"".format(errors))
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if not data:
return ("", 0)
return "", 0
labels = _unicode_dots_re.split(data)
trailing_dot = u''
trailing_dot = ''
if labels:
if not labels[-1]:
trailing_dot = '.'
@ -55,37 +54,29 @@ class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
size += len(label)
# Join with U+002E
result = ".".join(result) + trailing_dot
result_str = '.'.join(result) + trailing_dot # type: ignore
size += len(trailing_dot)
return (result, size)
return result_str, size
class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
def _buffer_decode(self, data, errors, final):
def _buffer_decode(self, data: str, errors: str, final: bool) -> Tuple[str, int]: # type: ignore
if errors != 'strict':
raise IDNAError("Unsupported error handling \"{0}\"".format(errors))
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if not data:
return (u"", 0)
return ('', 0)
# IDNA allows decoding to operate on Unicode strings, too.
if isinstance(data, unicode):
labels = _unicode_dots_re.split(data)
else:
# Must be ASCII string
data = str(data)
unicode(data, "ascii")
labels = data.split(".")
trailing_dot = u''
labels = _unicode_dots_re.split(data)
trailing_dot = ''
if labels:
if not labels[-1]:
trailing_dot = u'.'
trailing_dot = '.'
del labels[-1]
elif not final:
# Keep potentially unfinished label until the next call
del labels[-1]
if labels:
trailing_dot = u'.'
trailing_dot = '.'
result = []
size = 0
@ -95,22 +86,25 @@ class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
size += 1
size += len(label)
result = u".".join(result) + trailing_dot
result_str = '.'.join(result) + trailing_dot
size += len(trailing_dot)
return (result, size)
return (result_str, size)
class StreamWriter(Codec, codecs.StreamWriter):
pass
class StreamReader(Codec, codecs.StreamReader):
pass
def getregentry():
def getregentry() -> codecs.CodecInfo:
# Compatibility as a search_function for codecs.register()
return codecs.CodecInfo(
name='idna',
encode=Codec().encode,
decode=Codec().decode,
encode=Codec().encode, # type: ignore
decode=Codec().decode, # type: ignore
incrementalencoder=IncrementalEncoder,
incrementaldecoder=IncrementalDecoder,
streamwriter=StreamWriter,