Skip to content

Commit f783045

Browse files
authored
Escape backslashes in stub output (#208)
1 parent 99f14a1 commit f783045

File tree

4 files changed

+41
-3
lines changed
  • pybind11_stubgen
  • tests
    • py-demo/bindings/src/modules
    • stubs/python-3.12
      • pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings
      • pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings

4 files changed

+41
-3
lines changed

pybind11_stubgen/printer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ def print_class_body(self, class_: Class) -> list[str]:
122122
def print_docstring(self, doc: Docstring) -> list[str]:
123123
return [
124124
'"""',
125-
*(line.replace('"""', r"\"\"\"") for line in doc.splitlines()),
125+
*(
126+
line.replace("\\", r"\\").replace('"""', r"\"\"\"")
127+
for line in doc.splitlines()
128+
),
126129
'"""',
127130
]
128131

tests/py-demo/bindings/src/modules/issues.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ void bind_issues_module(py::module &&m) {
2222
"Tuning parameter (0 rad⁻¹ < zeta < 1 rad⁻¹) for which larger\n"
2323
"values provide more damping in response."));
2424
}
25+
{
26+
m.def("backslashes_should_be_escaped", [] {}, R"docstring(
27+
\brief A brief description of this function.
28+
29+
A detailed description of this function.
30+
31+
Here's some reStructuredText: :math:`x = [x, y, \theta]^T`
32+
)docstring");
33+
}
2534
{
2635
// https://github.com/sizmailov/pybind11-stubgen/issues/86
2736
auto cleanup_callback = []() { /* ... */ };

tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@ from __future__ import annotations
22

33
import typing
44

5-
__all__ = ["issue_51_catastrophic_regex", "issue_73_utf8_doc_chars"]
5+
__all__ = [
6+
"backslashes_should_be_escaped",
7+
"issue_51_catastrophic_regex",
8+
"issue_73_utf8_doc_chars",
9+
]
10+
11+
def backslashes_should_be_escaped() -> None:
12+
"""
13+
\\brief A brief description of this function.
14+
15+
A detailed description of this function.
16+
17+
Here's some reStructuredText: :math:`x = [x, y, \\theta]^T`
18+
"""
619

720
def issue_51_catastrophic_regex(arg0: int, arg1: int) -> None:
821
"""

tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@ from __future__ import annotations
22

33
import typing
44

5-
__all__ = ["issue_51_catastrophic_regex", "issue_73_utf8_doc_chars"]
5+
__all__ = [
6+
"backslashes_should_be_escaped",
7+
"issue_51_catastrophic_regex",
8+
"issue_73_utf8_doc_chars",
9+
]
10+
11+
def backslashes_should_be_escaped() -> None:
12+
"""
13+
\\brief A brief description of this function.
14+
15+
A detailed description of this function.
16+
17+
Here's some reStructuredText: :math:`x = [x, y, \\theta]^T`
18+
"""
619

720
def issue_51_catastrophic_regex(arg0: int, arg1: int) -> None:
821
"""

0 commit comments

Comments
 (0)