Skip to content

Commit f57b096

Browse files
committed
Fix generated Python code for Doxygen comments with triple quotes
In addition to the changes in the previous commit, also avoid syntax errors in the generated Python docstrings by splitting them into several parts if there are 3 quotes in a row in the input, as it's impossible to have them inside triple-quoted strings, generally speaking (i.e. if there are occurrences of both """ and ''' inside the string).
1 parent b81cd1b commit f57b096

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

Examples/test-suite/doxygen_misc_constructs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,9 @@ void cycle(int id, char *fileName)
9494
/// This doc comment ends with a quote: "and that's ok"
9595
void doc_ends_with_quote() {}
9696

97+
/**
98+
This comment contains embedded triple-quoted string:
99+
100+
"""How quaint"""
101+
*/
102+
void doc_with_triple_quotes() {}

Examples/test-suite/java/doxygen_misc_constructs_runme.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ public static void main(String argv[])
188188
wantedComments.put("doxygen_misc_constructs.doxygen_misc_constructs.doc_ends_with_quote()",
189189
"This doc comment ends with a quote: \"and that's ok\"");
190190

191+
wantedComments.put("doxygen_misc_constructs.doxygen_misc_constructs.doc_with_triple_quotes()",
192+
"This comment contains embedded triple-quoted string:\n" +
193+
"\"\"\"How quaint\"\"\"");
194+
191195
// and ask the parser to check comments for us
192196
System.exit(CommentParser.check(wantedComments));
193197
}

Examples/test-suite/python/doxygen_misc_constructs_runme.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,9 @@
135135
comment_verifier.check(inspect.getdoc(doxygen_misc_constructs.doc_ends_with_quote),
136136
r'''This doc comment ends with a quote: "and that's ok"'''
137137
);
138+
139+
comment_verifier.check(inspect.getdoc(doxygen_misc_constructs.doc_with_triple_quotes),
140+
r'''This comment contains embedded triple-quoted string:
141+
142+
"""How quaint"""'''
143+
);

Source/Modules/python.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,16 @@ class PYTHON:public Language {
15991599

16001600
Append(doc, useSingleQuotes ? "r'''" : "r\"\"\"");
16011601

1602+
// We also need to avoid having triple quotes of whichever type we use, as
1603+
// this would break Python doc string syntax too. Unfortunately there is no
1604+
// way to have triple quotes inside of raw-triple-quoted string, so we have
1605+
// to break the string in parts and rely on concatenation of the adjacent
1606+
// string literals.
1607+
if (useSingleQuotes)
1608+
Replaceall(docstr, "'''", "''' \"'''\" '''");
1609+
else
1610+
Replaceall(docstr, "\"\"\"", "\"\"\" '\"\"\"' \"\"\"");
1611+
16021612
Append(doc, docstr);
16031613
Append(doc, useSingleQuotes ? "'''" : "\"\"\"");
16041614
Delete(docstr);

0 commit comments

Comments
 (0)