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

Swap out is_all_ascii for built-in str.isascii #209

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/pycares/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

from typing import Union

try:
import idna as idna2008
except ImportError:
Expand All @@ -24,25 +26,20 @@ def maybe_str(data):
raise TypeError('only str (ascii encoding) and bytes are supported')


def is_all_ascii(text):
for c in text:
if ord(c) > 0x7f:
return False
return True

def parse_name_idna2008(name):
def parse_name_idna2008(name: str) -> str:
parts = name.split('.')
r = []
for part in parts:
if is_all_ascii(part):
if part.isascii():
r.append(part.encode('ascii'))
else:
r.append(idna2008.encode(part))
return b'.'.join(r)

def parse_name(name):

def parse_name(name: Union[str, bytes]) -> bytes:
if isinstance(name, str):
if is_all_ascii(name):
if name.isascii():
return name.encode('ascii')
if idna2008 is not None:
return parse_name_idna2008(name)
Expand Down
Loading