From c89e5b2ddfb508ce10925d8bc171c03fb46ad97d Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Sat, 8 Jul 2023 16:20:44 +0200 Subject: [PATCH 01/24] Remove newlines from docstring signature --- include/pybind11/pybind11.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 3bce1a01ba..decb340f98 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -462,6 +462,7 @@ class cpp_function : public function { pybind11_fail("Internal error while parsing type signature (2)"); } + signature.erase(std::remove(signature.begin(), signature.end(), '\n'), signature.end()); rec->signature = guarded_strdup(signature.c_str()); rec->args.shrink_to_fit(); rec->nargs = (std::uint16_t) args; From 214b200cad86737a35adfc4c5890ad6819154f1e Mon Sep 17 00:00:00 2001 From: Jean Elsner Date: Sat, 8 Jul 2023 21:20:38 +0200 Subject: [PATCH 02/24] Jean/dev (#1) Replace newlines in arg values with spaces --- include/pybind11/pybind11.h | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index decb340f98..84d668f001 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -52,6 +52,33 @@ PYBIND11_WARNING_DISABLE_MSVC(4127) PYBIND11_NAMESPACE_BEGIN(detail) +inline std::string replaceNewlinesAndSquash(const char* text) { + std::string result; + + // Replace newlines with spaces and squash consecutive spaces + while (*text) { + if (*text == '\n') { + result += ' '; + while (*(text + 1) == ' ') { + ++text; + } + } else { + result += *text; + } + ++text; + } + + // Strip leading and trailing spaces + result.erase(result.begin(), std::find_if(result.begin(), result.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); + result.erase(std::find_if(result.rbegin(), result.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), result.end()); + + return result; +} + // Apply all the extensions translators from a list // Return true if one of the translators completed without raising an exception // itself. Return of false indicates that if there are other translators @@ -424,7 +451,7 @@ class cpp_function : public function { // Write default value if available. if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) { signature += " = "; - signature += rec->args[arg_index].descr; + signature += detail::replaceNewlinesAndSquash(rec->args[arg_index].descr); } // Separator for positional-only arguments (placed after the // argument, rather than before like * @@ -462,7 +489,6 @@ class cpp_function : public function { pybind11_fail("Internal error while parsing type signature (2)"); } - signature.erase(std::remove(signature.begin(), signature.end(), '\n'), signature.end()); rec->signature = guarded_strdup(signature.c_str()); rec->args.shrink_to_fit(); rec->nargs = (std::uint16_t) args; From 99cd8d0a18bd87eb9ac89d9891057ce2794b2d08 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 8 Jul 2023 19:20:59 +0000 Subject: [PATCH 03/24] style: pre-commit fixes --- include/pybind11/pybind11.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 84d668f001..28ff0ca5dd 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -52,7 +52,7 @@ PYBIND11_WARNING_DISABLE_MSVC(4127) PYBIND11_NAMESPACE_BEGIN(detail) -inline std::string replaceNewlinesAndSquash(const char* text) { +inline std::string replaceNewlinesAndSquash(const char *text) { std::string result; // Replace newlines with spaces and squash consecutive spaces @@ -70,11 +70,13 @@ inline std::string replaceNewlinesAndSquash(const char* text) { // Strip leading and trailing spaces result.erase(result.begin(), std::find_if(result.begin(), result.end(), [](unsigned char ch) { - return !std::isspace(ch); - })); - result.erase(std::find_if(result.rbegin(), result.rend(), [](unsigned char ch) { - return !std::isspace(ch); - }).base(), result.end()); + return !std::isspace(ch); + })); + result.erase(std::find_if(result.rbegin(), + result.rend(), + [](unsigned char ch) { return !std::isspace(ch); }) + .base(), + result.end()); return result; } From f795b7e83a524124d05defa307e1ee8a66a8b80d Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Sat, 8 Jul 2023 21:47:49 +0200 Subject: [PATCH 04/24] Don't use std::find_if for C++ 11 compatibility --- include/pybind11/pybind11.h | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 28ff0ca5dd..25d659abb0 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -68,15 +68,23 @@ inline std::string replaceNewlinesAndSquash(const char *text) { ++text; } - // Strip leading and trailing spaces - result.erase(result.begin(), std::find_if(result.begin(), result.end(), [](unsigned char ch) { - return !std::isspace(ch); - })); - result.erase(std::find_if(result.rbegin(), - result.rend(), - [](unsigned char ch) { return !std::isspace(ch); }) - .base(), - result.end()); + // Strip leading spaces + size_t firstNonSpace = result.find_first_not_of(" \t\n\r\f\v"); + + if (firstNonSpace != std::string::npos) { + result.erase(0, firstNonSpace); + } else { + result.clear(); + } + + // Strip trailing spaces + size_t lastNonSpace = result.find_last_not_of(" \t\n\r\f\v"); + + if (lastNonSpace != std::string::npos) { + result.erase(lastNonSpace + 1); + } else { + result.clear(); + } return result; } From 964061356cff92669e112b613e0b89a8137456d3 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Sun, 9 Jul 2023 00:01:22 +0200 Subject: [PATCH 05/24] Avoid implicit char to bool conversion --- include/pybind11/pybind11.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 25d659abb0..d1c4ac7e5d 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -56,7 +56,7 @@ inline std::string replaceNewlinesAndSquash(const char *text) { std::string result; // Replace newlines with spaces and squash consecutive spaces - while (*text) { + while (*text != '\0') { if (*text == '\n') { result += ' '; while (*(text + 1) == ' ') { From d8e7d2d7b3aee1760b236cf01675e4458bc70c53 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 24 Jul 2023 19:15:53 +0200 Subject: [PATCH 06/24] Test default arguments for line breaks --- tests/test_kwargs_and_defaults.cpp | 24 ++++++++++++++++++++++++ tests/test_kwargs_and_defaults.py | 11 ++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 77e72c0c70..1af44be2d9 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -8,6 +8,7 @@ */ #include +#include #include "constructor_stats.h" #include "pybind11_tests.h" @@ -42,6 +43,29 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { m.def("kw_func_udl", kw_func, "x"_a, "y"_a = 300); m.def("kw_func_udl_z", kw_func, "x"_a, "y"_a = 0); + // test line breaks in default argument representation + struct CustomRepr { + std::string __repr__() { return "line\nbreak"; } + }; + + py::class_(m, "CustomRepr") + .def("__repr__", &CustomRepr::__repr__); + + CustomRepr defaultCustomRepr{}; + m.def( + "kw_lb_func0", [](const CustomRepr&) {}, + py::arg("custom") = defaultCustomRepr); + + Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); + m.def( + "kw_lb_func1", [](const Eigen::Matrix3d&) {}, + py::arg("mat") = defaultMatrix); + + Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); + m.def( + "kw_lb_func2", [](const Eigen::VectorXd&) {}, + py::arg("vec") = defaultVector); + // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { PYBIND11_WARNING_PUSH diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index 7174726fce..e6aa6cb56c 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -23,7 +23,16 @@ def test_function_signatures(doc): doc(m.KWClass.foo1) == "foo1(self: m.kwargs_and_defaults.KWClass, x: int, y: float) -> None" ) - + assert ( + doc(m.kw_lb_func0) + == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = line break) -> None" + ) + assert ( + '\n' not in str(doc(m.kw_lb_func1)) + ) + assert ( + '\n' not in str(doc(m.kw_lb_func2)) + ) def test_named_arguments(): assert m.kw_func0(5, 10) == "x=5, y=10" From 418bdeffa06495efc9d9c6343506192e05bc78f2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Jul 2023 17:16:23 +0000 Subject: [PATCH 07/24] style: pre-commit fixes --- tests/test_kwargs_and_defaults.cpp | 14 +++++--------- tests/test_kwargs_and_defaults.py | 9 +++------ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 1af44be2d9..93e2989ca7 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -7,8 +7,8 @@ BSD-style license that can be found in the LICENSE file. */ -#include #include +#include #include "constructor_stats.h" #include "pybind11_tests.h" @@ -48,23 +48,19 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { std::string __repr__() { return "line\nbreak"; } }; - py::class_(m, "CustomRepr") - .def("__repr__", &CustomRepr::__repr__); + py::class_(m, "CustomRepr").def("__repr__", &CustomRepr::__repr__); CustomRepr defaultCustomRepr{}; m.def( - "kw_lb_func0", [](const CustomRepr&) {}, - py::arg("custom") = defaultCustomRepr); + "kw_lb_func0", [](const CustomRepr &) {}, py::arg("custom") = defaultCustomRepr); Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); m.def( - "kw_lb_func1", [](const Eigen::Matrix3d&) {}, - py::arg("mat") = defaultMatrix); + "kw_lb_func1", [](const Eigen::Matrix3d &) {}, py::arg("mat") = defaultMatrix); Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); m.def( - "kw_lb_func2", [](const Eigen::VectorXd&) {}, - py::arg("vec") = defaultVector); + "kw_lb_func2", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultVector); // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index e6aa6cb56c..ad8a868d20 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -27,12 +27,9 @@ def test_function_signatures(doc): doc(m.kw_lb_func0) == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = line break) -> None" ) - assert ( - '\n' not in str(doc(m.kw_lb_func1)) - ) - assert ( - '\n' not in str(doc(m.kw_lb_func2)) - ) + assert "\n" not in str(doc(m.kw_lb_func1)) + assert "\n" not in str(doc(m.kw_lb_func2)) + def test_named_arguments(): assert m.kw_func0(5, 10) == "x=5, y=10" From 452119574e05ca7b8111dcf83f6a460f9fddfe47 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 24 Jul 2023 20:04:04 +0200 Subject: [PATCH 08/24] Separate Eigen tests --- tests/test_eigen_matrix.cpp | 10 ++++++++++ tests/test_eigen_matrix.py | 9 +++++++++ tests/test_kwargs_and_defaults.cpp | 4 +++- tests/test_kwargs_and_defaults.py | 3 --- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/tests/test_eigen_matrix.cpp b/tests/test_eigen_matrix.cpp index 554cc4d7f8..459d263a7c 100644 --- a/tests/test_eigen_matrix.cpp +++ b/tests/test_eigen_matrix.cpp @@ -330,6 +330,16 @@ TEST_SUBMODULE(eigen_matrix, m) { m.def("dense_c", [mat]() -> DenseMatrixC { return DenseMatrixC(mat); }); m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; }); m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; }); + // test_defaults + Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); + m.def( + "defaults_mat", [](const Eigen::Matrix3d&) {}, + py::arg("mat") = defaultMatrix); + + Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); + m.def( + "defaults_vec", [](const Eigen::VectorXd&) {}, + py::arg("vec") = defaultVector); // test_sparse, test_sparse_signature m.def("sparse_r", [mat]() -> SparseMatrixR { // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn) diff --git a/tests/test_eigen_matrix.py b/tests/test_eigen_matrix.py index b2e76740b1..988691c866 100644 --- a/tests/test_eigen_matrix.py +++ b/tests/test_eigen_matrix.py @@ -716,6 +716,15 @@ def test_dense_signature(doc): ) +def test_defaults(doc): + assert ( + '\n' not in str(doc(m.defaults_mat)) + ) + assert ( + '\n' not in str(doc(m.defaults_vec)) + ) + + def test_named_arguments(): a = np.array([[1.0, 2], [3, 4], [5, 6]]) b = np.ones((2, 1)) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 93e2989ca7..76f86ddd12 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -7,7 +7,6 @@ BSD-style license that can be found in the LICENSE file. */ -#include #include #include "constructor_stats.h" @@ -54,6 +53,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { m.def( "kw_lb_func0", [](const CustomRepr &) {}, py::arg("custom") = defaultCustomRepr); +<<<<<<< HEAD Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); m.def( "kw_lb_func1", [](const Eigen::Matrix3d &) {}, py::arg("mat") = defaultMatrix); @@ -62,6 +62,8 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { m.def( "kw_lb_func2", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultVector); +======= +>>>>>>> fea352cb (Separate Eigen tests) // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { PYBIND11_WARNING_PUSH diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index ad8a868d20..854e892276 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -27,9 +27,6 @@ def test_function_signatures(doc): doc(m.kw_lb_func0) == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = line break) -> None" ) - assert "\n" not in str(doc(m.kw_lb_func1)) - assert "\n" not in str(doc(m.kw_lb_func2)) - def test_named_arguments(): assert m.kw_func0(5, 10) == "x=5, y=10" From d47aa39f386a0f3ca49f2b0600b59eb2e8fa2807 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Jul 2023 18:12:07 +0000 Subject: [PATCH 09/24] style: pre-commit fixes --- tests/test_eigen_matrix.cpp | 6 ++---- tests/test_eigen_matrix.py | 8 ++------ tests/test_kwargs_and_defaults.py | 1 + 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/test_eigen_matrix.cpp b/tests/test_eigen_matrix.cpp index 459d263a7c..54a827246a 100644 --- a/tests/test_eigen_matrix.cpp +++ b/tests/test_eigen_matrix.cpp @@ -333,13 +333,11 @@ TEST_SUBMODULE(eigen_matrix, m) { // test_defaults Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); m.def( - "defaults_mat", [](const Eigen::Matrix3d&) {}, - py::arg("mat") = defaultMatrix); + "defaults_mat", [](const Eigen::Matrix3d &) {}, py::arg("mat") = defaultMatrix); Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); m.def( - "defaults_vec", [](const Eigen::VectorXd&) {}, - py::arg("vec") = defaultVector); + "defaults_vec", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultVector); // test_sparse, test_sparse_signature m.def("sparse_r", [mat]() -> SparseMatrixR { // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn) diff --git a/tests/test_eigen_matrix.py b/tests/test_eigen_matrix.py index 988691c866..a486c2f93b 100644 --- a/tests/test_eigen_matrix.py +++ b/tests/test_eigen_matrix.py @@ -717,12 +717,8 @@ def test_dense_signature(doc): def test_defaults(doc): - assert ( - '\n' not in str(doc(m.defaults_mat)) - ) - assert ( - '\n' not in str(doc(m.defaults_vec)) - ) + assert "\n" not in str(doc(m.defaults_mat)) + assert "\n" not in str(doc(m.defaults_vec)) def test_named_arguments(): diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index 854e892276..859a6b2408 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -28,6 +28,7 @@ def test_function_signatures(doc): == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = line break) -> None" ) + def test_named_arguments(): assert m.kw_func0(5, 10) == "x=5, y=10" From 7a9382ac20cdac0a7c5ce023dd2f3a668aba6b07 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 24 Jul 2023 20:24:35 +0200 Subject: [PATCH 10/24] Fix merge --- tests/test_kwargs_and_defaults.cpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 76f86ddd12..a3bf5ae027 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -53,17 +53,6 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { m.def( "kw_lb_func0", [](const CustomRepr &) {}, py::arg("custom") = defaultCustomRepr); -<<<<<<< HEAD - Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); - m.def( - "kw_lb_func1", [](const Eigen::Matrix3d &) {}, py::arg("mat") = defaultMatrix); - - Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); - m.def( - "kw_lb_func2", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultVector); - -======= ->>>>>>> fea352cb (Separate Eigen tests) // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { PYBIND11_WARNING_PUSH From 5d7e555b4769a11e1b4609e0abb26f75f3c37eac Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Sat, 29 Jul 2023 11:54:45 +0200 Subject: [PATCH 11/24] Try importing numpy --- tests/test_eigen_matrix.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tests/test_eigen_matrix.cpp b/tests/test_eigen_matrix.cpp index 54a827246a..97360b0285 100644 --- a/tests/test_eigen_matrix.cpp +++ b/tests/test_eigen_matrix.cpp @@ -331,13 +331,17 @@ TEST_SUBMODULE(eigen_matrix, m) { m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; }); m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; }); // test_defaults - Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); - m.def( - "defaults_mat", [](const Eigen::Matrix3d &) {}, py::arg("mat") = defaultMatrix); - - Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); - m.def( - "defaults_vec", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultVector); + try { + py::module_::import("numpy"); + Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); + m.def( + "defaults_mat", [](const Eigen::Matrix3d &) {}, py::arg("mat") = defaultMatrix); + + Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); + m.def( + "defaults_vec", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultMatrix); + } catch (const py::error_already_set &ex) { + } // test_sparse, test_sparse_signature m.def("sparse_r", [mat]() -> SparseMatrixR { // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn) From a2d675fdd60bc379de8bb53cc250aa424a84a21b Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Sat, 29 Jul 2023 12:20:11 +0200 Subject: [PATCH 12/24] Avoid unreferenced variable in catch block --- tests/test_eigen_matrix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_eigen_matrix.cpp b/tests/test_eigen_matrix.cpp index 97360b0285..a37617044a 100644 --- a/tests/test_eigen_matrix.cpp +++ b/tests/test_eigen_matrix.cpp @@ -340,7 +340,7 @@ TEST_SUBMODULE(eigen_matrix, m) { Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); m.def( "defaults_vec", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultMatrix); - } catch (const py::error_already_set &ex) { + } catch (const py::error_already_set&) { } // test_sparse, test_sparse_signature m.def("sparse_r", [mat]() -> SparseMatrixR { From 035a0446974f97f4d2dadab61f4ba325cf636eea Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Jul 2023 10:20:33 +0000 Subject: [PATCH 13/24] style: pre-commit fixes --- tests/test_eigen_matrix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_eigen_matrix.cpp b/tests/test_eigen_matrix.cpp index a37617044a..a531ab7ee3 100644 --- a/tests/test_eigen_matrix.cpp +++ b/tests/test_eigen_matrix.cpp @@ -340,7 +340,7 @@ TEST_SUBMODULE(eigen_matrix, m) { Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); m.def( "defaults_vec", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultMatrix); - } catch (const py::error_already_set&) { + } catch (const py::error_already_set &) { } // test_sparse, test_sparse_signature m.def("sparse_r", [mat]() -> SparseMatrixR { From 19a8d51c16015b4dd43f473e4a76011284dae88e Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 7 Aug 2023 18:56:58 +0200 Subject: [PATCH 14/24] Update squash function --- include/pybind11/pybind11.h | 42 ++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index d1c4ac7e5d..7b299020ff 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -52,41 +52,35 @@ PYBIND11_WARNING_DISABLE_MSVC(4127) PYBIND11_NAMESPACE_BEGIN(detail) -inline std::string replaceNewlinesAndSquash(const char *text) { +constexpr const char *whitespaces = " \t\n\r\f\v"; + +inline std::string replace_newlines_and_squash(const char *text) { std::string result; + bool previous_is_whitespace = false; - // Replace newlines with spaces and squash consecutive spaces + // Replace characters in whitespaces array with spaces and squash consecutive spaces while (*text != '\0') { - if (*text == '\n') { - result += ' '; - while (*(text + 1) == ' ') { - ++text; + if (strchr(whitespaces, *text)) { + if (!previous_is_whitespace) { + result += ' '; + previous_is_whitespace = true; } } else { result += *text; + previous_is_whitespace = false; } ++text; } - // Strip leading spaces - size_t firstNonSpace = result.find_first_not_of(" \t\n\r\f\v"); - - if (firstNonSpace != std::string::npos) { - result.erase(0, firstNonSpace); - } else { - result.clear(); - } + // Strip leading and trailing whitespaces + const size_t str_begin = result.find_first_not_of(whitespaces); + if (str_begin == std::string::npos) + return ""; - // Strip trailing spaces - size_t lastNonSpace = result.find_last_not_of(" \t\n\r\f\v"); - - if (lastNonSpace != std::string::npos) { - result.erase(lastNonSpace + 1); - } else { - result.clear(); - } + const size_t str_end = result.find_last_not_of(whitespaces); + const size_t str_range = str_end - str_begin + 1; - return result; + return result.substr(str_begin, str_range); } // Apply all the extensions translators from a list @@ -461,7 +455,7 @@ class cpp_function : public function { // Write default value if available. if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) { signature += " = "; - signature += detail::replaceNewlinesAndSquash(rec->args[arg_index].descr); + signature += detail::replace_newlines_and_squash(rec->args[arg_index].descr); } // Separator for positional-only arguments (placed after the // argument, rather than before like * From 122e15a2b2fdedd3cfe0461de4f86fb99c4cf6b8 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 7 Aug 2023 18:57:23 +0200 Subject: [PATCH 15/24] Reduce try block --- tests/test_eigen_matrix.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_eigen_matrix.cpp b/tests/test_eigen_matrix.cpp index a531ab7ee3..0c003d05e9 100644 --- a/tests/test_eigen_matrix.cpp +++ b/tests/test_eigen_matrix.cpp @@ -331,8 +331,14 @@ TEST_SUBMODULE(eigen_matrix, m) { m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; }); m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; }); // test_defaults + bool have_numpy = true; try { py::module_::import("numpy"); + } catch (const py::error_already_set &) { + have_numpy = false; + } + if (have_numpy) { + py::module_::import("numpy"); Eigen::Matrix defaultMatrix = Eigen::Matrix3d::Identity(); m.def( "defaults_mat", [](const Eigen::Matrix3d &) {}, py::arg("mat") = defaultMatrix); @@ -340,7 +346,6 @@ TEST_SUBMODULE(eigen_matrix, m) { Eigen::VectorXd defaultVector = Eigen::VectorXd::Ones(32); m.def( "defaults_vec", [](const Eigen::VectorXd &) {}, py::arg("vec") = defaultMatrix); - } catch (const py::error_already_set &) { } // test_sparse, test_sparse_signature m.def("sparse_r", [mat]() -> SparseMatrixR { From e6be9d5a4dccb82311633c34286f73757daebe4a Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 7 Aug 2023 18:57:45 +0200 Subject: [PATCH 16/24] Additional test cases --- tests/test_kwargs_and_defaults.cpp | 25 +++++++++++++++++++++---- tests/test_kwargs_and_defaults.py | 27 +++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index a3bf5ae027..ad2a2092a8 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -44,14 +44,31 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { // test line breaks in default argument representation struct CustomRepr { - std::string __repr__() { return "line\nbreak"; } + std::string repr_string; + + CustomRepr(const std::string &repr) : repr_string(repr) {} + + std::string __repr__() const { return repr_string; } }; - py::class_(m, "CustomRepr").def("__repr__", &CustomRepr::__repr__); + py::class_(m, "CustomRepr").def(py::init()) + .def("__repr__", &CustomRepr::__repr__); - CustomRepr defaultCustomRepr{}; m.def( - "kw_lb_func0", [](const CustomRepr &) {}, py::arg("custom") = defaultCustomRepr); + "kw_lb_func0", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" Hello World! ")); + m.def( + "kw_lb_func1", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" Hello\nWorld! ")); + m.def( + "kw_lb_func2", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("\n Hello World!")); + m.def( + "kw_lb_func3", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("Hello World! \n")); + m.def( + "kw_lb_func4", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("Hello\n\nWorld!")); + m.def( + "kw_lb_func5", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("Hello World!")); + m.def( + "kw_lb_func6", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" \t ")); + // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index 859a6b2408..7981256ba3 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -25,9 +25,32 @@ def test_function_signatures(doc): ) assert ( doc(m.kw_lb_func0) - == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = line break) -> None" + == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + ) + assert ( + doc(m.kw_lb_func1) + == "kw_lb_func1(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + ) + assert ( + doc(m.kw_lb_func2) + == "kw_lb_func2(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + ) + assert ( + doc(m.kw_lb_func3) + == "kw_lb_func3(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + ) + assert ( + doc(m.kw_lb_func4) + == "kw_lb_func4(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + ) + assert ( + doc(m.kw_lb_func5) + == "kw_lb_func5(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + ) + assert ( + doc(m.kw_lb_func6) + == "kw_lb_func6(custom: m.kwargs_and_defaults.CustomRepr = ) -> None" ) - def test_named_arguments(): assert m.kw_func0(5, 10) == "x=5, y=10" From 1b5a3d77bb528d6993ca1d9ccf864991cd285a57 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Aug 2023 17:00:38 +0000 Subject: [PATCH 17/24] style: pre-commit fixes --- tests/test_kwargs_and_defaults.cpp | 24 +++++++++++++++++------- tests/test_kwargs_and_defaults.py | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index ad2a2092a8..181ad01fab 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -51,25 +51,35 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { std::string __repr__() const { return repr_string; } }; - py::class_(m, "CustomRepr").def(py::init()) + py::class_(m, "CustomRepr") + .def(py::init()) .def("__repr__", &CustomRepr::__repr__); m.def( - "kw_lb_func0", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" Hello World! ")); + "kw_lb_func0", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr(" Hello World! ")); m.def( - "kw_lb_func1", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" Hello\nWorld! ")); + "kw_lb_func1", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr(" Hello\nWorld! ")); m.def( - "kw_lb_func2", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("\n Hello World!")); + "kw_lb_func2", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr("\n Hello World!")); m.def( - "kw_lb_func3", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("Hello World! \n")); + "kw_lb_func3", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr("Hello World! \n")); m.def( - "kw_lb_func4", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("Hello\n\nWorld!")); + "kw_lb_func4", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr("Hello\n\nWorld!")); m.def( "kw_lb_func5", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("Hello World!")); m.def( "kw_lb_func6", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" \t ")); - // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { PYBIND11_WARNING_PUSH diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index 7981256ba3..027cf7647d 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -52,6 +52,7 @@ def test_function_signatures(doc): == "kw_lb_func6(custom: m.kwargs_and_defaults.CustomRepr = ) -> None" ) + def test_named_arguments(): assert m.kw_func0(5, 10) == "x=5, y=10" From 9918f2202488335f044d8102664e0d90faa7d4c4 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 7 Aug 2023 19:12:23 +0200 Subject: [PATCH 18/24] Put statement inside braces --- include/pybind11/pybind11.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 7b299020ff..b400d4c25e 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -74,8 +74,9 @@ inline std::string replace_newlines_and_squash(const char *text) { // Strip leading and trailing whitespaces const size_t str_begin = result.find_first_not_of(whitespaces); - if (str_begin == std::string::npos) + if (str_begin == std::string::npos) { return ""; + } const size_t str_end = result.find_last_not_of(whitespaces); const size_t str_range = str_end - str_begin + 1; From 25c965760e8513eb3ea80a700d30c30e1ef927d3 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 7 Aug 2023 20:42:07 +0200 Subject: [PATCH 19/24] Move string into function body --- include/pybind11/pybind11.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index b400d4c25e..a2f4edc302 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -52,9 +52,8 @@ PYBIND11_WARNING_DISABLE_MSVC(4127) PYBIND11_NAMESPACE_BEGIN(detail) -constexpr const char *whitespaces = " \t\n\r\f\v"; - inline std::string replace_newlines_and_squash(const char *text) { + const char *whitespaces = " \t\n\r\f\v"; std::string result; bool previous_is_whitespace = false; From 0ea0a13e1880b81d9ed055556220b70b3b1eb846 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 7 Aug 2023 20:42:46 +0200 Subject: [PATCH 20/24] Rename repr for better readability. Make constr explicit. --- tests/test_kwargs_and_defaults.cpp | 16 +++++++++------- tests/test_kwargs_and_defaults.py | 12 ++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 181ad01fab..06a12fda24 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -46,7 +46,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { struct CustomRepr { std::string repr_string; - CustomRepr(const std::string &repr) : repr_string(repr) {} + explicit CustomRepr(const std::string &repr) : repr_string(repr) {} std::string __repr__() const { return repr_string; } }; @@ -58,25 +58,27 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { m.def( "kw_lb_func0", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr(" Hello World! ")); + py::arg("custom") = CustomRepr(" array([[A, B], [C, D]]) ")); m.def( "kw_lb_func1", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr(" Hello\nWorld! ")); + py::arg("custom") = CustomRepr(" array([[A, B],\n[C, D]]) ")); m.def( "kw_lb_func2", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr("\n Hello World!")); + py::arg("custom") = CustomRepr("\n array([[A, B], [C, D]])")); m.def( "kw_lb_func3", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr("Hello World! \n")); + py::arg("custom") = CustomRepr("array([[A, B], [C, D]]) \n")); m.def( "kw_lb_func4", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr("Hello\n\nWorld!")); + py::arg("custom") = CustomRepr("array([[A, B],\n\n[C, D]])")); m.def( - "kw_lb_func5", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr("Hello World!")); + "kw_lb_func5", + [](const CustomRepr &) {}, + py::arg("custom") = CustomRepr("array([[A, B], [C, D]])")); m.def( "kw_lb_func6", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" \t ")); diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index 027cf7647d..ffcee6b002 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -25,27 +25,27 @@ def test_function_signatures(doc): ) assert ( doc(m.kw_lb_func0) - == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + == "kw_lb_func0(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" ) assert ( doc(m.kw_lb_func1) - == "kw_lb_func1(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + == "kw_lb_func1(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" ) assert ( doc(m.kw_lb_func2) - == "kw_lb_func2(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + == "kw_lb_func2(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" ) assert ( doc(m.kw_lb_func3) - == "kw_lb_func3(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + == "kw_lb_func3(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" ) assert ( doc(m.kw_lb_func4) - == "kw_lb_func4(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + == "kw_lb_func4(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" ) assert ( doc(m.kw_lb_func5) - == "kw_lb_func5(custom: m.kwargs_and_defaults.CustomRepr = Hello World!) -> None" + == "kw_lb_func5(custom: m.kwargs_and_defaults.CustomRepr = array([[A, B], [C, D]])) -> None" ) assert ( doc(m.kw_lb_func6) From a2add0d7ad17da2f3f85946273edd4c34a4ce554 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Mon, 7 Aug 2023 21:04:30 +0200 Subject: [PATCH 21/24] Add multiline string default argument test case --- tests/test_kwargs_and_defaults.cpp | 1 + tests/test_kwargs_and_defaults.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 06a12fda24..9d77620698 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -81,6 +81,7 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { py::arg("custom") = CustomRepr("array([[A, B], [C, D]])")); m.def( "kw_lb_func6", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" \t ")); + m.def("kw_lb_func7", [](const std::string &) {}, py::arg("str_arg") = "First line.\nSecond line."); // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index ffcee6b002..b367a05e93 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -51,6 +51,10 @@ def test_function_signatures(doc): doc(m.kw_lb_func6) == "kw_lb_func6(custom: m.kwargs_and_defaults.CustomRepr = ) -> None" ) + assert ( + doc(m.kw_lb_func7) + == "kw_lb_func7(str_arg: str = 'First line.\\nSecond line.') -> None" + ) def test_named_arguments(): From 1a9dd7b42126e18ad11d511f5d3237921acf4271 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Aug 2023 19:05:10 +0000 Subject: [PATCH 22/24] style: pre-commit fixes --- tests/test_kwargs_and_defaults.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 9d77620698..908de6b0e7 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -81,7 +81,10 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { py::arg("custom") = CustomRepr("array([[A, B], [C, D]])")); m.def( "kw_lb_func6", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" \t ")); - m.def("kw_lb_func7", [](const std::string &) {}, py::arg("str_arg") = "First line.\nSecond line."); + m.def( + "kw_lb_func7", + [](const std::string &) {}, + py::arg("str_arg") = "First line.\nSecond line."); // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { From d53892ccb0e7089c189e65e734eec306ad25bdd1 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Tue, 8 Aug 2023 15:34:13 +0200 Subject: [PATCH 23/24] Add std namespace, do not modify string repr --- include/pybind11/pybind11.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index a2f4edc302..32b8276ef2 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -54,12 +54,20 @@ PYBIND11_NAMESPACE_BEGIN(detail) inline std::string replace_newlines_and_squash(const char *text) { const char *whitespaces = " \t\n\r\f\v"; - std::string result; + std::string result(text); bool previous_is_whitespace = false; + // Do not modify string representations + char first_char = result[0]; + char last_char = result[result.size() - 1]; + if (first_char == last_char && first_char == '\'') { + return result; + } + result.clear(); + // Replace characters in whitespaces array with spaces and squash consecutive spaces while (*text != '\0') { - if (strchr(whitespaces, *text)) { + if (std::strchr(whitespaces, *text)) { if (!previous_is_whitespace) { result += ' '; previous_is_whitespace = true; From 9cefda84b44e75f38c0a0401e6490814aebe467c Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Tue, 8 Aug 2023 15:34:55 +0200 Subject: [PATCH 24/24] Test for all space chars, test str repr not modified --- tests/test_kwargs_and_defaults.cpp | 12 ++++++------ tests/test_kwargs_and_defaults.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_kwargs_and_defaults.cpp b/tests/test_kwargs_and_defaults.cpp index 908de6b0e7..614c826dac 100644 --- a/tests/test_kwargs_and_defaults.cpp +++ b/tests/test_kwargs_and_defaults.cpp @@ -66,25 +66,25 @@ TEST_SUBMODULE(kwargs_and_defaults, m) { m.def( "kw_lb_func2", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr("\n array([[A, B], [C, D]])")); + py::arg("custom") = CustomRepr("\v\n array([[A, B], [C, D]])")); m.def( "kw_lb_func3", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr("array([[A, B], [C, D]]) \n")); + py::arg("custom") = CustomRepr("array([[A, B], [C, D]]) \f\n")); m.def( "kw_lb_func4", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr("array([[A, B],\n\n[C, D]])")); + py::arg("custom") = CustomRepr("array([[A, B],\n\f\n[C, D]])")); m.def( "kw_lb_func5", [](const CustomRepr &) {}, - py::arg("custom") = CustomRepr("array([[A, B], [C, D]])")); + py::arg("custom") = CustomRepr("array([[A, B],\r [C, D]])")); m.def( - "kw_lb_func6", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" \t ")); + "kw_lb_func6", [](const CustomRepr &) {}, py::arg("custom") = CustomRepr(" \v\t ")); m.def( "kw_lb_func7", [](const std::string &) {}, - py::arg("str_arg") = "First line.\nSecond line."); + py::arg("str_arg") = "First line.\n Second line."); // test_args_and_kwargs m.def("args_function", [](py::args args) -> py::tuple { diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index b367a05e93..b577001976 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -53,7 +53,7 @@ def test_function_signatures(doc): ) assert ( doc(m.kw_lb_func7) - == "kw_lb_func7(str_arg: str = 'First line.\\nSecond line.') -> None" + == "kw_lb_func7(str_arg: str = 'First line.\\n Second line.') -> None" )