From f5818e68e175693cfe6cc9453ab69829da95d612 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 23 Nov 2021 18:19:45 +0100 Subject: [PATCH 1/4] bpo-45873: Restore Python 3.6 compatibility --- Tools/scripts/deepfreeze.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py index b840c4b51d73a3..f328817231e37a 100644 --- a/Tools/scripts/deepfreeze.py +++ b/Tools/scripts/deepfreeze.py @@ -5,18 +5,37 @@ import contextlib import os import re +import sys import time import types +import unicodedata from typing import Dict, FrozenSet, Tuple, TextIO import umarshal verbose = False +# 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: + return False + for c in s: + if unicodedata.category(c) in NON_PRINTABLE: + return False + return True + def make_string_literal(b: bytes) -> str: res = ['"'] - if b.isascii() and b.decode("ascii").isprintable(): + if isprintable(b): res.append(b.decode("ascii").replace("\\", "\\\\").replace("\"", "\\\"")) else: for i in b: From 055b662a4d774d52b7fc873406ffeaf57d282b46 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 23 Nov 2021 19:04:02 +0100 Subject: [PATCH 2/4] Make Python for regen check more verbose --- configure | 12 +++++++++++- configure.ac | 11 ++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/configure b/configure index 0684921f41a42f..711b6d2805f16d 100755 --- a/configure +++ b/configure @@ -3142,7 +3142,7 @@ case $host_os in *\ *) host_os=`echo "$host_os" | sed 's/ /-/g'`;; esac # pybuilddir.txt will be created by --generate-posix-vars in the Makefile rm -f pybuilddir.txt -for ac_prog in python$PACKAGE_VERSION python3 python +for ac_prog in python$PACKAGE_VERSION python3.10 python3.9 python3.8 python3.7 python3.6 python3 python do # Extract the first word of "$ac_prog", so it can be a program name with args. set dummy $ac_prog; ac_word=$2 @@ -3187,6 +3187,16 @@ test -n "$PYTHON_FOR_REGEN" || PYTHON_FOR_REGEN="python3" +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking Python for regen version" >&5 +$as_echo_n "checking Python for regen version... " >&6; } +if command -v $PYTHON_FOR_REGEN >/dev/null 2>&1; then + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $($PYTHON_FOR_REGEN -V 2>/dev/null)" >&5 +$as_echo "$($PYTHON_FOR_REGEN -V 2>/dev/null)" >&6; } +else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: missing" >&5 +$as_echo "missing" >&6; } +fi + if test "$cross_compiling" = yes; then { $as_echo "$as_me:${as_lineno-$LINENO}: checking for python interpreter for cross build" >&5 $as_echo_n "checking for python interpreter for cross build... " >&6; } diff --git a/configure.ac b/configure.ac index ba22418b40694b..e539d3bb87d662 100644 --- a/configure.ac +++ b/configure.ac @@ -96,9 +96,18 @@ AC_SUBST(host) # pybuilddir.txt will be created by --generate-posix-vars in the Makefile rm -f pybuilddir.txt -AC_CHECK_PROGS(PYTHON_FOR_REGEN, python$PACKAGE_VERSION python3 python, python3) +AC_CHECK_PROGS([PYTHON_FOR_REGEN], + [python$PACKAGE_VERSION python3.10 python3.9 python3.8 python3.7 python3.6 python3 python], + [python3]) AC_SUBST(PYTHON_FOR_REGEN) +AC_MSG_CHECKING([Python for regen version]) +if command -v $PYTHON_FOR_REGEN >/dev/null 2>&1; then + AC_MSG_RESULT([$($PYTHON_FOR_REGEN -V 2>/dev/null)]) +else + AC_MSG_RESULT([missing]) +fi + if test "$cross_compiling" = yes; then AC_MSG_CHECKING([for python interpreter for cross build]) if test -z "$PYTHON_FOR_BUILD"; then From cb24cf596fdb5a8fab82cc790a4729bd79bebc3b Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 23 Nov 2021 19:09:06 +0100 Subject: [PATCH 3/4] Always use the slow path for isprintable --- Tools/scripts/deepfreeze.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py index f328817231e37a..85c385c9f9f67b 100644 --- a/Tools/scripts/deepfreeze.py +++ b/Tools/scripts/deepfreeze.py @@ -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: From 35bd38f4ddda3940c0ec3f1e62c3d06e9c315196 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 23 Nov 2021 21:48:51 +0200 Subject: [PATCH 4/4] Use trivial ASCII printable character test Co-authored-by: Guido van Rossum --- Tools/scripts/deepfreeze.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py index 85c385c9f9f67b..e51a408ae7f6ac 100644 --- a/Tools/scripts/deepfreeze.py +++ b/Tools/scripts/deepfreeze.py @@ -15,23 +15,9 @@ verbose = False -# See Objects/unicodectype.c:_PyUnicode_IsPrintable() -NON_PRINTABLE = {"Cc", "Cf", "Cs", "Co", "Cn", "Zl", "Zp", "Zs"} - def isprintable(b: bytes) -> bool: - """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 - return True + return all(0x20 <= c < 0x7f for c in b) def make_string_literal(b: bytes) -> str: