From 9018a9a90bc599a08e7c25959fa674297f64f151 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 28 Sep 2024 15:30:30 +0100 Subject: [PATCH] Python docstring whitespace strip for single lines Python does not consistently handle whitespace stripping of contents in __doc__ across different versions and interfaces. For example python-3.13 heap types don't strip whitespace but but static types do. Python-3.12 and earlier didn't strip any whitespace either. inspect.getdoc() does consistently remove docstrings though. Given the whitespace is pointless, SWIG now strips this off for single line docstrings. --- .../python/python_docstring_runme.py | 21 ++++++++++++------- Examples/test-suite/python_docstring.i | 8 +++++-- Source/Modules/python.cxx | 11 ++++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Examples/test-suite/python/python_docstring_runme.py b/Examples/test-suite/python/python_docstring_runme.py index 101f9dd8a..924dc2671 100644 --- a/Examples/test-suite/python/python_docstring_runme.py +++ b/Examples/test-suite/python/python_docstring_runme.py @@ -87,16 +87,21 @@ check(inspect.getdoc(DocStrings.docstringC), "second line" ) -# One line doc special case, use __doc__ -if sys.version_info[0:2] < (3, 13): - check(DocStrings.docstringX.__doc__, " one line docs") -else: - check(DocStrings.docstringX.__doc__, "one line docs") +# One line doc special case, use __doc__ to check for stripped whitespace +check(DocStrings.docstringW.__doc__, "one line docs") +check(DocStrings.docstringX.__doc__, "one line docs leading whitespace") +check(DocStrings.docstringY.__doc__, "one line docs trailing whitespace") +check(DocStrings.docstringZ.__doc__, "one line docs whitespace") +check(inspect.getdoc(DocStrings.docstringW), + "one line docs" + ) check(inspect.getdoc(DocStrings.docstringX), - "one line docs" + "one line docs leading whitespace" ) - check(inspect.getdoc(DocStrings.docstringY), - "one line docs" + "one line docs trailing whitespace" + ) +check(inspect.getdoc(DocStrings.docstringZ), + "one line docs whitespace" ) diff --git a/Examples/test-suite/python_docstring.i b/Examples/test-suite/python_docstring.i index 3b88167eb..23002a5d1 100644 --- a/Examples/test-suite/python_docstring.i +++ b/Examples/test-suite/python_docstring.i @@ -77,8 +77,10 @@ line 3 %{ first line second line%} -%feature("docstring") docstringX " one line docs" -%feature("docstring") docstringY "one line docs" +%feature("docstring") docstringW "one line docs" +%feature("docstring") docstringX " one line docs leading whitespace" +%feature("docstring") docstringY "one line docs trailing whitespace\t " +%feature("docstring") docstringZ "\tone line docs whitespace \t" %inline %{ struct DocStrings { @@ -92,7 +94,9 @@ struct DocStrings { void docstringA() {} void docstringB() {} void docstringC() {} + void docstringW() {} void docstringX() {} void docstringY() {} + void docstringZ() {} }; %} diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 3fbb22764..fe2603311 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -1614,6 +1614,17 @@ public: Append(tmp, indent); Delete(docstr); docstr = tmp; + } else { + // Removing leading and trailing whitespace for single line docstrings + Chop(docstr); + const char *c = Char(docstr); + if (isspace((int)*c)) { + while(isspace((int)*(++c))) { + } + String *old_docstr = docstr; + docstr = NewString(c); + Delete(old_docstr); + } } return docstr;