Skip to content

Commit

Permalink
Swap out is_all_ascii for built-in isascii
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Nov 30, 2024
1 parent 57a82a0 commit ae4b8af
Showing 1 changed file with 7 additions and 10 deletions.
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

0 comments on commit ae4b8af

Please sign in to comment.