From 3d80ad57d6088ca16d047566cdafa847bf9329c2 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 3 Dec 2020 17:33:51 -0800 Subject: [PATCH 1/4] github current snapshot --- include/pybind11/stl.h | 2 +- tests/test_stl.cpp | 3 +++ tests/test_stl.py | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 721bb669f0..b1a6f6a8da 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -144,7 +144,7 @@ template struct list_caster { using value_conv = make_caster; bool load(handle src, bool convert) { - if (!isinstance(src) || isinstance(src)) + if (!isinstance(src) || (!isinstance(src) && isinstance(src))) return false; auto s = reinterpret_borrow(src); value.clear(); diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 0590162770..3b6d93cc5e 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -291,6 +291,9 @@ TEST_SUBMODULE(stl, m) { m.def("func_with_string_or_vector_string_arg_overload", [](std::list) { return 2; }); m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; }); + // #1807: 2.3.0 regression: is not converted to std::vector anymore + m.def("func_with_vector_uint8_t_arg", [](std::vector v) { return v.size(); }); + class Placeholder { public: Placeholder() { print_created(this); } diff --git a/tests/test_stl.py b/tests/test_stl.py index 330017544d..a6f41a0273 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -245,6 +245,13 @@ def test_function_with_string_and_vector_string_arg(): assert m.func_with_string_or_vector_string_arg_overload("A") == 3 +def test_bytes_to_vector_uint8_t(): + """Check if a bytes is implicitly converted to std::vector, issue #1807""" + assert m.func_with_vector_uint8_t_arg(b'abc') == 3 + with pytest.raises(TypeError): + m.func_with_vector_uint8_t_arg('stringval') + + def test_stl_ownership(): cstats = ConstructorStats.get(m.Placeholder) assert cstats.alive() == 0 From 011663dfd2299ded1ec1a81cfc5dc4234c48beb0 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 3 Dec 2020 18:25:07 -0800 Subject: [PATCH 2/4] enable new behavior only for Python 3, adjust new test to work with Python 2 --- include/pybind11/stl.h | 6 +++++- tests/test_stl.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index b1a6f6a8da..1ee55de25c 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -144,7 +144,11 @@ template struct list_caster { using value_conv = make_caster; bool load(handle src, bool convert) { - if (!isinstance(src) || (!isinstance(src) && isinstance(src))) + if (!isinstance(src) || ( +#if PY_MAJOR_VERSION >= 3 + !isinstance(src) && +#endif + isinstance(src))) return false; auto s = reinterpret_borrow(src); value.clear(); diff --git a/tests/test_stl.py b/tests/test_stl.py index a6f41a0273..f67add06b4 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -247,7 +247,7 @@ def test_function_with_string_and_vector_string_arg(): def test_bytes_to_vector_uint8_t(): """Check if a bytes is implicitly converted to std::vector, issue #1807""" - assert m.func_with_vector_uint8_t_arg(b'abc') == 3 + assert m.func_with_vector_uint8_t_arg([ord(c) for c in b'abc']) == 3 with pytest.raises(TypeError): m.func_with_vector_uint8_t_arg('stringval') From 97b346382af98820c0051907fcc9e45a769dba8f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 3 Dec 2020 18:48:30 -0800 Subject: [PATCH 3/4] restoring original behavior, only leaving the new lambda function in test_stl.cpp --- include/pybind11/stl.h | 4 ++++ tests/test_stl.py | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 1ee55de25c..2ff65b16a4 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -144,11 +144,15 @@ template struct list_caster { using value_conv = make_caster; bool load(handle src, bool convert) { +#if 1 + if (!isinstance(src) || isinstance(src)) +#else if (!isinstance(src) || ( #if PY_MAJOR_VERSION >= 3 !isinstance(src) && #endif isinstance(src))) +#endif return false; auto s = reinterpret_borrow(src); value.clear(); diff --git a/tests/test_stl.py b/tests/test_stl.py index f67add06b4..ff6f910d69 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -245,11 +245,11 @@ def test_function_with_string_and_vector_string_arg(): assert m.func_with_string_or_vector_string_arg_overload("A") == 3 -def test_bytes_to_vector_uint8_t(): - """Check if a bytes is implicitly converted to std::vector, issue #1807""" - assert m.func_with_vector_uint8_t_arg([ord(c) for c in b'abc']) == 3 - with pytest.raises(TypeError): - m.func_with_vector_uint8_t_arg('stringval') +#def test_bytes_to_vector_uint8_t(): +# """Check if a bytes is implicitly converted to std::vector, issue #1807""" +# assert m.func_with_vector_uint8_t_arg([ord(c) for c in b'abc']) == 3 +# with pytest.raises(TypeError): +# m.func_with_vector_uint8_t_arg('stringval') def test_stl_ownership(): From 04859f25d989c82b007a8cfaaeb8348e83f25c37 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 3 Dec 2020 19:48:43 -0800 Subject: [PATCH 4/4] also commenting out the new lambda function in test_stl.cpp --- tests/test_stl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 3b6d93cc5e..14ecd35ee4 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -292,7 +292,7 @@ TEST_SUBMODULE(stl, m) { m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; }); // #1807: 2.3.0 regression: is not converted to std::vector anymore - m.def("func_with_vector_uint8_t_arg", [](std::vector v) { return v.size(); }); + //m.def("func_with_vector_uint8_t_arg", [](std::vector v) { return v.size(); }); class Placeholder { public: