Skip to content

Commit f5818e6

Browse files
committed
bpo-45873: Restore Python 3.6 compatibility
1 parent 5be98e5 commit f5818e6

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Tools/scripts/deepfreeze.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,37 @@
55
import contextlib
66
import os
77
import re
8+
import sys
89
import time
910
import types
11+
import unicodedata
1012
from typing import Dict, FrozenSet, Tuple, TextIO
1113

1214
import umarshal
1315

1416
verbose = False
1517

18+
# See Objects/unicodectype.c:_PyUnicode_IsPrintable
19+
NON_PRINTABLE = {"Cc", "Cf", "Cs", "Co", "Cn", "Zl", "Zp", "Zs"}
20+
21+
22+
def isprintable(b: bytes) -> bool:
23+
if sys.version_info > (3, 7):
24+
return b.isascii() and b.decode("ascii").isprintable()
25+
else:
26+
try:
27+
s = b.decode("ascii")
28+
except UnicodeDecodeError:
29+
return False
30+
for c in s:
31+
if unicodedata.category(c) in NON_PRINTABLE:
32+
return False
33+
return True
34+
1635

1736
def make_string_literal(b: bytes) -> str:
1837
res = ['"']
19-
if b.isascii() and b.decode("ascii").isprintable():
38+
if isprintable(b):
2039
res.append(b.decode("ascii").replace("\\", "\\\\").replace("\"", "\\\""))
2140
else:
2241
for i in b:

0 commit comments

Comments
 (0)