X Tutup
Skip to content

Commit b3c8cbb

Browse files
committed
More python2 -> python3 compatibility
1 parent d3c7322 commit b3c8cbb

19 files changed

+387
-286
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.pyc
22
*~
33
/.python-version
4+
/uncompyle6.egg-info
45
build

__pkginfo__.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""uncompyle6 packaging information"""
2+
3+
# To the extent possible we make this file look more like a
4+
# configuration file rather than code like setup.py. I find putting
5+
# configuration stuff in the middle of a function call in setup.py,
6+
# which for example requires commas in between parameters, is a little
7+
# less elegant than having it here with reduced code, albeit there
8+
# still is some room for improvement.
9+
10+
# Things that change more often go here.
11+
copyright = """
12+
Copyright (C) 2015 Rocky Bernstein <rocky@gnu.org>.
13+
"""
14+
15+
# classifiers = ['Development Status :: 5 - Production/Stable',
16+
# 'Environment :: Console',
17+
# 'Intended Audience :: Developers',
18+
# 'License :: OSI Approved :: GNU General Public License (GPL)',
19+
# 'Operating System :: OS Independent',
20+
# 'Programming Language :: Python',
21+
# 'Topic :: Software Development :: Debuggers',
22+
# 'Topic :: Software Development :: Libraries :: Python Modules',
23+
# ]
24+
25+
# The rest in alphabetic order
26+
author = "Rocky Bernstein"
27+
author_email = "rocky@gnu.org"
28+
ftp_url = None
29+
# license = 'GPL'
30+
mailing_list = 'python-debugger@googlegroups.com'
31+
modname = 'uncompyle6'
32+
packages = ['uncompyle6', 'uncompyle6.opcodes']
33+
py_modules = None
34+
short_desc = 'Python byte-code to source-code converter'
35+
36+
import os
37+
import os.path, sys
38+
39+
40+
def get_srcdir():
41+
filename = os.path.normcase(os.path.dirname(os.path.abspath(__file__)))
42+
return os.path.realpath(filename)
43+
44+
# VERSION.py sets variable VERSION.
45+
ns = {}
46+
version = '2.0'
47+
web = 'https://github.com/rocky/uncompyle6/'
48+
49+
# tracebacks in zip files are funky and not debuggable
50+
zip_safe = False
51+
52+
53+
def read(*rnames):
54+
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
55+
56+
long_description = ( read("README.rst") + '\n' )

setup.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
#! python
22

3-
"""Setup script for the 'uncompyle' distribution."""
3+
"""Setup script for the 'uncompyle6' distribution."""
44

55
from distutils.core import setup, Extension
66

7-
setup (name = "uncompyle6",
8-
version = "2.0",
9-
description = "Python byte-code to source-code converter",
10-
author = "Mysterie",
11-
author_email = "kajusska@gmail.com",
12-
url = "http://github.com/Mysterie/uncompyle2",
13-
packages=['uncompyle6', 'uncompyle6.opcode'],
14-
scripts=['scripts/uncompyle6']
15-
)
7+
# Get the package information used in setup().
8+
# from __pkginfo__ import \
9+
# author, author_email, classifiers, \
10+
# install_requires, license, long_description, \
11+
# modname, packages, py_modules, \
12+
# short_desc, version, web, zip_safe
13+
14+
from __pkginfo__ import \
15+
author, author_email, \
16+
long_description, \
17+
modname, packages, py_modules, \
18+
short_desc, version, web, zip_safe
19+
20+
__import__('pkg_resources')
21+
from setuptools import setup
22+
23+
setup(
24+
author = author,
25+
author_email = author_email,
26+
# classifiers = classifiers,
27+
description = short_desc,
28+
# install_requires = install_requires,
29+
# license = license,
30+
long_description = long_description,
31+
py_modules = py_modules,
32+
name = modname,
33+
packages = packages,
34+
test_suite = 'nose.collector',
35+
url = web,
36+
setup_requires = ['nose>=1.0'],
37+
version = version,
38+
zip_safe = zip_safe)

test/compile_tests

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#! python
1+
#!/usr/bin/env python
2+
from __future__ import print_function
3+
24
"""
35
compile_tests -- compile test patterns for the decompyle test suite
46
"""
@@ -20,8 +22,8 @@ for opt, val in opts:
2022
if args:
2123
raise 'This tool does not want any arguments'
2224

23-
print "Using files in dir %s" % src_dir
24-
print "Compiling into dir %s" % work_dir
25+
print("Using files in dir %s" % src_dir)
26+
print("Compiling into dir %s" % work_dir)
2527

2628
tests = {}
2729

@@ -46,6 +48,7 @@ tests['2.3'] = tests['2.2']
4648
tests['2.5'] = tests['2.3']
4749
tests['2.6'] = tests['2.5']
4850
tests['2.7'] = ['mine'] + tests['2.6']
51+
tests['3.4'] = ['mine']
4952
total_tests = len(tests['2.7'])
5053
#tests['2.2'].sort(); print tests['2.2']
5154

@@ -62,13 +65,13 @@ def compile_for_version(version):
6265
os.mkdir(target_dir)
6366
for file in tests[version]:
6467
compile(file, target_dir)
65-
68+
6669
try:
6770
version = '%i.%i' % sys.version_info[:2]
6871
except AttributeError:
6972
version = sys.version[:3]
7073

71-
print 'Compiling test files for Python', version,
72-
print '(%i/%i files)' % (len(tests[version]), total_tests)
74+
print('Compiling test files for Python', version)
75+
print('(%i/%i files)' % (len(tests[version]), total_tests))
7376
compile_for_version(version)
74-
print 'Done.'
77+
print('Done.')

test_pythonlib.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#! python
1+
#!/usr/bin/env python
2+
from __future__ import print_function
23

34
'''
45
test_pythonlib -- uncompyle and verify Python libraries
@@ -19,7 +20,7 @@
1920
test_pythonlib --mylib --verify # decompile verify 'mylib'
2021
'''
2122

22-
from uncompyle2 import main, verify
23+
from uncompyle6 import main, verify
2324
import getopt, sys
2425
import os, time, shutil
2526
from fnmatch import fnmatch
@@ -44,11 +45,11 @@
4445
#-----
4546

4647
def help():
47-
print 'Usage-Examples:'
48-
print 'test_pythonlib --all # decompile all tests (suite + libs)'
49-
print 'test_pythonlib --all --verify # decomyile all tests and verify results'
50-
print 'test_pythonlib --test # decompile only the testsuite'
51-
print 'test_pythonlib --2.2 --verify # decompile and verify python lib 2.2'
48+
print('Usage-Examples:')
49+
print('test_pythonlib --all # decompile all tests (suite + libs)')
50+
print('test_pythonlib --all --verify # decomyile all tests and verify results')
51+
print('test_pythonlib --test # decompile only the testsuite')
52+
print('test_pythonlib --2.2 --verify # decompile and verify python lib 2.2')
5253

5354
def do_tests(src_dir, patterns, target_dir, start_with=None, do_verify=0):
5455
def visitor(files, dirname, names):
@@ -69,18 +70,18 @@ def visitor(files, dirname, names):
6970
try:
7071
start_with = files.index(start_with)
7172
files = files[start_with:]
72-
print '>>> starting with file', files[0]
73+
print('>>> starting with file', files[0])
7374
except ValueError:
7475
pass
7576

76-
print time.ctime()
77-
print 'Working directory: ', src_dir
77+
print(time.ctime())
78+
print('Working directory: ', src_dir)
7879
try:
7980
main(src_dir, target_dir, files, [], do_verify=do_verify)
8081
except (KeyboardInterrupt, OSError):
81-
print
82+
print
8283
exit(1)
83-
84+
8485
if __name__ == '__main__':
8586
do_verify = 0
8687
test_dirs = []

uncompyle-code.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import uncompyle2
2-
from uncompyle2 import uncompyle, walker, verify, magics
3-
from uncompyle2.spark import GenericASTTraversal, GenericASTTraversalPruningException
1+
import uncompyle6
2+
from uncompyle6 import uncompyle, walker, verify, magics
3+
from uncompyle6.spark import GenericASTTraversal, GenericASTTraversalPruningException
44
import sys, inspect, types, cStringIO
55

66
from collections import namedtuple
@@ -137,7 +137,7 @@ def uncompyle_find(version, co, find_offset, out=sys.stdout, showasm=0, showast=
137137
# store final output stream for case of error
138138
__real_out = out or sys.stdout
139139
if version == 2.7:
140-
import uncompyle2.scanner27 as scan
140+
import uncompyle6.scanner27 as scan
141141
scanner = scan.Scanner27()
142142
elif version == 2.6:
143143
import scanner26 as scan

uncompyle6/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import print_function
2+
3+
14
'''
25
Copyright (c) 1999 John Aycock
36
Copyright (c) 2000 by hartmut Goebel <h.goebel@crazy-compilers.com>
@@ -68,9 +71,9 @@ def _load_module(filename):
6871
try:
6972
version = float(magics.versions[magic])
7073
except KeyError:
71-
raise ImportError, "Unknown magic number %s in %s" % (ord(magic[0])+256*ord(magic[1]), filename)
74+
raise ImportError("Unknown magic number %s in %s" % (ord(magic[0])+256*ord(magic[1]), filename))
7275
if (version > 2.7) or (version < 2.5):
73-
raise ImportError, "This is a Python %s file! Only Python 2.5 to 2.7 files are supported." % version
76+
raise ImportError("This is a Python %s file! Only Python 2.5 to 2.7 files are supported." % version)
7477
#print version
7578
fp.read(4) # timestamp
7679
co = dis.marshalLoad(fp)
@@ -105,7 +108,7 @@ def uncompyle(version, co, out=None, showasm=0, showast=0):
105108
walk = walker.Walker(out, scanner, showast=showast)
106109
try:
107110
ast = walk.build_ast(tokens, customize)
108-
except walker.ParserError, e : # parser failed, dump disassembly
111+
except walker.ParserError as e : # parser failed, dump disassembly
109112
print >>__real_out, e
110113
raise
111114
del tokens # save memory
@@ -223,17 +226,17 @@ def _get_outstream(outfile):
223226
if do_verify:
224227
try:
225228
verify.compare_code_with_srcfile(infile, outfile)
226-
if not outfile: print '\n# okay decompyling', infile, __memUsage()
229+
if not outfile: print('\n# okay decompyling', infile, __memUsage())
227230
okay_files += 1
228-
except verify.VerifyCmpError, e:
231+
except verify.VerifyCmpError as e:
229232
verify_failed_files += 1
230233
os.rename(outfile, outfile + '_unverified')
231234
if not outfile:
232235
print >>sys.stderr, "### Error Verifiying", file
233236
print >>sys.stderr, e
234237
else:
235238
okay_files += 1
236-
if not outfile: print '\n# okay decompyling', infile, __memUsage()
239+
if not outfile: print('\n# okay decompyling', infile, __memUsage())
237240
if outfile:
238241
sys.stdout.write("decompiled %i files: %i okay, %i failed, %i verify failed\r" % (tot_files, okay_files, failed_files, verify_failed_files))
239242
sys.stdout.flush()

uncompyle6/magics.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
from __future__ import print_function
2-
import struct
2+
3+
import struct, sys
34

45
__all__ = ['magics', 'versions']
56

67
def __build_magic(magic):
7-
return struct.pack('Hcc', magic, '\r', '\n')
8+
if (sys.version_info > (3, 0)):
9+
return struct.pack('Hcc', magic, bytes('\r', 'utf-8'), bytes('\n', 'utf-8'))
10+
else:
11+
return struct.pack('Hcc', magic, '\r', '\n')
812

913
by_magic = {}
1014
by_version = {}
1115

1216
def __by_version(magics):
13-
for m, v in magics.items():
17+
for m, v in list(magics.items()):
1418
by_magic[m] = v
1519
by_version[v] = m
1620
return by_version
@@ -77,6 +81,7 @@ def test():
7781
magic_20 = magics['2.0']
7882
current = imp.get_magic()
7983
current_version = struct.unpack('HBB', current)[0]
84+
from trepan.api import debug; debug()
8085
magic_current = by_magic[ current ]
8186
print(type(magic_20), len(magic_20), repr(magic_20))
8287
print()

0 commit comments

Comments
 (0)
X Tutup