You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
Fix bug in docstring triple quotes
Problem was not escaping """ inside """. Use ''' when possible; and when not, use: \"\"\".
This commit is contained in:
BIN
test/bytecode_2.7/00_docstring.pyc
Normal file
BIN
test/bytecode_2.7/00_docstring.pyc
Normal file
Binary file not shown.
7
test/simple_source/stmts/00_docstring.py
Normal file
7
test/simple_source/stmts/00_docstring.py
Normal file
@@ -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\"\"\" """
|
@@ -741,7 +741,12 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
self.pending_newlines = max(self.pending_newlines, 1)
|
self.pending_newlines = max(self.pending_newlines, 1)
|
||||||
|
|
||||||
def print_docstring(self, indent, docstring):
|
def print_docstring(self, indent, docstring):
|
||||||
quote = '"""'
|
## FIXME: put this into a testable function.
|
||||||
|
if docstring.find('"""') == -1:
|
||||||
|
quote = '"""'
|
||||||
|
else:
|
||||||
|
quote = "'''"
|
||||||
|
|
||||||
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
|
||||||
@@ -774,10 +779,11 @@ class SourceWalker(GenericASTTraversal, object):
|
|||||||
# ruin the ending triple quote
|
# ruin the ending triple quote
|
||||||
if len(docstring) and docstring[-1] == '"':
|
if len(docstring) and docstring[-1] == '"':
|
||||||
docstring = docstring[:-1] + '\\"'
|
docstring = docstring[:-1] + '\\"'
|
||||||
# Escape triple quote anywhere
|
|
||||||
docstring = docstring.replace('"""', '\\"\\"\\"')
|
|
||||||
# Restore escaped backslashes
|
# Restore escaped backslashes
|
||||||
docstring = docstring.replace('\t', '\\\\')
|
docstring = docstring.replace('\t', '\\\\')
|
||||||
|
# Escape triple quote when needed
|
||||||
|
if quote == '""""':
|
||||||
|
docstring = docstring.replace('"""', '\\"\\"\\"')
|
||||||
lines = docstring.split('\n')
|
lines = docstring.split('\n')
|
||||||
calculate_indent = maxint
|
calculate_indent = maxint
|
||||||
for line in lines[1:]:
|
for line in lines[1:]:
|
||||||
|
Reference in New Issue
Block a user