diff --git a/pytest/test_docstring.py b/pytest/test_docstring.py deleted file mode 100644 index 21157b5f..00000000 --- a/pytest/test_docstring.py +++ /dev/null @@ -1,78 +0,0 @@ -import sys -from uncompyle6 import PYTHON3 -if PYTHON3: - from io import StringIO - minint = -sys.maxsize-1 - maxint = sys.maxsize -else: - from StringIO import StringIO - minint = -sys.maxint-1 - maxint = sys.maxint -from uncompyle6.semantics.helper import print_docstring - -class PrintFake(): - def __init__(self): - self.pending_newlines = 0 - self.f = StringIO() - - def write(self, *data): - if (len(data) == 0) or (len(data) == 1 and data[0] == ''): - return - out = ''.join((str(j) for j in data)) - n = 0 - for i in out: - if i == '\n': - n += 1 - if n == len(out): - self.pending_newlines = max(self.pending_newlines, n) - return - elif n: - self.pending_newlines = max(self.pending_newlines, n) - out = out[n:] - break - else: - break - - if self.pending_newlines > 0: - self.f.write('\n'*self.pending_newlines) - self.pending_newlines = 0 - - for i in out[::-1]: - if i == '\n': - self.pending_newlines += 1 - else: - break - - if self.pending_newlines: - out = out[:-self.pending_newlines] - self.f.write(out) - def println(self, *data): - if data and not(len(data) == 1 and data[0] == ''): - self.write(*data) - self.pending_newlines = max(self.pending_newlines, 1) - return - pass - -def test_docstring(): - - for doc, expect in ( - ("Now is the time", - ' """Now is the time"""'), - (""" -Now is the time -""", - ''' """ - Now is the time - """''') - - # (r'''func placeholder - ' and with ("""\nstring\n """)''', - # """ r'''func placeholder - ' and with (\"\"\"\nstring\n\"\"\")'''"""), - # (r"""func placeholder - ' and with ('''\nstring\n''') and \"\"\"\nstring\n\"\"\" """, - # """ r\"\"\"func placeholder - ' and with ('''\nstring\n''') and \"\"\"\nstring\n\"\"\" \"\"\"""") - ): - - o = PrintFake() - # print(doc) - # print(expect) - print_docstring(o, ' ', doc) - assert expect == o.f.getvalue() diff --git a/test/bytecode_2.7/00_docstring.pyc b/test/bytecode_2.7/00_docstring.pyc deleted file mode 100644 index 61aa0c01..00000000 Binary files a/test/bytecode_2.7/00_docstring.pyc and /dev/null differ diff --git a/test/bytecode_2.7_run/00_docstring.pyc b/test/bytecode_2.7_run/00_docstring.pyc new file mode 100644 index 00000000..8f67e566 Binary files /dev/null and b/test/bytecode_2.7_run/00_docstring.pyc differ diff --git a/test/bytecode_3.7/00_docstring.pyc b/test/bytecode_3.7/00_docstring.pyc deleted file mode 100644 index 2c80e86a..00000000 Binary files a/test/bytecode_3.7/00_docstring.pyc and /dev/null differ diff --git a/test/bytecode_3.7_run/00_docstring.pyc b/test/bytecode_3.7_run/00_docstring.pyc new file mode 100644 index 00000000..fa450ff1 Binary files /dev/null and b/test/bytecode_3.7_run/00_docstring.pyc differ diff --git a/uncompyle6/semantics/helper.py b/uncompyle6/semantics/helper.py index f213e1ff..f941831e 100644 --- a/uncompyle6/semantics/helper.py +++ b/uncompyle6/semantics/helper.py @@ -99,14 +99,9 @@ def strip_quotes(str): def print_docstring(self, indent, docstring): - try: - if docstring.find('"""') == -1: - quote = '"""' - else: - quote = "'''" - docstring = docstring.replace("'''", "\\'''") - except: - return False + quote = '"""' + if docstring.find("'''") == -1: + quote = "'''" self.write(indent) if not PYTHON3 and not isinstance(docstring, str): # Must be unicode in Python2 @@ -132,18 +127,31 @@ def print_docstring(self, indent, docstring): and (docstring[-1] != '"' or docstring[-2] == '\t')): self.write('r') # raw string - # restore backslashes unescaped since raw + # Restore backslashes unescaped since raw docstring = docstring.replace('\t', '\\') else: # Escape '"' if it's the last character, so it doesn't # ruin the ending triple quote if len(docstring) and docstring[-1] == '"': docstring = docstring[:-1] + '\\"' - # Restore escaped backslashes + + # Escape triple quote when needed + if quote == '"""': + if self.version > 2.7: + replace_str = '\\"""' + else: + replace_str = '\\"\\"\\"' + docstring = docstring.replace(quote, replace_str) + else: + assert quote == "'''" + if self.version > 2.7: + replace_str = "\\'''" + else: + replace_str = "\\'\\'\\'" + docstring = docstring.replace(quote, replace_str) + docstring = docstring.replace('\t', '\\\\') - # Escape triple quote when needed - if quote == '""""': - docstring = docstring.replace('"""', '\\"\\"\\"') + lines = docstring.split('\n') calculate_indent = maxint for line in lines[1:]: @@ -152,6 +160,7 @@ def print_docstring(self, indent, docstring): 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:]] @@ -164,7 +173,12 @@ def print_docstring(self, indent, docstring): else: self.println(trimmed[0]) for line in trimmed[1:-1]: - self.println( indent, line ) + if line: + self.println( indent, line ) + else: + self.println( "\n\n" ) + pass + pass self.println(indent, trimmed[-1], quote) return True