Skip to content

Fixes #1807: 2.3.0 regression: <class 'bytes'> -> std::vector<uint8_t> #2198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ template <typename Type, typename Value> struct list_caster {
using value_conv = make_caster<Value>;

bool load(handle src, bool convert) {
if (!isinstance<sequence>(src) || isinstance<str>(src))
if (!isinstance<sequence>(src) || (!isinstance<bytes>(src) && isinstance<str>(src)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this only cures a symptom of a bigger problem, pointed out to me by @bstaletic :
77059f0
With that change, the code here can stay as-is.
@wjakob Did you attempt work based on @jagerman 's code, or do you know if anyone else tried?
@cdump While I think it's best to not change this line here, your test looks valuable to me. I'm in favor of merging it after the more fundamental issue is fixed.

return false;
auto s = reinterpret_borrow<sequence>(src);
value.clear();
Expand Down
3 changes: 3 additions & 0 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ TEST_SUBMODULE(stl, m) {
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; });

// #1807: 2.3.0 regression: <class 'bytes'> is not converted to std::vector<uint8_t> anymore
m.def("func_with_vector_uint8_t_arg", [](std::vector<uint8_t> v) { return v.size(); });

class Placeholder {
public:
Placeholder() { print_created(this); }
Expand Down
7 changes: 7 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>, 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
Expand Down