Fix for Issue #1258 (#1298)
* Fix for Issue #1258
list_caster::load method will now check for a Python string and prevent its automatic conversion to a list.
This should fix the issue "pybind11/stl.h converts string to vector<string> #1258" (https://github.com/pybind/pybind11/issues/1258)
* Added tests for fix of issue #1258
* Changelog: stl string auto-conversion
diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp
index cd0985d..8736ea8 100644
--- a/tests/test_stl.cpp
+++ b/tests/test_stl.cpp
@@ -11,6 +11,9 @@
#include "constructor_stats.h"
#include <pybind11/stl.h>
+#include <vector>
+#include <string>
+
// Test with `std::variant` in C++17 mode, or with `boost::variant` in C++11/14
#if PYBIND11_HAS_VARIANT
using std::variant;
@@ -33,6 +36,8 @@
}} // namespace pybind11::detail
#endif
+PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
+
/// Issue #528: templated constructor
struct TplCtorClass {
template <typename T> TplCtorClass(const T &) { }
@@ -237,6 +242,11 @@
// test_stl_pass_by_pointer
m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr);
+ // #1258: pybind11/stl.h converts string to vector<string>
+ m.def("func_with_string_or_vector_string_arg_overload", [](std::vector<std::string>) { return 1; });
+ m.def("func_with_string_or_vector_string_arg_overload", [](std::list<std::string>) { return 2; });
+ m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; });
+
class Placeholder {
public:
Placeholder() { print_created(this); }
diff --git a/tests/test_stl.py b/tests/test_stl.py
index 2c5e995..b78f86a 100644
--- a/tests/test_stl.py
+++ b/tests/test_stl.py
@@ -201,6 +201,14 @@
assert expected_message in str(excinfo.value)
+def test_function_with_string_and_vector_string_arg():
+ """Check if a string is NOT implicitly converted to a list, which was the
+ behavior before fix of issue #1258"""
+ assert m.func_with_string_or_vector_string_arg_overload(('A', 'B', )) == 2
+ assert m.func_with_string_or_vector_string_arg_overload(['A', 'B']) == 2
+ assert m.func_with_string_or_vector_string_arg_overload('A') == 3
+
+
def test_stl_ownership():
cstats = ConstructorStats.get(m.Placeholder)
assert cstats.alive() == 0