@@ -52,6 +52,33 @@ PYBIND11_WARNING_DISABLE_MSVC(4127)
5252
5353PYBIND11_NAMESPACE_BEGIN(detail)
5454
55+ inline std::string replaceNewlinesAndSquash(const char * text) {
56+ std::string result;
57+
58+ // Replace newlines with spaces and squash consecutive spaces
59+ while (*text) {
60+ if (*text == ' \n ' ) {
61+ result += ' ' ;
62+ while (*(text + 1 ) == ' ' ) {
63+ ++text;
64+ }
65+ } else {
66+ result += *text;
67+ }
68+ ++text;
69+ }
70+
71+ // Strip leading and trailing spaces
72+ result.erase (result.begin (), std::find_if (result.begin (), result.end (), [](unsigned char ch) {
73+ return !std::isspace (ch);
74+ }));
75+ result.erase (std::find_if (result.rbegin (), result.rend (), [](unsigned char ch) {
76+ return !std::isspace (ch);
77+ }).base (), result.end ());
78+
79+ return result;
80+ }
81+
5582// Apply all the extensions translators from a list
5683// Return true if one of the translators completed without raising an exception
5784// itself. Return of false indicates that if there are other translators
@@ -424,7 +451,7 @@ class cpp_function : public function {
424451 // Write default value if available.
425452 if (!is_starred && arg_index < rec->args .size () && rec->args [arg_index].descr ) {
426453 signature += " = " ;
427- signature += rec->args [arg_index].descr ;
454+ signature += detail::replaceNewlinesAndSquash ( rec->args [arg_index].descr ) ;
428455 }
429456 // Separator for positional-only arguments (placed after the
430457 // argument, rather than before like *
@@ -462,7 +489,6 @@ class cpp_function : public function {
462489 pybind11_fail (" Internal error while parsing type signature (2)" );
463490 }
464491
465- signature.erase (std::remove (signature.begin (), signature.end (), ' \n ' ), signature.end ());
466492 rec->signature = guarded_strdup (signature.c_str ());
467493 rec->args .shrink_to_fit ();
468494 rec->nargs = (std::uint16_t ) args;
0 commit comments