Skip to content

Commit

Permalink
Keep optype info in token...
Browse files Browse the repository at this point in the history
It is useful for ADD_VALUE
  • Loading branch information
rocky committed Feb 24, 2024
1 parent d2a1716 commit 8a1fd7e
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion test/stdlib/3.6-exclude.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SKIP_TESTS=(
[test_ast.py]=1 # FIXME: Works on c90ff51
[test_cmath.py]=1 # FIXME: Works on c90ff51
[test_cmath.py]=1 # fails on its own
[test_format.py]=1 # FIXME: Works on c90ff51
[test_ftplib.py]=1 # FIXME: Works on c90ff51
[test_slice.py]=1 # FIXME: Works on c90ff51
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/3.7-exclude.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SKIP_TESTS=(
[test_bdb.py]=1 #
[test_buffer.py]=1 # parse error
[test_clinic.py]=1 # it fails on its own
[test_cmath.py]=1 # test assertion failure
[test_cmath.py]=pytest
[test_cmd_line.py]=1 # Interactive?
[test_cmd_line_script.py]=1
[test_compileall.py]=1 # fails on its own
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/3.8-exclude.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ SKIP_TESTS=(
[test_cgi.py]=1 # parse error
[test_cgitb.py]=1 # parse error
[test_clinic.py]=1 # it fails on its own
[test_cmath.py]=1 # test assertion failure
[test_cmath.py]=pytest
[test_cmd.py]=1 # parse error
[test_cmd_line.py]=1 # Interactive?
[test_cmd_line_script.py]=1
Expand Down
2 changes: 1 addition & 1 deletion test/stdlib/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ if [[ -n $1 ]] ; then
if (( ${#files_ary[@]} == 1 || DONT_SKIP_TESTS == 1 )) ; then
for file in $files; do
if (( SKIP_TESTS[$file] != "pytest" )); then
SKIP_TESTS[$file] = 1;
SKIP_TESTS[$file]=1;
fi
done
fi
Expand Down
6 changes: 6 additions & 0 deletions uncompyle6/scanners/scanner3.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def bound_collection_from_inst(
has_arg=True,
has_extended_arg=False,
opc=self.opc,
optype=None,
)
)
for j in range(collection_start, i):
Expand All @@ -279,6 +280,7 @@ def bound_collection_from_inst(
has_arg=True,
has_extended_arg=False,
opc=self.opc,
optype=insts[j].optype,
)
)
new_tokens.append(
Expand All @@ -291,6 +293,7 @@ def bound_collection_from_inst(
has_arg=t.has_arg,
has_extended_arg=False,
opc=t.opc,
optype=None,
)
)
return new_tokens
Expand Down Expand Up @@ -339,6 +342,7 @@ def bound_map_from_inst(
has_arg=True,
has_extended_arg=False,
opc=self.opc,
optype=None,
)
)
for j in range(collection_start, i, 2):
Expand All @@ -364,6 +368,7 @@ def bound_map_from_inst(
has_arg=True,
has_extended_arg=False,
opc=self.opc,
optype=insts[j + 1].optype,
)
)
new_tokens.append(
Expand All @@ -376,6 +381,7 @@ def bound_map_from_inst(
has_arg=t.has_arg,
has_extended_arg=False,
opc=t.opc,
optype=t.optype,
)
)
return new_tokens
Expand Down
8 changes: 5 additions & 3 deletions uncompyle6/scanners/scanner37.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

from typing import Tuple

from uncompyle6.scanner import CONST_COLLECTIONS, Token
from uncompyle6.scanners.scanner37base import Scanner37Base

# bytecode verification, verify(), uses JUMP_OPs from here
from xdis.opcodes import opcode_37 as opc

from uncompyle6.scanner import CONST_COLLECTIONS, Token
from uncompyle6.scanners.scanner37base import Scanner37Base

# bytecode verification, verify(), uses JUMP_OPS from here
JUMP_OPs = opc.JUMP_OPS

Expand Down Expand Up @@ -90,6 +90,7 @@ def bound_collection_from_tokens(
has_arg=True,
has_extended_arg=False,
opc=self.opc,
optype=None,
)
)
for j in range(collection_start, i):
Expand All @@ -103,6 +104,7 @@ def bound_collection_from_tokens(
has_arg=True,
has_extended_arg=False,
opc=self.opc,
optype=tokens[j].optype,
)
)
new_tokens.append(
Expand Down
3 changes: 3 additions & 0 deletions uncompyle6/scanners/scanner37base.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ def tokens_append(j, token):
has_arg=True,
opc=self.opc,
has_extended_arg=False,
optype=inst.optype,
),
)
jump_idx += 1
Expand Down Expand Up @@ -426,6 +427,7 @@ def tokens_append(j, token):
has_arg=inst.has_arg,
opc=self.opc,
has_extended_arg=inst.has_extended_arg,
optype=inst.optype,
),
)
continue
Expand Down Expand Up @@ -527,6 +529,7 @@ def tokens_append(j, token):
has_arg=inst.has_arg,
opc=self.opc,
has_extended_arg=inst.has_extended_arg,
optype=inst.optype,
),
)
pass
Expand Down
10 changes: 6 additions & 4 deletions uncompyle6/scanners/tok.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ def __init__(
opname,
attr=None,
pattr=None,
offset:Union[int, str]=-1,
offset: Union[int, str] = -1,
linestart=None,
op=None,
has_arg=None,
opc=None,
has_extended_arg=False,
optype=None,
):
self.kind = intern(opname)
self.has_arg = has_arg
self.attr = attr
self.pattr = pattr
self.optype = optype
if has_extended_arg:
self.offset = "%d_%d" % (offset, offset + 2)
else:
Expand Down Expand Up @@ -107,8 +109,8 @@ def __init__(
self.op = op

def __eq__(self, o):
""" '==' on kind and "pattr" attributes.
It is okay if offsets and linestarts are different"""
"""'==' on kind and "pattr" attributes.
It is okay if offsets and linestarts are different"""
if isinstance(o, Token):
return (self.kind == o.kind) and (
(self.pattr == o.pattr) or self.attr == o.attr
Expand All @@ -118,7 +120,7 @@ def __eq__(self, o):
return self.kind == o

def __ne__(self, o):
""" '!=', but it's okay if offsets and linestarts are different"""
"""'!=', but it's okay if offsets and linestarts are different"""
return not self.__eq__(o)

def __repr__(self):
Expand Down
6 changes: 3 additions & 3 deletions uncompyle6/semantics/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
SyntaxTree(
"expr", [Token(doc_load, pattr=doc_string, attr=doc_string)]
),
SyntaxTree("store", [Token("STORE_NAME", pattr="__doc__")]),
SyntaxTree("store", [Token("STORE_NAME", pattr="__doc__", optype="name")]),
],
)

Expand All @@ -148,10 +148,10 @@
"assign",
[
SyntaxTree(
"expr", [Token("LOAD_NAME", pattr="__name__", offset=0, has_arg=True)]
"expr", [Token("LOAD_NAME", pattr="__name__", offset=0, has_arg=True, optype="name")]
),
SyntaxTree(
"store", [Token("STORE_NAME", pattr="__module__", offset=3, has_arg=True)]
"store", [Token("STORE_NAME", pattr="__module__", offset=3, has_arg=True, optype="name")]
),
],
)
Expand Down
22 changes: 15 additions & 7 deletions uncompyle6/semantics/n_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,15 @@ def n_const_list(self, node: SyntaxTree):
assert len(keys) == len(flat_elems) - 1
for i, elem in enumerate(flat_elems[:-1]):
assert elem.kind == "ADD_VALUE"
try:
value = "%r" % elem.pattr
except Exception:
value = elem.pattr
if elem.optype in ("local", "name"):
value = elem.attr
elif elem.optype == "const" and not isinstance(elem.attr, str):
value = elem.attr
else:
try:
value = "%r" % elem.pattr
except Exception:
value = elem.pattr
if elem.linestart is not None:
if elem.linestart != self.line_number:
next_indent = self.indent + INDENT_PER_LEVEL[:-1]
Expand All @@ -266,11 +271,14 @@ def n_const_list(self, node: SyntaxTree):
for elem in flat_elems:
if elem == "add_value":
elem = elem[0]

if elem == "ADD_VALUE":
if self.version < (3, 0, 0):
value = "%r" % repr(elem.pattr)
if elem.optype in ("local", "name"):
value = elem.attr
elif elem.optype == "const" and not isinstance(elem.attr, str):
value = elem.attr
else:
value = "%s" % str(elem.pattr)
value = "%s" % repr(elem.pattr)
else:
assert elem.kind == "ADD_VALUE_VAR"
value = "%s" % elem.pattr
Expand Down

0 comments on commit 8a1fd7e

Please sign in to comment.