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
When a C++ function returns a std::string that is a serialized protobuf (like in callService), and that is part of a Pybind11 wrapper, Pybind11 assumes it is UTF-8 and will try to decode it as such.
When a C++ function returns a std::string or char* to a Python caller, pybind11 will assume that the string is valid UTF-8 and will decode it to a native Python str, using the same API as Python uses to perform bytes.decode('utf-8'). If this implicit conversion fails, pybind11 will raise a UnicodeDecodeError.
This is not a valid assumption for protobuf, and so sometimes an exception is raised when returning the service response, and so you might see an exception like:
'utf-8' codec can't decode byte in position 14 invalid start byte '0x3'
The fix is to instead have the python wrappers return py::bytes which will be directly returned as a bytes object in Python without any attempt to decode.
The text was updated successfully, but these errors were encountered:
When a C++ function returns a
std::string
that is a serialized protobuf (like incallService
), and that is part of a Pybind11 wrapper, Pybind11 assumes it is UTF-8 and will try to decode it as such.https://pybind11.readthedocs.io/en/stable/advanced/cast/strings.html#returning-c-strings-to-python
This is not a valid assumption for protobuf, and so sometimes an exception is raised when returning the service response, and so you might see an exception like:
The fix is to instead have the python wrappers return
py::bytes
which will be directly returned as abytes
object in Python without any attempt to decode.The text was updated successfully, but these errors were encountered: