Skip to content

Commit

Permalink
Escape backslashes in stub output (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
auscompgeek authored Mar 4, 2024
1 parent 99f14a1 commit f783045
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pybind11_stubgen/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def print_class_body(self, class_: Class) -> list[str]:
def print_docstring(self, doc: Docstring) -> list[str]:
return [
'"""',
*(line.replace('"""', r"\"\"\"") for line in doc.splitlines()),
*(
line.replace("\\", r"\\").replace('"""', r"\"\"\"")
for line in doc.splitlines()
),
'"""',
]

Expand Down
9 changes: 9 additions & 0 deletions tests/py-demo/bindings/src/modules/issues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ void bind_issues_module(py::module &&m) {
"Tuning parameter (0 rad⁻¹ < zeta < 1 rad⁻¹) for which larger\n"
"values provide more damping in response."));
}
{
m.def("backslashes_should_be_escaped", [] {}, R"docstring(
\brief A brief description of this function.
A detailed description of this function.
Here's some reStructuredText: :math:`x = [x, y, \theta]^T`
)docstring");
}
{
// https://github.com/sizmailov/pybind11-stubgen/issues/86
auto cleanup_callback = []() { /* ... */ };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ from __future__ import annotations

import typing

__all__ = ["issue_51_catastrophic_regex", "issue_73_utf8_doc_chars"]
__all__ = [
"backslashes_should_be_escaped",
"issue_51_catastrophic_regex",
"issue_73_utf8_doc_chars",
]

def backslashes_should_be_escaped() -> None:
"""
\\brief A brief description of this function.
A detailed description of this function.
Here's some reStructuredText: :math:`x = [x, y, \\theta]^T`
"""

def issue_51_catastrophic_regex(arg0: int, arg1: int) -> None:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ from __future__ import annotations

import typing

__all__ = ["issue_51_catastrophic_regex", "issue_73_utf8_doc_chars"]
__all__ = [
"backslashes_should_be_escaped",
"issue_51_catastrophic_regex",
"issue_73_utf8_doc_chars",
]

def backslashes_should_be_escaped() -> None:
"""
\\brief A brief description of this function.
A detailed description of this function.
Here's some reStructuredText: :math:`x = [x, y, \\theta]^T`
"""

def issue_51_catastrophic_regex(arg0: int, arg1: int) -> None:
"""
Expand Down

0 comments on commit f783045

Please sign in to comment.