Skip to content

Commit 20d0a60

Browse files
committed
Remove duplicate code of print_docstring
1 parent 4cd10b7 commit 20d0a60

File tree

1 file changed

+2
-65
lines changed

1 file changed

+2
-65
lines changed

uncompyle6/semantics/n_actions.py

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
PRECEDENCE,
2727
minint,
2828
)
29-
from uncompyle6.semantics.helper import find_code_node, flatten_list
29+
from uncompyle6.semantics.helper import find_code_node, flatten_list, print_docstring
3030
from uncompyle6.util import better_repr, get_code_name
3131

3232

@@ -541,70 +541,7 @@ def n_docstring(self, node):
541541
else:
542542
docstring = node[0].pattr
543543

544-
quote = '"""'
545-
if docstring.find(quote) >= 0:
546-
if docstring.find("'''") == -1:
547-
quote = "'''"
548-
549-
self.write(indent)
550-
docstring = repr(docstring.expandtabs())[1:-1]
551-
552-
for orig, replace in (
553-
("\\\\", "\t"),
554-
("\\r\\n", "\n"),
555-
("\\n", "\n"),
556-
("\\r", "\n"),
557-
('\\"', '"'),
558-
("\\'", "'"),
559-
):
560-
docstring = docstring.replace(orig, replace)
561-
562-
# Do a raw string if there are backslashes but no other escaped characters:
563-
# also check some edge cases
564-
if (
565-
"\t" in docstring
566-
and "\\" not in docstring
567-
and len(docstring) >= 2
568-
and docstring[-1] != "\t"
569-
and (docstring[-1] != '"' or docstring[-2] == "\t")
570-
):
571-
self.write("r") # raw string
572-
# Restore backslashes unescaped since raw
573-
docstring = docstring.replace("\t", "\\")
574-
else:
575-
# Escape the last character if it is the same as the
576-
# triple quote character.
577-
quote1 = quote[-1]
578-
if len(docstring) and docstring[-1] == quote1:
579-
docstring = docstring[:-1] + "\\" + quote1
580-
581-
# Escape triple quote when needed
582-
if quote == '"""':
583-
replace_str = '\\"""'
584-
else:
585-
assert quote == "'''"
586-
replace_str = "\\'''"
587-
588-
docstring = docstring.replace(quote, replace_str)
589-
docstring = docstring.replace("\t", "\\\\")
590-
591-
lines = docstring.split("\n")
592-
593-
self.write(quote)
594-
if len(lines) == 0:
595-
self.println(quote)
596-
elif len(lines) == 1:
597-
self.println(lines[0], quote)
598-
else:
599-
self.println(lines[0])
600-
for line in lines[1:-1]:
601-
if line:
602-
self.println(line)
603-
else:
604-
self.println("\n\n")
605-
pass
606-
pass
607-
self.println(lines[-1], quote)
544+
print_docstring(self, indent, docstring)
608545
self.prune()
609546

610547
def n_elifelsestmtr(self, node: SyntaxTree):

0 commit comments

Comments
 (0)