Skip to content

Commit 8ad9596

Browse files
committed
Fixes #1807: 2.3.0 regression: <class 'bytes'> is not converted to std::vector<uint8_t> anymore
1 parent 0234871 commit 8ad9596

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

include/pybind11/stl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ template <typename Type, typename Value> struct list_caster {
144144
using value_conv = make_caster<Value>;
145145

146146
bool load(handle src, bool convert) {
147-
if (!isinstance<sequence>(src) || isinstance<str>(src))
147+
if (!isinstance<sequence>(src) || (!isinstance<bytes>(src) && isinstance<str>(src)))
148148
return false;
149149
auto s = reinterpret_borrow<sequence>(src);
150150
value.clear();

tests/test_stl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ TEST_SUBMODULE(stl, m) {
251251
m.def("func_with_string_or_vector_string_arg_overload", [](std::list<std::string>) { return 2; });
252252
m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; });
253253

254+
// #1807: 2.3.0 regression: <class 'bytes'> is not converted to std::vector<uint8_t> anymore
255+
m.def("func_with_vector_uint8_t_arg", [](std::vector<uint8_t> v) { return v.size(); });
256+
254257
class Placeholder {
255258
public:
256259
Placeholder() { print_created(this); }

tests/test_stl.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ def test_function_with_string_and_vector_string_arg():
220220
assert m.func_with_string_or_vector_string_arg_overload('A') == 3
221221

222222

223+
def test_bytes_to_vector_uint8_t():
224+
"""Check if a bytes is implicitly converted to std::vector<uint8_t>, issue #1807"""
225+
assert m.func_with_vector_uint8_t_arg(b'abc') == 3
226+
with pytest.raises(TypeError):
227+
m.func_with_vector_uint8_t_arg('stringval')
228+
229+
223230
def test_stl_ownership():
224231
cstats = ConstructorStats.get(m.Placeholder)
225232
assert cstats.alive() == 0

0 commit comments

Comments
 (0)