You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-03 00:45:53 +08:00
Start to handle FUTURE_UNICODE_LITERALS flag
This commit is contained in:
BIN
test/bytecode_2.6/05_unicode_literals.pyc
Normal file
BIN
test/bytecode_2.6/05_unicode_literals.pyc
Normal file
Binary file not shown.
BIN
test/bytecode_2.7/05_unicode_literals.pyc
Normal file
BIN
test/bytecode_2.7/05_unicode_literals.pyc
Normal file
Binary file not shown.
BIN
test/bytecode_3.0/05_unicode_literals.pyc
Normal file
BIN
test/bytecode_3.0/05_unicode_literals.pyc
Normal file
Binary file not shown.
BIN
test/bytecode_3.1/05_unicode_literals.pyc
Normal file
BIN
test/bytecode_3.1/05_unicode_literals.pyc
Normal file
Binary file not shown.
8
test/simple_source/stmts/05_unicode_literals.py
Normal file
8
test/simple_source/stmts/05_unicode_literals.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
# __future__ unicode_literals changes the way we need to print
|
||||||
|
# the below
|
||||||
|
# In Python assembler code "a" is u"a" and b"a" is "a".
|
||||||
|
a = "a"
|
||||||
|
ba = b"a"
|
||||||
|
bb = b"b"
|
@@ -73,12 +73,13 @@ case $PYVERSION in
|
|||||||
SKIP_TESTS=(
|
SKIP_TESTS=(
|
||||||
[test_builtin.py]=1
|
[test_builtin.py]=1
|
||||||
[test_contextlib.py]=1 # decorators
|
[test_contextlib.py]=1 # decorators
|
||||||
[test_decorators.py]=1 # decorators
|
|
||||||
[test_descr.py]=1 # syntax error look at
|
[test_descr.py]=1 # syntax error look at
|
||||||
[test_dis.py]=1 # We change line numbers - duh!
|
[test_dis.py]=1 # We change line numbers - duh!
|
||||||
[test_future4.py]=1 # Possible additional rule for future mechanism?
|
[test_future4.py]=1 # Possible additional rule for future mechanism?
|
||||||
[test_grammar.py]=1 # Too many stmts. Handle large stmts
|
[test_grammar.py]=1 # Too many stmts. Handle large stmts
|
||||||
[test_importlib.py]=1 # Control flow?
|
[test_importlib.py]=1 # Control flow?
|
||||||
|
[test_ioctl.py]=1 # Test takes too long to run
|
||||||
|
[test_itertools.py]=1 # Syntax error - look at!
|
||||||
)
|
)
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
@@ -108,7 +109,7 @@ if [[ -n $1 ]] ; then
|
|||||||
files=$1
|
files=$1
|
||||||
SKIP_TESTS=()
|
SKIP_TESTS=()
|
||||||
else
|
else
|
||||||
files=test_*.py
|
files=test_[m]*.py
|
||||||
fi
|
fi
|
||||||
for file in $files; do
|
for file in $files; do
|
||||||
[[ -v SKIP_TESTS[$file] ]] && continue
|
[[ -v SKIP_TESTS[$file] ]] && continue
|
||||||
|
@@ -214,6 +214,10 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
self.line_number = 0
|
self.line_number = 0
|
||||||
self.ast_errors = []
|
self.ast_errors = []
|
||||||
|
|
||||||
|
# This is in Python 2.6 on. It changes the way
|
||||||
|
# strings get interpreted. See n_LOAD_CONST
|
||||||
|
self.FUTURE_UNICODE_LITERALS = False
|
||||||
|
|
||||||
# Sometimes we may want to continue decompiling when there are errors
|
# Sometimes we may want to continue decompiling when there are errors
|
||||||
# and sometimes not
|
# and sometimes not
|
||||||
self.tolerate_errors = tolerate_errors
|
self.tolerate_errors = tolerate_errors
|
||||||
@@ -644,7 +648,8 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
|
|
||||||
if self.pending_newlines:
|
if self.pending_newlines:
|
||||||
out = out[:-self.pending_newlines]
|
out = out[:-self.pending_newlines]
|
||||||
if isinstance(out, str) and not PYTHON3:
|
if (isinstance(out, str) and
|
||||||
|
not (PYTHON3 and self.FUTURE_UNICODE_LITERALS)):
|
||||||
out = unicode(out, 'utf-8')
|
out = unicode(out, 'utf-8')
|
||||||
self.f.write(out)
|
self.f.write(out)
|
||||||
|
|
||||||
@@ -843,6 +848,27 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
self.write('None')
|
self.write('None')
|
||||||
elif isinstance(data, tuple):
|
elif isinstance(data, tuple):
|
||||||
self.pp_tuple(data)
|
self.pp_tuple(data)
|
||||||
|
elif self.FUTURE_UNICODE_LITERALS:
|
||||||
|
# The FUTURE_UNICODE_LITERALS compiler flag
|
||||||
|
# in 2.6 on change the way
|
||||||
|
# strings are interpreted:
|
||||||
|
# u'xxx' -> 'xxx'
|
||||||
|
# xxx' -> b'xxx'
|
||||||
|
if isinstance(data, unicode):
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
data = str(data)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
# Have to keep data as it is: in Unicode.
|
||||||
|
pass
|
||||||
|
self.write(repr(data))
|
||||||
|
except:
|
||||||
|
from trepan.api import debug; debug()
|
||||||
|
self.write(repr(data))
|
||||||
|
elif isinstance(data, str):
|
||||||
|
self.write('b'+repr(data))
|
||||||
|
else:
|
||||||
|
self.write(repr(data))
|
||||||
else:
|
else:
|
||||||
self.write(repr(data))
|
self.write(repr(data))
|
||||||
# LOAD_CONST is a terminal, so stop processing/recursing early
|
# LOAD_CONST is a terminal, so stop processing/recursing early
|
||||||
@@ -1592,7 +1618,7 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
n_classdefdeco2 = n_classdef
|
n_classdefdeco2 = n_classdef
|
||||||
|
|
||||||
def print_super_classes(self, node):
|
def print_super_classes(self, node):
|
||||||
if not (node == 'list'):
|
if not (node == 'tuple'):
|
||||||
return
|
return
|
||||||
|
|
||||||
n_subclasses = len(node[:-1])
|
n_subclasses = len(node[:-1])
|
||||||
@@ -2378,6 +2404,9 @@ def deparse_code(version, co, out=sys.stdout, showasm=None, showast=False,
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
deparsed.FUTURE_UNICODE_LITERALS = (
|
||||||
|
COMPILER_FLAG_BIT['FUTURE_UNICODE_LITERALS'] & co.co_flags != 0)
|
||||||
|
|
||||||
# What we've been waiting for: Generate source from AST!
|
# What we've been waiting for: Generate source from AST!
|
||||||
deparsed.gen_source(deparsed.ast, co.co_name, customize)
|
deparsed.gen_source(deparsed.ast, co.co_name, customize)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user