Skip to content

Commit b81cd1b

Browse files
committed
Fix generated Python code for Doxygen comments ending with quote
Single-line Doxygen comments ending with a double quote resulted in syntactically-invalid Python docstrings in the output, so use triple single quotes as delimiters in this case to avoid it.
1 parent 3a32956 commit b81cd1b

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

Examples/test-suite/doxygen_misc_constructs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,6 @@ 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

Examples/test-suite/java/doxygen_misc_constructs_runme.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ 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\"");
188190

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

Examples/test-suite/python/doxygen_misc_constructs_runme.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@
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+
);

Source/Modules/python.cxx

Lines changed: 17 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,22 @@ 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+
15881602
Append(doc, docstr);
1589-
Append(doc, "\"\"\"");
1603+
Append(doc, useSingleQuotes ? "'''" : "\"\"\"");
15901604
Delete(docstr);
15911605

15921606
return doc;

0 commit comments

Comments
 (0)