fix unicode docstring again, handling unicode string in py2, fix docstring indentation

This commit is contained in:
x0ret
2019-05-28 15:11:44 +04:30
parent 3b3fc09b60
commit 39c12704a8
3 changed files with 27 additions and 30 deletions

View File

@@ -41,6 +41,16 @@ def dq7():
5
"""
def dq8():
u""" <----- SEE 'u' HERE
>>> mylen(u"تست")
5
"""
assert dq8.__doc__ == u""" <----- SEE 'u' HERE
>>> mylen(u"تست")
5
"""
def baz():
"""
... '''>>> assert 1 == 1
@@ -78,4 +88,5 @@ dq4()
dq5()
dq6()
dq7()
dq8()
baz()

View File

@@ -107,22 +107,14 @@ def print_docstring(self, indent, docstring):
self.write(indent)
if not PYTHON3 and not isinstance(docstring, str):
# Must be unicode in Python2
if self.version >= 2.4:
if self.version > 2.7:
docstring = repr(docstring.expandtabs())[2:-1].decode("unicode-escape")
else:
self.write('u')
docstring = repr(docstring.expandtabs())[2:-1].decode("string-escape")\
.decode("utf-8", errors="ignore")
else:
docstring = repr(docstring.expandtabs())[2:-1]
self.write('u')
docstring = repr(docstring.expandtabs())[2:-1]
elif PYTHON3 and 2.4 <= self.version <= 2.7:
# TODO: check for unicode string
try:
docstring = repr(docstring.expandtabs())[1:-1].encode("latin-1").decode("utf-8")
repr(docstring.expandtabs())[1:-1].encode("ascii")
except UnicodeEncodeError:
self.write('u')
docstring = repr(docstring.expandtabs())[1:-1]
docstring = repr(docstring.expandtabs())[1:-1]
else:
docstring = repr(docstring.expandtabs())[1:-1]
@@ -163,33 +155,22 @@ def print_docstring(self, indent, docstring):
docstring = docstring.replace('\t', '\\\\')
lines = docstring.split('\n')
calculate_indent = maxint
for line in lines[1:]:
stripped = line.lstrip()
if len(stripped) > 0:
calculate_indent = min(calculate_indent, len(line) - len(stripped))
calculate_indent = min(calculate_indent, len(lines[-1]) - len(lines[-1].lstrip()))
# Remove indentation (first line is special):
trimmed = [lines[0]]
if calculate_indent < maxint:
trimmed += [line[calculate_indent:] for line in lines[1:]]
self.write(quote)
if len(trimmed) == 0:
if len(lines) == 0:
self.println(quote)
elif len(trimmed) == 1:
self.println(trimmed[0], quote)
elif len(lines) == 1:
self.println(lines[0], quote)
else:
self.println(trimmed[0])
for line in trimmed[1:-1]:
self.println(lines[0])
for line in lines[1:-1]:
if line:
self.println( indent, line )
self.println( line )
else:
self.println( "\n\n" )
pass
pass
self.println(indent, trimmed[-1], quote)
self.println(lines[-1], quote)
return True

View File

@@ -610,6 +610,11 @@ class SourceWalker(GenericASTTraversal, object):
else:
self.write(repr(data))
else:
if not PYTHON3:
try:
repr(data).encode("ascii")
except UnicodeEncodeError:
self.write('u')
self.write(repr(data))
# LOAD_CONST is a terminal, so stop processing/recursing early
self.prune()