From f57b096c92983758b49c2231fdfd7cb0a75690f5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 3 Mar 2020 15:48:42 +0100 Subject: [PATCH] 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). --- Examples/test-suite/doxygen_misc_constructs.h | 6 ++++++ .../test-suite/java/doxygen_misc_constructs_runme.java | 4 ++++ .../test-suite/python/doxygen_misc_constructs_runme.py | 6 ++++++ Source/Modules/python.cxx | 10 ++++++++++ 4 files changed, 26 insertions(+) diff --git a/Examples/test-suite/doxygen_misc_constructs.h b/Examples/test-suite/doxygen_misc_constructs.h index 7782b532f..9e81aaf28 100644 --- a/Examples/test-suite/doxygen_misc_constructs.h +++ b/Examples/test-suite/doxygen_misc_constructs.h @@ -94,3 +94,9 @@ void cycle(int id, char *fileName) /// This doc comment ends with a quote: "and that's ok" void doc_ends_with_quote() {} +/** + This comment contains embedded triple-quoted string: + + """How quaint""" + */ +void doc_with_triple_quotes() {} diff --git a/Examples/test-suite/java/doxygen_misc_constructs_runme.java b/Examples/test-suite/java/doxygen_misc_constructs_runme.java index ca4b06b55..cae2b2192 100644 --- a/Examples/test-suite/java/doxygen_misc_constructs_runme.java +++ b/Examples/test-suite/java/doxygen_misc_constructs_runme.java @@ -188,6 +188,10 @@ public class doxygen_misc_constructs_runme { wantedComments.put("doxygen_misc_constructs.doxygen_misc_constructs.doc_ends_with_quote()", "This doc comment ends with a quote: \"and that's ok\""); + wantedComments.put("doxygen_misc_constructs.doxygen_misc_constructs.doc_with_triple_quotes()", + "This comment contains embedded triple-quoted string:\n" + + "\"\"\"How quaint\"\"\""); + // and ask the parser to check comments for us System.exit(CommentParser.check(wantedComments)); } diff --git a/Examples/test-suite/python/doxygen_misc_constructs_runme.py b/Examples/test-suite/python/doxygen_misc_constructs_runme.py index 5655b2cef..44f607fee 100644 --- a/Examples/test-suite/python/doxygen_misc_constructs_runme.py +++ b/Examples/test-suite/python/doxygen_misc_constructs_runme.py @@ -135,3 +135,9 @@ Spaces at the start of line should be taken into account: comment_verifier.check(inspect.getdoc(doxygen_misc_constructs.doc_ends_with_quote), r'''This doc comment ends with a quote: "and that's ok"''' ); + +comment_verifier.check(inspect.getdoc(doxygen_misc_constructs.doc_with_triple_quotes), + r'''This comment contains embedded triple-quoted string: + + """How quaint"""''' +); diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index a4ac11811..c8c45df35 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -1599,6 +1599,16 @@ public: Append(doc, useSingleQuotes ? "r'''" : "r\"\"\""); + // We also need to avoid having triple quotes of whichever type we use, as + // this would break Python doc string syntax too. Unfortunately there is no + // way to have triple quotes inside of raw-triple-quoted string, so we have + // to break the string in parts and rely on concatenation of the adjacent + // string literals. + if (useSingleQuotes) + Replaceall(docstr, "'''", "''' \"'''\" '''"); + else + Replaceall(docstr, "\"\"\"", "\"\"\" '\"\"\"' \"\"\""); + Append(doc, docstr); Append(doc, useSingleQuotes ? "'''" : "\"\"\""); Delete(docstr);