You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In pybind 2.2.3, the template specialization py::isinstance<py::str>() returns True when used on py::bytes objects. This is incorrect in both Py2 and 3 (since py::str is unicode in Py2).
py::instance(obj, py::str().get_type()) works correctly.
Reproducible example code
m.def("test_isinstance",
[]() {
py::bytes b = "123";
py::print(py::isinstance<py::str>(b));
}
);
>>>pybind11.test_isinstance()
True
The text was updated successfully, but these errors were encountered:
The reason for the failure is that py::str's check function permissively allows both str and bytes objects (or under Python 2: both unicode and str objects). This is mainly here for Python 2 compatibility to allow str objects to be automatically converted to unicode objects.
We can easily add a isinstance<str> specialization does only checks against str (Py 3) or unicode (Py 2) to keep the up-converting behaviour and fix isinstance. That's going to work as expected under Python 3, but the behaviour under Python 2 is going to be a little strange:
Issue description
In pybind 2.2.3, the template specialization
py::isinstance<py::str>()
returns True when used onpy::bytes
objects. This is incorrect in both Py2 and 3 (sincepy::str
isunicode
in Py2).py::instance(obj, py::str().get_type())
works correctly.Reproducible example code
The text was updated successfully, but these errors were encountered: