Some limited support for 3.8 "=" specifier

This commit is contained in:
rocky
2022-07-06 13:00:52 -04:00
parent cc4ea47d24
commit 5a4136a7f6
9 changed files with 248 additions and 5 deletions

View File

@@ -24,6 +24,8 @@ from uncompyle6.semantics.consts import (
from uncompyle6.semantics.helper import flatten_list
FSTRING_CONVERSION_MAP = {1: "!s", 2: "!r", 3: "!a", "X": ":X"}
#######################
def customize_for_version37(self, version):
########################
@@ -39,7 +41,9 @@ def customize_for_version37(self, version):
PRECEDENCE["call_ex_kw4"] = 1
PRECEDENCE["call_kw"] = 0
PRECEDENCE["call_kw36"] = 1
PRECEDENCE["formatted_value1"] = 100
PRECEDENCE["formatted_value1"] = 38 # f"...". This has to be below "named_expr" to make
# f'{(x := 10)}' preserve parenthesis
PRECEDENCE["formatted_value2"] = 38 # See above
PRECEDENCE["if_exp_37a"] = 28
PRECEDENCE["if_exp_37b"] = 28
PRECEDENCE["dict_unpack"] = 0 # **{...}

View File

@@ -20,6 +20,8 @@
#######################
from uncompyle6.semantics.consts import PRECEDENCE, TABLE_DIRECT
from uncompyle6.semantics.customize37 import FSTRING_CONVERSION_MAP
from uncompyle6.semantics.helper import escape_string, strip_quotes
def customize_for_version38(self, version):
@@ -125,7 +127,7 @@ def customize_for_version38(self, version):
"set_for": (" for %c in %c", (2, "store"), (0, "expr_or_arg"),),
"whilestmt38": (
"%|while %c:\n%+%c%-\n\n",
(1, "testexpr"),
(1, ("bool_op", "testexpr", "testexprc")),
(2, ("l_stmts", "pass")),
),
"whileTruestmt38": (
@@ -282,6 +284,54 @@ def customize_for_version38(self, version):
self.n_set_afor = n_set_afor
def n_formatted_value_debug(node):
p = self.prec
self.prec = 100
formatted_value = node[1]
value_equal = node[0].attr
assert formatted_value.kind.startswith("formatted_value")
old_in_format_string = self.in_format_string
self.in_format_string = formatted_value.kind
format_value_attr = node[-1]
post_str = ""
if node[-1] == "BUILD_STRING_3":
post_load_str = node[-2]
post_str = self.traverse(post_load_str, indent="")
post_str = strip_quotes(post_str)
if format_value_attr == "FORMAT_VALUE_ATTR":
attr = format_value_attr.attr
if attr & 4:
fmt = strip_quotes(self.traverse(node[3], indent=""))
attr_flags = attr & 3
if attr_flags:
conversion = "%s:%s" % (
FSTRING_CONVERSION_MAP.get(attr_flags, ""),
fmt,
)
else:
conversion = ":%s" % fmt
else:
conversion = FSTRING_CONVERSION_MAP.get(attr, "")
f_str = "f%s" % escape_string(
"{%s%s}%s" % (value_equal, conversion, post_str)
)
else:
f_conversion = self.traverse(formatted_value, indent="")
# Remove leaving "f" and quotes
conversion = strip_quotes(f_conversion[1:])
f_str = "f%s" % escape_string(f"{value_equal}{conversion}" + post_str)
self.write(f_str)
self.in_format_string = old_in_format_string
self.prec = p
self.prune()
self.n_formatted_value_debug = n_formatted_value_debug
def n_suite_stmts_return(node):
if len(node) > 1:
assert len(node) == 2