diff --git a/test/bytecode_2.7/00_docstring.pyc b/test/bytecode_2.7/00_docstring.pyc new file mode 100644 index 00000000..505f7c03 Binary files /dev/null and b/test/bytecode_2.7/00_docstring.pyc differ diff --git a/test/simple_source/stmts/00_docstring.py b/test/simple_source/stmts/00_docstring.py new file mode 100644 index 00000000..194eadc9 --- /dev/null +++ b/test/simple_source/stmts/00_docstring.py @@ -0,0 +1,7 @@ +# uncompyle2 bug was not escaping """ properly +r'''func placeholder - with ("""\nstring\n""")''' +def foo(): + r'''func placeholder - ' and with ("""\nstring\n""")''' + +def bar(): + r"""func placeholder - ' and with ('''\nstring\n''') and \"\"\"\nstring\n\"\"\" """ diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index 1f104ac0..17221ec0 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -741,7 +741,12 @@ class SourceWalker(GenericASTTraversal, object): self.pending_newlines = max(self.pending_newlines, 1) def print_docstring(self, indent, docstring): - quote = '"""' + ## FIXME: put this into a testable function. + if docstring.find('"""') == -1: + quote = '"""' + else: + quote = "'''" + self.write(indent) if not PYTHON3 and not isinstance(docstring, str): # Must be unicode in Python2 @@ -774,10 +779,11 @@ class SourceWalker(GenericASTTraversal, object): # ruin the ending triple quote if len(docstring) and docstring[-1] == '"': docstring = docstring[:-1] + '\\"' - # Escape triple quote anywhere - docstring = docstring.replace('"""', '\\"\\"\\"') # Restore escaped backslashes 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:]: