Skip to content

Commit a5cdb50

Browse files
author
x0ret
committed
towards supporting unicode: docstring
1 parent 792ef5b commit a5cdb50

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

uncompyle6/semantics/helper.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,21 @@ def print_docstring(self, indent, docstring):
105105
self.write(indent)
106106
if not PYTHON3 and not isinstance(docstring, str):
107107
# Must be unicode in Python2
108-
self.write('u')
109-
docstring = repr(docstring.expandtabs())[2:-1]
108+
if self.version >= 2.4:
109+
if self.version > 2.7:
110+
docstring = repr(docstring.expandtabs())[2:-1].decode("unicode-escape")
111+
else:
112+
self.write('u')
113+
docstring = repr(docstring.expandtabs())[2:-1].decode("string-escape").decode("utf-8")
114+
else:
115+
docstring = repr(docstring.expandtabs())[2:-1]
116+
elif PYTHON3 and 2.4 <= self.version <= 2.7:
117+
# TODO: check for unicode string
118+
try:
119+
docstring = repr(docstring.expandtabs())[1:-1].encode("latin-1").decode("utf-8")
120+
except UnicodeEncodeError:
121+
self.write('u')
122+
docstring = repr(docstring.expandtabs())[1:-1]
110123
else:
111124
docstring = repr(docstring.expandtabs())[1:-1]
112125

uncompyle6/semantics/pysource.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,10 @@ def traverse(self, node, indent=None, is_lambda=False):
363363
def write(self, *data):
364364
if (len(data) == 0) or (len(data) == 1 and data[0] == ''):
365365
return
366-
out = ''.join((str(j) for j in data))
366+
if not PYTHON3:
367+
out = ''.join((unicode(j) for j in data))
368+
else:
369+
out = ''.join((str(j) for j in data))
367370
n = 0
368371
for i in out:
369372
if i == '\n':

0 commit comments

Comments
 (0)