Uh oh!
There was an error while loading. Please reload this page.
Make py::isinstance<py::str> less permissive - #1463
Conversation
py::str's uses a permissive check function that allows both str (Py 3)/ unicode (Py 2), *and* bytes (Py 3)/str (Py 2) so that a `py::str` can be created from either, but this makes the default `isinstance` implementation overly permissive.
bstaletic
commented
Jul 19, 2018
If I got this right, this won't break code that does the following: std::string GetStringFromPythonObject( pybind11::object o ) {
// If it is any kind of string like python object just cast it to std::stringif ( pybind11::isinstance< bytes >( o ) || pybind11::isinstance< str >( o ) ) {
return o.cast< std::string >();
}
// Else, go through pybind11::str()returnpybind11::str( o ).cast< std::string >();
}Is that correct? |
jagerman
commented
Jul 19, 2018
It looks like this PR does break Python 2, where This might be trickier to fix without breaking things. |
Your code is fine; the problem is that we have code expecting that |
wjakob
commented
Aug 28, 2018
How about only enabling the more restrictive check for Python >= 3.x? |
fix some uninitialized members in `value_and_holder` for some of the constructurs. Found with coverity in a downstream project.
) In some cases the user of pythonbuf needs to allocate the internal buffer to a specific size e.g. for performance or to enable synchronous writes to the buffer. By changing `pythonbuf::d_buffer` to be dynamically allocated we can now enable these use-cases while still providing the default behavior of allocating a 1024 byte internal buffer (through a default parameter).
…1587) (pybind#1595) * Fix async Python functors invoking from multiple C++ threads (pybind#1587) Ensure GIL is held during functor destruction. * Add async Python callbacks test that runs in separate Python thread
In def_readonly and def_readwrite, there is an assertion that the member comes from the class or a base class: static_assert(std::is_base_of<C, type>::value, "..."); However, if C and type are the same type, is_base_of will still only be true if they are the same _non-union_ type. This means we can't define accessors for the members of a union type because of this assertion. Update the assertion to test std::is_same<C, type>::value || std::is_base_of<C, type>::value which will allow union types, or members of base classes. Also add a basic unit test for accessing unions.
wjakob
commented
Jun 11, 2019
This had some weird conflicts, I ended up applying the patch directly myself. |
wjakob
commented
Jun 11, 2019
Whoops, I missed the fact that this does indeed break the test suite on 2.7. Reverting & reopening. |
py::str's uses a permissive check function that allows both str (Py 3)/
unicode (Py 2), and bytes (Py 3)/str (Py 2) so that a
py::strcanbe created from either, but this makes the default
isinstanceimplementation overly permissive.
Fixes#1461