You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 09:22:40 +08:00
localize 2 and 3 argument BUILD_SLICE...
Nontermninal name matches AST anme now. Add test.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,3 +1,8 @@
|
|||||||
ary = [1,2,3]
|
# Tests of Python slice operators
|
||||||
|
|
||||||
|
ary = [1,2,3,4,5]
|
||||||
|
# Forces BUILD_SLICE 2 on 3.x
|
||||||
ary[:2]
|
ary[:2]
|
||||||
ary[2:]
|
ary[2:]
|
||||||
|
# Forces BUILD_SLICE 3
|
||||||
|
ary[1:4:2]
|
||||||
|
@@ -101,7 +101,10 @@ class PythonParser(GenericASTBuilder):
|
|||||||
def fix(c):
|
def fix(c):
|
||||||
s = str(c)
|
s = str(c)
|
||||||
last_token_pos = s.find('_')
|
last_token_pos = s.find('_')
|
||||||
return s if last_token_pos == -1 else s[:last_token_pos]
|
if last_token_pos == -1:
|
||||||
|
return s
|
||||||
|
else:
|
||||||
|
return s[:last_token_pos]
|
||||||
|
|
||||||
prefix = ''
|
prefix = ''
|
||||||
if parent and tokens:
|
if parent and tokens:
|
||||||
@@ -134,7 +137,10 @@ class PythonParser(GenericASTBuilder):
|
|||||||
err_token = instructions[index]
|
err_token = instructions[index]
|
||||||
print("Instruction context:")
|
print("Instruction context:")
|
||||||
for i in range(start, finish):
|
for i in range(start, finish):
|
||||||
indent = ' ' if i != index else '-> '
|
if i != index:
|
||||||
|
indent = ' '
|
||||||
|
else:
|
||||||
|
indent = '-> '
|
||||||
print("%s%s" % (indent, instructions[i]))
|
print("%s%s" % (indent, instructions[i]))
|
||||||
raise ParserError(err_token, err_token.offset)
|
raise ParserError(err_token, err_token.offset)
|
||||||
else:
|
else:
|
||||||
@@ -448,8 +454,6 @@ class PythonParser(GenericASTBuilder):
|
|||||||
expr ::= binary_subscr
|
expr ::= binary_subscr
|
||||||
expr ::= binary_subscr2
|
expr ::= binary_subscr2
|
||||||
expr ::= get_iter
|
expr ::= get_iter
|
||||||
expr ::= buildslice2
|
|
||||||
expr ::= buildslice3
|
|
||||||
expr ::= yield
|
expr ::= yield
|
||||||
|
|
||||||
binary_expr ::= expr expr binary_op
|
binary_expr ::= expr expr binary_op
|
||||||
@@ -477,8 +481,6 @@ class PythonParser(GenericASTBuilder):
|
|||||||
|
|
||||||
load_attr ::= expr LOAD_ATTR
|
load_attr ::= expr LOAD_ATTR
|
||||||
get_iter ::= expr GET_ITER
|
get_iter ::= expr GET_ITER
|
||||||
buildslice3 ::= expr expr expr BUILD_SLICE_3
|
|
||||||
buildslice2 ::= expr expr BUILD_SLICE_2
|
|
||||||
|
|
||||||
yield ::= expr YIELD_VALUE
|
yield ::= expr YIELD_VALUE
|
||||||
|
|
||||||
|
@@ -313,6 +313,21 @@ class Python2Parser(PythonParser):
|
|||||||
"mapexpr ::= %s %s" % (opname, kvlist_n)
|
"mapexpr ::= %s %s" % (opname, kvlist_n)
|
||||||
], customize)
|
], customize)
|
||||||
continue
|
continue
|
||||||
|
elif opname_base == 'BUILD_SLICE':
|
||||||
|
slice_num = token.attr
|
||||||
|
if slice_num == 2:
|
||||||
|
self.add_unique_rules([
|
||||||
|
'expr ::= slice2',
|
||||||
|
'slice2 ::= expr expr BUILD_SLICE_2'
|
||||||
|
], customize)
|
||||||
|
else:
|
||||||
|
assert slice_num == 3, ("BUILD_SLICE value must be 2 or 3; is %s" %
|
||||||
|
slice_num)
|
||||||
|
self.add_unique_rules([
|
||||||
|
'expr ::= slice3',
|
||||||
|
'slice3 ::= expr expr expr BUILD_SLICE_3',
|
||||||
|
], customize)
|
||||||
|
continue
|
||||||
elif opname_base in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR',
|
elif opname_base in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR',
|
||||||
'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'):
|
'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'):
|
||||||
args_pos = (v & 0xff) # positional parameters
|
args_pos = (v & 0xff) # positional parameters
|
||||||
|
@@ -738,6 +738,18 @@ class Python3Parser(PythonParser):
|
|||||||
self.custom_classfunc_rule(opname, token, customize,
|
self.custom_classfunc_rule(opname, token, customize,
|
||||||
seen_LOAD_BUILD_CLASS,
|
seen_LOAD_BUILD_CLASS,
|
||||||
seen_GET_AWAITABLE_YIELD_FROM)
|
seen_GET_AWAITABLE_YIELD_FROM)
|
||||||
|
elif opname_base == 'BUILD_SLICE':
|
||||||
|
if token.attr == 2:
|
||||||
|
self.add_unique_rules([
|
||||||
|
'expr ::= slice2',
|
||||||
|
'slice2 ::= expr expr BUILD_SLICE_2'
|
||||||
|
], customize)
|
||||||
|
else:
|
||||||
|
assert token.attr == 3, "BUILD_SLICE value must be 2 or 3; is %s" % v
|
||||||
|
self.add_unique_rules([
|
||||||
|
'expr ::= slice3',
|
||||||
|
'slice3 ::= expr expr expr BUILD_SLICE_3',
|
||||||
|
], customize)
|
||||||
elif opname_base == 'CALL_METHOD':
|
elif opname_base == 'CALL_METHOD':
|
||||||
# PyPy only - DRY with parse2
|
# PyPy only - DRY with parse2
|
||||||
|
|
||||||
|
@@ -85,7 +85,9 @@ class Scanner2(Scanner):
|
|||||||
cause specific rules for the specific number of arguments they take.
|
cause specific rules for the specific number of arguments they take.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
show_asm = self.show_asm if not show_asm else show_asm
|
if not show_asm:
|
||||||
|
show_asm = self.show_asm
|
||||||
|
|
||||||
# show_asm = 'after'
|
# show_asm = 'after'
|
||||||
if show_asm in ('both', 'before'):
|
if show_asm in ('both', 'before'):
|
||||||
from xdis.bytecode import Bytecode
|
from xdis.bytecode import Bytecode
|
||||||
@@ -247,8 +249,7 @@ class Scanner2(Scanner):
|
|||||||
op_name = 'BUILD_MAP_n'
|
op_name = 'BUILD_MAP_n'
|
||||||
else:
|
else:
|
||||||
op_name = '%s_%d' % (op_name, oparg)
|
op_name = '%s_%d' % (op_name, oparg)
|
||||||
if op != self.opc.BUILD_SLICE:
|
customize[op_name] = oparg
|
||||||
customize[op_name] = oparg
|
|
||||||
elif self.is_pypy and op_name in ('LOOKUP_METHOD',
|
elif self.is_pypy and op_name in ('LOOKUP_METHOD',
|
||||||
'JUMP_IF_NOT_DEBUG',
|
'JUMP_IF_NOT_DEBUG',
|
||||||
'SETUP_EXCEPT',
|
'SETUP_EXCEPT',
|
||||||
@@ -1038,8 +1039,10 @@ class Scanner2(Scanner):
|
|||||||
|
|
||||||
# FIXME: rocky: I think we need something like this...
|
# FIXME: rocky: I think we need something like this...
|
||||||
if offset not in set(self.ignore_if) or self.version == 2.7:
|
if offset not in set(self.ignore_if) or self.version == 2.7:
|
||||||
source = (self.setup_loops[label]
|
if label in self.setup_loops:
|
||||||
if label in self.setup_loops else offset)
|
source = self.setup_loops[label]
|
||||||
|
else:
|
||||||
|
source = offset
|
||||||
targets[label] = targets.get(label, []) + [source]
|
targets[label] = targets.get(label, []) + [source]
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@@ -235,8 +235,7 @@ class Scanner26(scan.Scanner2):
|
|||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
op_name = '%s_%d' % (op_name, oparg)
|
op_name = '%s_%d' % (op_name, oparg)
|
||||||
if op != self.opc.BUILD_SLICE:
|
customize[op_name] = oparg
|
||||||
customize[op_name] = oparg
|
|
||||||
elif op == self.opc.JUMP_ABSOLUTE:
|
elif op == self.opc.JUMP_ABSOLUTE:
|
||||||
# Further classify JUMP_ABSOLUTE into backward jumps
|
# Further classify JUMP_ABSOLUTE into backward jumps
|
||||||
# which are used in loops, and "CONTINUE" jumps which
|
# which are used in loops, and "CONTINUE" jumps which
|
||||||
|
@@ -714,7 +714,7 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
assert False, "dunno about this python version"
|
assert False, "dunno about this python version"
|
||||||
self.prune() # stop recursing
|
self.prune() # stop recursing
|
||||||
|
|
||||||
def n_buildslice3(self, node):
|
def n_slice3(self, node):
|
||||||
p = self.prec
|
p = self.prec
|
||||||
self.prec = 100
|
self.prec = 100
|
||||||
if not node[0].isNone():
|
if not node[0].isNone():
|
||||||
@@ -728,7 +728,7 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
self.prec = p
|
self.prec = p
|
||||||
self.prune() # stop recursing
|
self.prune() # stop recursing
|
||||||
|
|
||||||
def n_buildslice2(self, node):
|
def n_slice2(self, node):
|
||||||
p = self.prec
|
p = self.prec
|
||||||
self.prec = 100
|
self.prec = 100
|
||||||
if not node[0].isNone():
|
if not node[0].isNone():
|
||||||
@@ -1744,7 +1744,7 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
# if a tuple item is some sort of slice.
|
# if a tuple item is some sort of slice.
|
||||||
no_parens = False
|
no_parens = False
|
||||||
for n in node:
|
for n in node:
|
||||||
if n == 'expr' and n[0].kind.startswith('buildslice'):
|
if n == 'expr' and n[0].kind.startswith('slice'):
|
||||||
no_parens = True
|
no_parens = True
|
||||||
break
|
break
|
||||||
pass
|
pass
|
||||||
|
Reference in New Issue
Block a user