Customize tables better for specific Python versions

This commit is contained in:
rocky
2016-07-27 06:56:02 -04:00
parent 6dc9d3ab2f
commit c3f6fa32db
2 changed files with 54 additions and 61 deletions

View File

@@ -58,7 +58,6 @@ import re, sys
from uncompyle6 import PYTHON3 from uncompyle6 import PYTHON3
from xdis.code import iscode from xdis.code import iscode
from uncompyle6.semantics import pysource from uncompyle6.semantics import pysource
from uncompyle6.parser import get_python_parser
from uncompyle6 import parser from uncompyle6 import parser
from uncompyle6.scanner import Token, Code, get_scanner from uncompyle6.scanner import Token, Code, get_scanner
from uncompyle6.show import ( from uncompyle6.show import (
@@ -78,7 +77,7 @@ else:
from StringIO import StringIO from StringIO import StringIO
from spark_parser import GenericASTTraversal, GenericASTTraversalPruningException, \ from spark_parser import GenericASTTraversalPruningException, \
DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from collections import namedtuple from collections import namedtuple
@@ -110,37 +109,19 @@ TABLE_DIRECT_FRAGMENT = {
} }
MAP_DIRECT_FRAGMENT = dict(TABLE_DIRECT, **TABLE_DIRECT_FRAGMENT),
class FragmentsWalker(pysource.SourceWalker, object): class FragmentsWalker(pysource.SourceWalker, object):
MAP_DIRECT_FRAGMENT = ()
stacked_params = ('f', 'indent', 'isLambda', '_globals') stacked_params = ('f', 'indent', 'isLambda', '_globals')
def __init__(self, version, scanner, showast=False, def __init__(self, version, scanner, showast=False,
debug_parser=PARSER_DEFAULT_DEBUG, debug_parser=PARSER_DEFAULT_DEBUG,
compile_mode='exec', is_pypy=False): compile_mode='exec', is_pypy=False):
GenericASTTraversal.__init__(self, ast=None) pysource.SourceWalker.__init__(self, version=version, out=StringIO(),
self.scanner = scanner scanner=scanner,
params = { showast=showast, debug_parser=debug_parser,
'f': StringIO(), compile_mode=compile_mode, is_pypy=is_pypy)
'indent': '',
}
self.version = version
self.p = get_python_parser(
version, dict(debug_parser),
compile_mode=compile_mode, is_pypy=is_pypy
)
self.showast = showast
self.params = params
self.param_stack = []
self.ERROR = None
self.prec = 100
self.return_none = False
self.mod_globs = set()
self.currentclass = None
self.classes = []
self.pending_newlines = 0
# hide_internal suppresses displaying the additional instructions that sometimes # hide_internal suppresses displaying the additional instructions that sometimes
# exist in code but but were not written in the source code. # exist in code but but were not written in the source code.
@@ -150,12 +131,13 @@ class FragmentsWalker(pysource.SourceWalker, object):
# deparsing we generally do need to see these instructions since we may be stopped # deparsing we generally do need to see these instructions since we may be stopped
# at one. So here we do not want to suppress showing such instructions. # at one. So here we do not want to suppress showing such instructions.
self.hide_internal = False self.hide_internal = False
self.name = None
self.offsets = {} self.offsets = {}
self.last_finish = -1 self.last_finish = -1
# FIXME: is there a better way?
global MAP_DIRECT_FRAGMENT
MAP_DIRECT_FRAGMENT = dict(TABLE_DIRECT, **TABLE_DIRECT_FRAGMENT),
f = property(lambda s: s.params['f'], f = property(lambda s: s.params['f'],
lambda s, x: s.params.__setitem__('f', x), lambda s, x: s.params.__setitem__('f', x),
lambda s: s.params.__delitem__('f'), lambda s: s.params.__delitem__('f'),

View File

@@ -140,18 +140,6 @@ TABLE_R = {
# 'EXEC_STMT': ( '%|exec %c in %[1]C\n', 0, (0,maxint,', ') ), # 'EXEC_STMT': ( '%|exec %c in %[1]C\n', 0, (0,maxint,', ') ),
} }
if not PYTHON3:
TABLE_R.update({
'STORE_SLICE+0': ( '%c[:]', 0 ),
'STORE_SLICE+1': ( '%c[%p:]', 0, (1, 100) ),
'STORE_SLICE+2': ( '%c[:%p]', 0, (1, 100) ),
'STORE_SLICE+3': ( '%c[%p:%p]', 0, (1, 100), (2, 100) ),
'DELETE_SLICE+0': ( '%|del %c[:]\n', 0 ),
'DELETE_SLICE+1': ( '%|del %c[%c:]\n', 0, 1 ),
'DELETE_SLICE+2': ( '%|del %c[:%c]\n', 0, 1 ),
'DELETE_SLICE+3': ( '%|del %c[%c:%c]\n', 0, 1, 2 ),
})
TABLE_R0 = { TABLE_R0 = {
# 'BUILD_LIST': ( '[%C]', (0,-1,', ') ), # 'BUILD_LIST': ( '[%C]', (0,-1,', ') ),
# 'BUILD_TUPLE': ( '(%C)', (0,-1,', ') ), # 'BUILD_TUPLE': ( '(%C)', (0,-1,', ') ),
@@ -273,8 +261,6 @@ TABLE_DIRECT = {
'kwargs': ( '%D', (0, maxint, ', ') ), 'kwargs': ( '%D', (0, maxint, ', ') ),
'importlist2': ( '%C', (0, maxint, ', ') ), 'importlist2': ( '%C', (0, maxint, ', ') ),
'assert': ( '%|assert %c\n' , 0 ),
'assert2': ( '%|assert %c, %c\n' , 0, 3 ),
'assert_expr_or': ( '%c or %c', 0, 2 ), 'assert_expr_or': ( '%c or %c', 0, 2 ),
'assert_expr_and': ( '%c and %c', 0, 2 ), 'assert_expr_and': ( '%c and %c', 0, 2 ),
'print_items_stmt': ( '%|print %c%c,\n', 0, 2), 'print_items_stmt': ( '%|print %c%c,\n', 0, 2),
@@ -336,7 +322,6 @@ TABLE_DIRECT = {
'except_cond2': ( '%|except %c as %c:\n', 1, 5 ), 'except_cond2': ( '%|except %c as %c:\n', 1, 5 ),
'except_suite': ( '%+%c%-%C', 0, (1, maxint, '') ), 'except_suite': ( '%+%c%-%C', 0, (1, maxint, '') ),
'except_suite_finalize': ( '%+%c%-%C', 1, (3, maxint, '') ), 'except_suite_finalize': ( '%+%c%-%C', 1, (3, maxint, '') ),
'tryfinallystmt': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 5 ),
'withstmt': ( '%|with %c:\n%+%c%-', 0, 3), 'withstmt': ( '%|with %c:\n%+%c%-', 0, 3),
'withasstmt': ( '%|with %c as %c:\n%+%c%-', 0, 2, 3), 'withasstmt': ( '%|with %c as %c:\n%+%c%-', 0, 2, 3),
'passstmt': ( '%|pass\n', ), 'passstmt': ( '%|pass\n', ),
@@ -345,10 +330,6 @@ TABLE_DIRECT = {
'kv2': ( '%c: %c', 1, 2 ), 'kv2': ( '%c: %c', 1, 2 ),
'mapexpr': ( '{%[1]C}', (0, maxint, ', ') ), 'mapexpr': ( '{%[1]C}', (0, maxint, ', ') ),
# CE - Fixes for tuples
'assign2': ( '%|%c, %c = %c, %c\n', 3, 4, 0, 1 ),
'assign3': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 6, 7, 0, 1, 2 ),
####################### #######################
# Python 2.3 Additions # Python 2.3 Additions
####################### #######################
@@ -380,17 +361,6 @@ TABLE_DIRECT = {
####################### #######################
'LOAD_CLASSDEREF': ( '%{pattr}', ), 'LOAD_CLASSDEREF': ( '%{pattr}', ),
########################
# PyPy Additions
# FIXME: we could remove the corresponding
# rules without _pypy if we have pypy
#######################
'assert_pypy': ( '%|assert %c\n' , 1 ),
'assert2_pypy': ( '%|assert %c, %c\n' , 1, 4 ),
'trystmt_pypy': ( '%|try:\n%+%c%-%c\n\n', 1, 2 ),
'tryfinallystmt_pypy': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 3 ),
'assign3_pypy': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 4, 3, 0, 1, 2 ),
'assign2_pypy': ( '%|%c, %c = %c, %c\n', 3, 2, 0, 1),
} }
@@ -544,8 +514,8 @@ class SourceWalker(GenericASTTraversal, object):
'indent': '', 'indent': '',
} }
self.version = version self.version = version
self.p = get_python_parser(version, debug_parser=debug_parser, self.p = get_python_parser(version, debug_parser=dict(debug_parser),
compile_mode=compile_mode) compile_mode=compile_mode, is_pypy=is_pypy)
self.debug_parser = dict(debug_parser) self.debug_parser = dict(debug_parser)
self.showast = showast self.showast = showast
self.params = params self.params = params
@@ -570,8 +540,49 @@ class SourceWalker(GenericASTTraversal, object):
if 2.0 <= version <= 2.3: if 2.0 <= version <= 2.3:
TABLE_DIRECT['tryfinallystmt'] = ( TABLE_DIRECT['tryfinallystmt'] = (
'%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 4 ) '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 4 )
if version < 3.0:
TABLE_R.update({
'STORE_SLICE+0': ( '%c[:]', 0 ),
'STORE_SLICE+1': ( '%c[%p:]', 0, (1, 100) ),
'STORE_SLICE+2': ( '%c[:%p]', 0, (1, 100) ),
'STORE_SLICE+3': ( '%c[%p:%p]', 0, (1, 100), (2, 100) ),
'DELETE_SLICE+0': ( '%|del %c[:]\n', 0 ),
'DELETE_SLICE+1': ( '%|del %c[%c:]\n', 0, 1 ),
'DELETE_SLICE+2': ( '%|del %c[:%c]\n', 0, 1 ),
'DELETE_SLICE+3': ( '%|del %c[%c:%c]\n', 0, 1, 2 ),
})
self.customize_pypy(is_pypy)
return return
@staticmethod
def customize_pypy(is_pypy):
if is_pypy:
########################
# PyPy changes
#######################
TABLE_DIRECT.update({
'assert_pypy': ( '%|assert %c\n' , 1 ),
'assert2_pypy': ( '%|assert %c, %c\n' , 1, 4 ),
'trystmt_pypy': ( '%|try:\n%+%c%-%c\n\n', 1, 2 ),
'tryfinallystmt_pypy': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 3 ),
'assign3_pypy': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 4, 3, 0, 1, 2 ),
'assign2_pypy': ( '%|%c, %c = %c, %c\n', 3, 2, 0, 1),
})
else:
########################
# Without PyPy
#######################
TABLE_DIRECT.update({
'assert': ( '%|assert %c\n' , 0 ),
'assert2': ( '%|assert %c, %c\n' , 0, 3 ),
'trystmt': ( '%|try:\n%+%c%-%c\n\n', 1, 3 ),
'tryfinallystmt': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 5 ),
'assign2': ( '%|%c, %c = %c, %c\n', 3, 4, 0, 1 ),
'assign3': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 6, 7, 0, 1, 2 ),
})
f = property(lambda s: s.params['f'], f = property(lambda s: s.params['f'],
lambda s, x: s.params.__setitem__('f', x), lambda s, x: s.params.__setitem__('f', x),
lambda s: s.params.__delitem__('f'), lambda s: s.params.__delitem__('f'),