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

View File

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

View File

@@ -610,6 +610,11 @@ class SourceWalker(GenericASTTraversal, object):
else: else:
self.write(repr(data)) self.write(repr(data))
else: else:
if not PYTHON3:
try:
repr(data).encode("ascii")
except UnicodeEncodeError:
self.write('u')
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
self.prune() self.prune()