Skip to content

Fix for Issue #1258 #1298

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

Merged
merged 3 commits into from
Oct 11, 2018
Merged

Fix for Issue #1258 #1298

merged 3 commits into from
Oct 11, 2018

Conversation

allanleal
Copy link
Contributor

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 #1258" (#1258)

@jagerman
Copy link
Member

Looks fine, but can you add a simple test case for this to the PR (i.e. something that goes to the wrong overload when passed a string). test_methods_and_attributes.cpp/.py seems like a good place.

@allanleal
Copy link
Contributor Author

Could any of you guys help me on making these automated tests pass? This is a just minor change in pybind11 to fix issue #1258.

@ax3l
Copy link
Collaborator

ax3l commented Sep 2, 2018

@allanleal can you rebase this PR? I appreciate if this is fixed as well for one of my projects :)

def test_stl_ownership():
cstats = ConstructorStats.get(m.Placeholder)
assert cstats.alive() == 0
r = m.test_stl_ownership()
assert len(r) == 1
del r
assert cstats.alive() == 0

Copy link
Collaborator

Choose a reason for hiding this comment

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

unnecessary white space change, please remove :)

@ax3l
Copy link
Collaborator

ax3l commented Sep 4, 2018

@allanleal please really rebase this branch, I think the debug errors you see were recently fixed in #1438

@allanleal
Copy link
Contributor Author

@ax3l Could you please take the lead on this? This is a very simple bug fix, yet, for some reason, it does not go though during the CI phase, and I'm not able to fix this right now. Thanks.

@ax3l
Copy link
Collaborator

ax3l commented Sep 4, 2018

@allanleal I can try :) Can you add me as a collaborator with write access to https://github.com/allanleal/pybind11?

I am not a pybind11 maintainer and can due to that not push to your branch.

@allanleal
Copy link
Contributor Author

Thanks, @ax3l - just added you.

@allanleal
Copy link
Contributor Author

allanleal commented Sep 4, 2018 via email

@ax3l
Copy link
Collaborator

ax3l commented Sep 4, 2018

I can locally verify the failing test in debug mode. Also, there is a violation of the one definition rule in type_caster that needs a fix.

@@ -237,6 +237,10 @@ TEST_SUBMODULE(stl, m) {
// 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 0; });
Copy link
Collaborator

Choose a reason for hiding this comment

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

this line triggers a ODR warning:

include/pybind11/stl.h:179:49: warning: type ‘struct type_caster’ violates the C++ One Definition Rule [-Wodr]
 template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>
                                                 ^
tests/test_opaque_types.cpp:19:1: note: a type with the same name but different base type is defined in another translation unit
 PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
 ^
include/pybind11/stl.h:137:49: note: type name ‘pybind11::detail::list_caster<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >’ should match type name ‘pybind11::detail::type_caster_base<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >’
 template <typename Type, typename Value> struct list_caster {
                                                 ^
include/pybind11/cast.h:814:32: note: the incompatible type is defined here
 template <typename type> class type_caster_base : public type_caster_generic {
                                ^
tests/test_opaque_types.cpp:19:1: note: type ‘struct type_caster’ itself violate the C++ One Definition Rule
 PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
 ^
include/pybind11/stl.h:179:49: note: the incompatible type is defined here
 template <typename Type, typename Alloc> struct type_caster<std::vector<Type, Alloc>>

Copy link
Collaborator

Choose a reason for hiding this comment

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

probably a

PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);

missing

Copy link
Collaborator

Choose a reason for hiding this comment

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

fixed :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

hm, now both py arrays and py lists are std::list< std::string >( ) ...

@ax3l
Copy link
Collaborator

ax3l commented Sep 4, 2018

@allanleal shouldn't the same treatment go to the array_caster (std::vector) and not only to list_caster (std::list)?

@allanleal
Copy link
Contributor Author

Hi @ax3l , did it fail again?

@jagerman Could you please help here? This is a very simple fix, but not passing the tests.

@ax3l
Copy link
Collaborator

ax3l commented Oct 2, 2018

@allanleal yep, sorry I don't know what's off here

@jagerman
Copy link
Member

jagerman commented Oct 2, 2018

There is an ODR violation happening (you can see the warning in the gcc-7 build) because the PYBIND11_MAKE_OPAQUE added in test_stl.cpp is in the wrong place — it's inside the #elif defined(PYBIND11_TEST_BOOST) && ..., moving to after the #endif ought to resolve at least that issue. With that fix it builds for me here under gcc-8 without warnings and passes the tests.

@ax3l
Copy link
Collaborator

ax3l commented Oct 2, 2018

Thanks a lot, I did not see that. Will fix it and push again.

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" (#1258)
@ax3l
Copy link
Collaborator

ax3l commented Oct 2, 2018

Yay, that fixed it! :)

@allanleal
Copy link
Contributor Author

Great - thanks @ax3l. @jagerman can we finally see this merged? Thank you so much for revising this.

@jagerman
Copy link
Member

jagerman commented Oct 4, 2018

This looks good to me. @wjakob ?

@jagerman
Copy link
Member

jagerman commented Oct 4, 2018

Oh, one last request -- can you add a changelog entry for it to docs/changelog.rst?

@ax3l
Copy link
Collaborator

ax3l commented Oct 4, 2018

@jagerman shall I start a 2.2.5 section or add it to 2.3.0?

@jagerman
Copy link
Member

jagerman commented Oct 4, 2018

2.3.0; it seems like more than just a minor .x bug fix.

@ax3l
Copy link
Collaborator

ax3l commented Oct 4, 2018

@jagerman alright, added the changelog and CI is green :)

@allanleal
Copy link
Contributor Author

@ax3l, thanks a lot.

@jagerman, just a kind remind if we could see this merged soon. Is there anything else missing?

@wjakob
Copy link
Member

wjakob commented Oct 11, 2018

Looks good, thanks!

@wjakob wjakob merged commit e76dff7 into pybind:master Oct 11, 2018
@allanleal
Copy link
Contributor Author

allanleal commented Oct 11, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants