@@ -52,6 +52,33 @@ PYBIND11_WARNING_DISABLE_MSVC(4127)
52
52
53
53
PYBIND11_NAMESPACE_BEGIN(detail)
54
54
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
+
55
82
// Apply all the extensions translators from a list
56
83
// Return true if one of the translators completed without raising an exception
57
84
// itself. Return of false indicates that if there are other translators
@@ -424,7 +451,7 @@ class cpp_function : public function {
424
451
// Write default value if available.
425
452
if (!is_starred && arg_index < rec->args .size () && rec->args [arg_index].descr ) {
426
453
signature += " = " ;
427
- signature += rec->args [arg_index].descr ;
454
+ signature += detail::replaceNewlinesAndSquash ( rec->args [arg_index].descr ) ;
428
455
}
429
456
// Separator for positional-only arguments (placed after the
430
457
// argument, rather than before like *
@@ -462,7 +489,6 @@ class cpp_function : public function {
462
489
pybind11_fail (" Internal error while parsing type signature (2)" );
463
490
}
464
491
465
- signature.erase (std::remove (signature.begin (), signature.end (), ' \n ' ), signature.end ());
466
492
rec->signature = guarded_strdup (signature.c_str ());
467
493
rec->args .shrink_to_fit ();
468
494
rec->nargs = (std::uint16_t ) args;
0 commit comments