Correct long-literals for Python 2.7

This commit is contained in:
rocky 2022-10-16 19:33:51 -04:00
parent bb9b9fb4b3
commit 5b3ea47bac
8 changed files with 26 additions and 11 deletions

View File

@ -161,7 +161,9 @@ x = {
"return": 12,
}
assert tuple(x.keys()) == ("b", "c", "e", "g", "h", "j", "k", "return")
# We need sorted here and below, because x.keys() in 2.7 comes out in the reverse order.
# Go figure.
assert sorted(x.keys()) == ["b", "c", "e", "g", "h", "j", "k", "return"]
# Ensure that in dictionary we produce integers, not strings
x = {1: 2, 3: 4}
@ -675,7 +677,12 @@ values = {
"value502": 502 + 1,
}
assert list(values.values())[1:] == list(range(3, 502 + 2))
import sys
if sys.version < (3, 0):
# Python 2.7 is funky with values.values() ordering
assert sorted(values.values())[1:-2] == list(range(4, 502 + 1))
else:
assert list(values.values())[1:] == list(range(3, 502 + 2))
# Try a long dictionary that fails because we have a binary op.
# We can get a expr32 grouping speedup
@ -717,4 +724,4 @@ values = {
"value33": 33,
}
assert list(values.values())[1:] == list(range(2, 34))
assert sorted(values.values())[1:] == list(range(2, 34))

View File

@ -99,7 +99,7 @@ class PythonParser(GenericASTBuilder):
# so on but that would require major changes to the
# semantic actions
self.singleton = frozenset(
("str", "store", "_stmts", "suite_stmts_opt", "inplace_op")
("str", "store", "_stmts", "suite_stmts_opt", "inplace_op", "add_value")
)
# Instructions filled in from scanner
self.insts = []

View File

@ -315,7 +315,9 @@ class Python2Parser(PythonParser):
if opname in ("BUILD_CONST_LIST", "BUILD_CONST_SET"):
rule = (
"""
add_consts ::= ADD_VALUE*
add_consts ::= add_value+
add_value ::= ADD_VALUE
add_value ::= ADD_VALUE_VAR
const_list ::= COLLECTION_START add_consts %s
expr ::= const_list
"""

View File

@ -171,10 +171,14 @@ class Scanner(object):
has_extended_arg=False,
)
)
if tokens[j] == "LOAD_CONST":
opname = "ADD_VALUE"
else:
opname = "ADD_VALUE_VAR"
for j in range(collection_start, i):
new_tokens.append(
Token(
opname="ADD_VALUE",
opname=opname,
attr=tokens[j].attr,
pattr=tokens[j].pattr,
offset=tokens[j].offset,

View File

@ -25,7 +25,6 @@ from xdis import (
)
from uncompyle6.scanner import Code
from uncompyle6.semantics.parser_error import ParserError
from uncompyle6.parser import ParserError as ParserError2
from uncompyle6.semantics.helper import (
find_all_globals,
find_globals_and_nonlocals,

View File

@ -257,11 +257,14 @@ class NonterminalActions:
sep = ", "
else:
for elem in flat_elems:
assert elem.kind == "ADD_VALUE"
try:
if elem == "add_value":
elem = elem[0]
if elem == "ADD_VALUE":
value = "%r" % elem.pattr
except Exception:
value = elem.pattr
else:
assert elem.kind == "ADD_VALUE_VAR"
value = "%s" % elem.pattr
if elem.linestart is not None:
if elem.linestart != self.line_number:
next_indent = self.indent + INDENT_PER_LEVEL[:-1]