Skip to content

Commit

Permalink
python 3.6 compatibility: reimplement str.isascii()
Browse files Browse the repository at this point in the history
  • Loading branch information
Elia Robyn Speer committed Oct 5, 2021
1 parent 0ed9adf commit 205785c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions langcodes/tag_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
...
langcodes.tag_parser.LanguageTagError: Language tags must be made of ASCII characters
"""
from __future__ import print_function, unicode_literals

# These tags should not be parsed by the usual parser; they're grandfathered
# in from RFC 3066. The 'irregular' ones don't fit the syntax at all; the
Expand Down Expand Up @@ -147,6 +146,18 @@
]


def _is_ascii(s):
"""
Determine whether a tag consists of ASCII characters.
"""
# When Python 3.6 support is dropped, we can replace this with str.isascii().
try:
s.encode('ascii')
return True
except UnicodeEncodeError:
return False


def normalize_characters(tag):
"""
BCP 47 is case-insensitive, and CLDR's use of it considers underscores
Expand All @@ -167,7 +178,7 @@ def parse_tag(tag):
registry, yet. Returns a list of (type, value) tuples indicating what
information will need to be looked up.
"""
if not tag.isascii():
if not _is_ascii(tag):
raise LanguageTagError("Language tags must be made of ASCII characters")

tag = normalize_characters(tag)
Expand Down

0 comments on commit 205785c

Please sign in to comment.