python3 compatibiity and remove some flake8 warnings.

This commit is contained in:
rocky
2015-12-11 19:27:29 -05:00
parent 39842cef1f
commit bc1c1d6a67
3 changed files with 43 additions and 30 deletions

View File

@@ -1,3 +1,5 @@
from __future__ import print_function
import uncompyle6
from uncompyle6 import uncompyle, walker, verify, magics
from uncompyle6.spark import GenericASTTraversal, GenericASTTraversalPruningException
@@ -28,7 +30,6 @@ class FindWalker(walker.Walker, object):
self.found_offset = False
self.offsets = {}
f = property(lambda s: s.__params['f'],
lambda s, x: s.__params.__setitem__('f', x),
lambda s: s.__params.__delitem__('f'),
@@ -85,7 +86,6 @@ class FindWalker(walker.Walker, object):
return
def find_source(self, offset, ast, customize, isLambda=0, returnNone=False):
"""convert AST to source code"""
@@ -133,7 +133,7 @@ class FindWalker(walker.Walker, object):
pass
def uncompyle_find(version, co, find_offset, out=sys.stdout, showasm=0, showast=0):
assert type(co) == types.CodeType
assert isinstance(co, types.CodeType)
# store final output stream for case of error
__real_out = out or sys.stdout
if version == 2.7:
@@ -154,8 +154,8 @@ def uncompyle_find(version, co, find_offset, out=sys.stdout, showasm=0, showast=
try:
ast = walk.build_ast(tokens, customize)
except walker.ParserError, e : # parser failed, dump disassembly
print >>__real_out, e
except walker.ParserError as e : # parser failed, dump disassembly
print(e, file=__real_out)
raise
del tokens # save memory
@@ -169,7 +169,7 @@ def uncompyle_find(version, co, find_offset, out=sys.stdout, showasm=0, showast=
del ast[0]
if ast[-1] == walker.RETURN_NONE:
ast.pop() # remove last node
#todo: if empty, add 'pass'
# todo: if empty, add 'pass'
except:
pass
walk.mod_globs = walker.find_globals(ast, set())
@@ -187,8 +187,8 @@ def uncompyle_test():
try:
co = frame.f_code
uncompyle(2.7, co, sys.stdout, 1)
print
print '------------------------'
print()
print('------------------------')
uncompyle_find(2.7, co, 33)
finally:
del frame

View File

@@ -31,8 +31,14 @@ from __future__ import print_function
'''
import sys, types, os
import walker, verify, magics
import disas as dis
if (sys.version_info > (3, 0)):
from . import walker, verify, magics
from . import disas as dis
else:
import walker, verify, magics
import disas as dis
import marshal
sys.setrecursionlimit(5000)
@@ -90,7 +96,8 @@ def uncompyle(version, co, out=None, showasm=0, showast=0):
# store final output stream for case of error
__real_out = out or sys.stdout
if co.co_filename:
print >>__real_out, '# Embedded file name: %s' % co.co_filename
print('# Embedded file name: %s' % co.co_filename,
file=__real_out)
# diff scanner
if version == 2.7:
import scanner27 as scan
@@ -109,7 +116,7 @@ def uncompyle(version, co, out=None, showasm=0, showast=0):
try:
ast = walk.build_ast(tokens, customize)
except walker.ParserError as e : # parser failed, dump disassembly
print >>__real_out, e
print(e, file=__real_out)
raise
del tokens # save memory
@@ -190,7 +197,7 @@ def main(in_base, out_base, files, codes, outfile=None,
for file in files:
infile = os.path.join(in_base, file)
# print >>sys.stderr, infile
# print (infile, file=sys.stderr)
if of: # outfile was given as parameter
outstream = _get_outstream(outfile)
@@ -199,7 +206,7 @@ def main(in_base, out_base, files, codes, outfile=None,
else:
outfile = os.path.join(out_base, file) + '_dis'
outstream = _get_outstream(outfile)
# print >>sys.stderr, outfile
# print(outfile, file=sys.stderr)
# try to decomyple the input file
try:
@@ -232,8 +239,8 @@ def main(in_base, out_base, files, codes, outfile=None,
verify_failed_files += 1
os.rename(outfile, outfile + '_unverified')
if not outfile:
print >>sys.stderr, "### Error Verifiying", file
print >>sys.stderr, e
print("### Error Verifiying %s" % file, file=sys.stderr)
print(e, file=sys.stderr)
else:
okay_files += 1
if not outfile: print('\n# okay decompyling', infile, __memUsage())

View File

@@ -1,3 +1,5 @@
from __future__ import print_function
'''
Copyright (c) 1999 John Aycock
Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
@@ -41,9 +43,13 @@
makes the engine walk down to N[C] before evaluating the escape code.
'''
import sys, re, cStringIO
from types import ListType, TupleType, DictType, \
EllipsisType, IntType, CodeType
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import sys, re
from types import IntType, CodeType
from spark import GenericASTTraversal
import parser
@@ -488,7 +494,7 @@ class Walker(GenericASTTraversal, object):
self.pending_newlines = 0
self.__params = {
'_globals': {},
'f': cStringIO.StringIO(),
'f': StringIO(),
'indent': indent,
'isLambda': isLambda,
}
@@ -704,7 +710,7 @@ class Walker(GenericASTTraversal, object):
# change:hG/2002-02-07: this was done for all negative integers
# todo: check whether this is necessary in Python 2.1
self.write( hex(data) )
elif datatype is EllipsisType:
elif datatype is type(Ellipsis):
self.write('...')
elif data is None:
# LOAD_CONST 'None' only occurs, when None is
@@ -1118,7 +1124,7 @@ class Walker(GenericASTTraversal, object):
if m.group('child'):
node = node[int(m.group('child'))]
except:
print node.__dict__
print(node.__dict__)
raise
if typ == '%': self.write('%')
@@ -1166,7 +1172,7 @@ class Walker(GenericASTTraversal, object):
try:
self.write(eval(expr, d, d))
except:
print node
print(node)
raise
m = escape.search(fmt, i)
self.write(fmt[i:])
@@ -1268,9 +1274,9 @@ class Walker(GenericASTTraversal, object):
if default:
if self.showast:
print '--', name
print default
print '--'
print('--', name)
print(default)
print('--')
result = '%s = %s' % (name, self.traverse(default, indent='') )
if result[-2:] == '= ': # default was 'LOAD_CONST None'
result += 'None'
@@ -1408,14 +1414,14 @@ class Walker(GenericASTTraversal, object):
self.return_none = rn
def build_ast(self, tokens, customize, isLambda=0, noneInNames=False):
assert type(tokens) == ListType
#assert isinstance(tokens[0], Token)
# assert isinstance(tokens[0], Token)
if isLambda:
tokens.append(Token('LAMBDA_MARKER'))
try:
ast = parser.parse(tokens, customize)
except parser.ParserError, e:
except parser.ParserError as e:
raise ParserError(e, tokens)
if self.showast:
self.print_(repr(ast))
@@ -1433,7 +1439,7 @@ class Walker(GenericASTTraversal, object):
# Build AST from disassembly.
try:
ast = parser.parse(tokens, customize)
except parser.ParserError, e:
except parser.ParserError as e:
raise ParserError(e, tokens)
if self.showast: