add support for generated source encoding

This commit is contained in:
x0ret
2019-05-27 17:19:10 +04:30
parent a5cdb50154
commit 9db59f1b80
2 changed files with 13 additions and 7 deletions

View File

@@ -41,6 +41,8 @@ Options:
--weak-verify compile generated source
--linemaps generated line number correspondencies between byte-code
and generated source output
--encoding <encoding>
use <encoding> in generated source according to pep-0263
--help show this message
Debugging Options:
@@ -90,7 +92,7 @@ def main_bin():
'timestamp tree tree+ '
'fragments verify verify-run version '
'weak-verify '
'showgrammar'.split(' '))
'showgrammar encoding='.split(' '))
except getopt.GetoptError as e:
print('%s: %s' % (os.path.basename(sys.argv[0]), e), file=sys.stderr)
sys.exit(-1)
@@ -134,6 +136,8 @@ def main_bin():
numproc = int(val)
elif opt in ('--recurse', '-r'):
recurse_dirs = True
elif opt == '--encoding':
options['source_encoding'] = val
else:
print(opt, file=sys.stderr)
usage()

View File

@@ -46,7 +46,7 @@ def _get_outstream(outfile):
def decompile(
bytecode_version, co, out=None, showasm=None, showast=False,
timestamp=None, showgrammar=False, code_objects={},
timestamp=None, showgrammar=False, source_encoding=None, code_objects={},
source_size=None, is_pypy=None, magic_int=None,
mapstream=None, do_fragments=False):
"""
@@ -72,6 +72,8 @@ def decompile(
co_pypy_str = 'PyPy ' if is_pypy else ''
run_pypy_str = 'PyPy ' if IS_PYPY else ''
sys_version_lines = sys.version.split('\n')
if source_encoding:
write('# -*- coding: %s -*-' % source_encoding)
write('# uncompyle6 version %s\n'
'# %sPython bytecode %s%s\n# Decompiled from: %sPython %s' %
(VERSION, co_pypy_str, bytecode_version,
@@ -136,7 +138,7 @@ def compile_file(source_path):
def decompile_file(filename, outstream=None, showasm=None, showast=False,
showgrammar=False, mapstream=None, do_fragments=False):
showgrammar=False, source_encoding=None, mapstream=None, do_fragments=False):
"""
decompile Python byte-code file (.pyc). Return objects to
all of the deparsed objects found in `filename`.
@@ -152,12 +154,12 @@ def decompile_file(filename, outstream=None, showasm=None, showast=False,
for con in co:
deparsed.append(
decompile(version, con, outstream, showasm, showast,
timestamp, showgrammar, code_objects=code_objects,
timestamp, showgrammar, source_encoding, code_objects=code_objects,
is_pypy=is_pypy, magic_int=magic_int),
mapstream=mapstream)
else:
deparsed = [decompile(version, co, outstream, showasm, showast,
timestamp, showgrammar,
timestamp, showgrammar, source_encoding,
code_objects=code_objects, source_size=source_size,
is_pypy=is_pypy, magic_int=magic_int,
mapstream=mapstream, do_fragments=do_fragments)]
@@ -168,7 +170,7 @@ def decompile_file(filename, outstream=None, showasm=None, showast=False,
# FIXME: combine into an options parameter
def main(in_base, out_base, compiled_files, source_files, outfile=None,
showasm=None, showast=False, do_verify=False,
showgrammar=False, raise_on_error=False,
showgrammar=False, source_encoding=None, raise_on_error=False,
do_linemaps=False, do_fragments=False):
"""
in_base base directory for input files
@@ -245,7 +247,7 @@ def main(in_base, out_base, compiled_files, source_files, outfile=None,
# Try to uncompile the input file
try:
deparsed = decompile_file(infile, outstream, showasm, showast, showgrammar,
linemap_stream, do_fragments)
source_encoding, linemap_stream, do_fragments)
if do_fragments:
for d in deparsed:
last_mod = None