Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ struct string_caster {
return false;
}
value = StringType(buffer, static_cast<size_t>(size));
if (IsView) {
loader_life_support::add_patient(src);
}
return true;
}

Expand Down Expand Up @@ -603,6 +606,9 @@ struct string_caster {
pybind11_fail("Unexpected PYBIND11_BYTES_AS_STRING() failure.");
}
value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
if (IsView) {
loader_life_support::add_patient(src);
}
return true;
}
if (PyByteArray_Check(src.ptr())) {
Expand All @@ -613,6 +619,9 @@ struct string_caster {
pybind11_fail("Unexpected PyByteArray_AsString() failure.");
}
value = StringType(bytearray, (size_t) PyByteArray_Size(src.ptr()));
if (IsView) {
loader_life_support::add_patient(src);
}
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,16 @@ TEST_SUBMODULE(stl, m) {
[](const std::list<std::string> &) { return 2; });
m.def("func_with_string_or_vector_string_arg_overload", [](const std::string &) { return 3; });

#ifdef PYBIND11_HAS_STRING_VIEW
m.def("func_with_string_views", [](const std::vector<std::string_view> &svs) {
py::list l;
for (std::string_view sv : svs) {
l.append(sv);
}
return l;
});
#endif

class Placeholder {
public:
Placeholder() { print_created(this); }
Expand Down
12 changes: 12 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def test_vector(doc):
# Test regression caused by 936: pointers to stl containers weren't castable
assert m.cast_ptr_vector() == ["lvalue", "lvalue"]

if hasattr(m, "func_with_string_views"):

def gen():
return ("a" + str(x) for x in range(10000, 10010))

expected = list(gen())
assert m.func_with_string_views(gen()) == expected
assert m.func_with_string_views(x.encode() for x in gen()) == expected
assert (
m.func_with_string_views(bytearray(x.encode()) for x in gen()) == expected
)


def test_deque():
"""std::deque <-> list"""
Expand Down
Loading