Skip to content

Commit 991c2af

Browse files
committed
Merge branch 'python-doxygen-quotes'
* python-doxygen-quotes: Fix generated Python code for Doxygen comments with triple quotes Fix generated Python code for Doxygen comments ending with quote
2 parents 2e846e5 + f57b096 commit 991c2af

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

Examples/test-suite/doxygen_misc_constructs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,12 @@ void backslashC()
9191
void cycle(int id, char *fileName)
9292
{}
9393

94+
/// This doc comment ends with a quote: "and that's ok"
95+
void doc_ends_with_quote() {}
9496

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ public static void main(String argv[])
185185
"\n" +
186186
" @param fileName name of the log file\n");
187187

188+
wantedComments.put("doxygen_misc_constructs.doxygen_misc_constructs.doc_ends_with_quote()",
189+
"This doc comment ends with a quote: \"and that's ok\"");
190+
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\"\"\"");
188194

189195
// and ask the parser to check comments for us
190196
System.exit(CommentParser.check(wantedComments));

Examples/test-suite/python/doxygen_misc_constructs_runme.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,13 @@
131131
:type fileName: string
132132
:param fileName: name of the log file"""
133133
);
134+
135+
comment_verifier.check(inspect.getdoc(doxygen_misc_constructs.doc_ends_with_quote),
136+
r'''This doc comment ends with a quote: "and that's ok"'''
137+
);
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: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,8 @@ class PYTHON:public Language {
15711571

15721572
String *docstring(Node *n, autodoc_t ad_type, const String *indent, bool low_level = false) {
15731573
String *docstr = build_combined_docstring(n, ad_type, indent, low_level);
1574-
if (!Len(docstr))
1574+
const int len = Len(docstr);
1575+
if (!len)
15751576
return docstr;
15761577

15771578
// Notice that all comments are created as raw strings (prefix "r"),
@@ -1584,9 +1585,32 @@ class PYTHON:public Language {
15841585
// escape '\x'. '\' may additionally appear in verbatim or htmlonly sections
15851586
// of doxygen doc, Latex expressions, ...
15861587
String *doc = NewString("");
1587-
Append(doc, "r\"\"\"");
1588+
1589+
// Determine which kind of quotes to use as delimiters: for single line
1590+
// strings we can avoid problems with having a quote as the last character
1591+
// of the docstring by using different kind of quotes as delimiters. For
1592+
// multi-line strings this problem doesn't arise, as we always have a new
1593+
// line or spaces at the end of it, but it still does no harm to do it for
1594+
// them too.
1595+
//
1596+
// Note: we use double quotes by default, i.e. if there is no reason to
1597+
// prefer using single ones, for consistency with the older SWIG versions.
1598+
const bool useSingleQuotes = (Char(docstr))[len - 1] == '"';
1599+
1600+
Append(doc, useSingleQuotes ? "r'''" : "r\"\"\"");
1601+
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+
15881612
Append(doc, docstr);
1589-
Append(doc, "\"\"\"");
1613+
Append(doc, useSingleQuotes ? "'''" : "\"\"\"");
15901614
Delete(docstr);
15911615

15921616
return doc;

0 commit comments

Comments
 (0)