Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code #173

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ci:
autoupdate_schedule: "quarterly"

default_language_version:
python: "python3.12"

repos:
- repo: "meta"
hooks:
- id: "check-hooks-apply"
- id: "check-useless-excludes"

- repo: "https://github.com/psf/black-pre-commit-mirror"
rev: "24.3.0"
hooks:
- id: "black"

- repo: "https://github.com/pycqa/isort"
rev: "5.13.2"
hooks:
- id: "isort"
2 changes: 1 addition & 1 deletion idna/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .package_data import __version__
from .core import (
IDNABidiError,
IDNAError,
Expand All @@ -20,6 +19,7 @@
valid_string_length,
)
from .intranges import intranges_contain
from .package_data import __version__

__all__ = [
"IDNABidiError",
Expand Down
57 changes: 31 additions & 26 deletions idna/codec.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
from .core import encode, decode, alabel, ulabel, IDNAError
import codecs
import re
from typing import Any, Tuple, Optional
from typing import Any, Optional, Tuple

from .core import IDNAError, alabel, decode, encode, ulabel

_unicode_dots_re = re.compile("[\u002e\u3002\uff0e\uff61]")

_unicode_dots_re = re.compile('[\u002e\u3002\uff0e\uff61]')

class Codec(codecs.Codec):

def encode(self, data: str, errors: str = 'strict') -> Tuple[bytes, int]:
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
def encode(self, data: str, errors: str = "strict") -> Tuple[bytes, int]:
if errors != "strict":
raise IDNAError('Unsupported error handling "{}"'.format(errors))

if not data:
return b"", 0

return encode(data), len(data)

def decode(self, data: bytes, errors: str = 'strict') -> Tuple[str, int]:
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
def decode(self, data: bytes, errors: str = "strict") -> Tuple[str, int]:
if errors != "strict":
raise IDNAError('Unsupported error handling "{}"'.format(errors))

if not data:
return '', 0
return "", 0

return decode(data), len(data)


class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
def _buffer_encode(self, data: str, errors: str, final: bool) -> Tuple[bytes, int]:
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if errors != "strict":
raise IDNAError('Unsupported error handling "{}"'.format(errors))

if not data:
return b'', 0
return b"", 0

labels = _unicode_dots_re.split(data)
trailing_dot = b''
trailing_dot = b""
if labels:
if not labels[-1]:
trailing_dot = b'.'
trailing_dot = b"."
del labels[-1]
elif not final:
# Keep potentially unfinished label until the next call
del labels[-1]
if labels:
trailing_dot = b'.'
trailing_dot = b"."

result = []
size = 0
Expand All @@ -54,32 +57,33 @@ def _buffer_encode(self, data: str, errors: str, final: bool) -> Tuple[bytes, in
size += len(label)

# Join with U+002E
result_bytes = b'.'.join(result) + trailing_dot
result_bytes = b".".join(result) + trailing_dot
size += len(trailing_dot)
return result_bytes, size


class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
def _buffer_decode(self, data: Any, errors: str, final: bool) -> Tuple[str, int]:
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if errors != "strict":
raise IDNAError('Unsupported error handling "{}"'.format(errors))

if not data:
return ('', 0)
return ("", 0)

if not isinstance(data, str):
data = str(data, 'ascii')
data = str(data, "ascii")

labels = _unicode_dots_re.split(data)
trailing_dot = ''
trailing_dot = ""
if labels:
if not labels[-1]:
trailing_dot = '.'
trailing_dot = "."
del labels[-1]
elif not final:
# Keep potentially unfinished label until the next call
del labels[-1]
if labels:
trailing_dot = '.'
trailing_dot = "."

result = []
size = 0
Expand All @@ -89,7 +93,7 @@ def _buffer_decode(self, data: Any, errors: str, final: bool) -> Tuple[str, int]
size += 1
size += len(label)

result_str = '.'.join(result) + trailing_dot
result_str = ".".join(result) + trailing_dot
size += len(trailing_dot)
return (result_str, size)

Expand All @@ -103,7 +107,7 @@ class StreamReader(Codec, codecs.StreamReader):


def search_function(name: str) -> Optional[codecs.CodecInfo]:
if name != 'idna2008':
if name != "idna2008":
return None
return codecs.CodecInfo(
name=name,
Expand All @@ -115,4 +119,5 @@ def search_function(name: str) -> Optional[codecs.CodecInfo]:
streamreader=StreamReader,
)


codecs.register(search_function)
11 changes: 7 additions & 4 deletions idna/compat.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from .core import *
from .codec import *
from typing import Any, Union

from .codec import *
from .core import *


def ToASCII(label: str) -> bytes:
return encode(label)


def ToUnicode(label: Union[bytes, bytearray]) -> str:
return decode(label)

def nameprep(s: Any) -> None:
raise NotImplementedError('IDNA 2008 does not utilise nameprep protocol')

def nameprep(s: Any) -> None:
raise NotImplementedError("IDNA 2008 does not utilise nameprep protocol")