Skip to content

Commit

Permalink
Always use the slow path for isprintable
Browse files Browse the repository at this point in the history
  • Loading branch information
tiran committed Nov 23, 2021
1 parent 055b662 commit cb24cf5
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions Tools/scripts/deepfreeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@

verbose = False

# See Objects/unicodectype.c:_PyUnicode_IsPrintable
# See Objects/unicodectype.c:_PyUnicode_IsPrintable()
NON_PRINTABLE = {"Cc", "Cf", "Cs", "Co", "Cn", "Zl", "Zp", "Zs"}


def isprintable(b: bytes) -> bool:
if sys.version_info > (3, 7):
return b.isascii() and b.decode("ascii").isprintable()
else:
try:
s = b.decode("ascii")
except UnicodeDecodeError:
"""isascii() and isprintable() for Python 3.6
return b.isascii() and b.decode("ascii").isprintable()
"""
try:
s = b.decode("ascii")
except UnicodeDecodeError:
return False
for c in s:
if unicodedata.category(c) in NON_PRINTABLE:
return False
for c in s:
if unicodedata.category(c) in NON_PRINTABLE:
return False
return True
return True


def make_string_literal(b: bytes) -> str:
Expand Down

0 comments on commit cb24cf5

Please sign in to comment.