diff --git a/pybind11_stubgen/printer.py b/pybind11_stubgen/printer.py index 1f755aa..8ef4af2 100644 --- a/pybind11_stubgen/printer.py +++ b/pybind11_stubgen/printer.py @@ -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() + ), '"""', ] diff --git a/tests/py-demo/bindings/src/modules/issues.cpp b/tests/py-demo/bindings/src/modules/issues.cpp index 3b56fbf..c1bd6a8 100644 --- a/tests/py-demo/bindings/src/modules/issues.cpp +++ b/tests/py-demo/bindings/src/modules/issues.cpp @@ -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 = []() { /* ... */ }; diff --git a/tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi b/tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi index 09af4e4..09cc9d0 100644 --- a/tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi +++ b/tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi @@ -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: """ diff --git a/tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi b/tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi index 09af4e4..09cc9d0 100644 --- a/tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi +++ b/tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/issues.pyi @@ -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: """