Skip to content

Commit aa2bc03

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

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
@@ -291,6 +291,9 @@ TEST_SUBMODULE(stl, m) {
291291
m.def("func_with_string_or_vector_string_arg_overload", [](std::list<std::string>) { return 2; });
292292
m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; });
293293

294+
// #1807: 2.3.0 regression: <class 'bytes'> is not converted to std::vector<uint8_t> anymore
295+
m.def("func_with_vector_uint8_t_arg", [](std::vector<uint8_t> v) { return v.size(); });
296+
294297
class Placeholder {
295298
public:
296299
Placeholder() { print_created(this); }

tests/test_stl.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ def test_function_with_string_and_vector_string_arg():
245245
assert m.func_with_string_or_vector_string_arg_overload("A") == 3
246246

247247

248+
def test_bytes_to_vector_uint8_t():
249+
"""Check if a bytes is implicitly converted to std::vector<uint8_t>, issue #1807"""
250+
assert m.func_with_vector_uint8_t_arg(b"abc") == 3
251+
with pytest.raises(TypeError):
252+
m.func_with_vector_uint8_t_arg("stringval")
253+
254+
248255
def test_stl_ownership():
249256
cstats = ConstructorStats.get(m.Placeholder)
250257
assert cstats.alive() == 0

0 commit comments

Comments
 (0)