def test_bytes_to_string(): """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is one-way: the only way to return bytes to Python is via the pybind11::bytes class.""" # Issue #816 import sys byte = bytes if sys.version_info[0] < 3 else str assert m.strlen(byte("hi")) == 2 assert m.string_length(byte("world")) == 5 assert m.string_length(byte("a\x00b")) == 3 assert m.strlen(byte("a\x00b")) == 1 # C-string limitation # passing in a utf8 encoded string should work assert m.string_length(u'💩'.encode("utf8")) == 4
def test_bytes_to_string(): """Tests the ability to pass bytes to C++ string-accepting functions. Note that this is one-way: the only way to return bytes to Python is via the pybind11::bytes class.""" # Issue #816 def to_bytes(s): b = s if env.PY2 else s.encode("utf8") assert isinstance(b, bytes) return b assert m.strlen(to_bytes("hi")) == 2 assert m.string_length(to_bytes("world")) == 5 assert m.string_length(to_bytes("a\x00b")) == 3 assert m.strlen(to_bytes("a\x00b")) == 1 # C-string limitation # passing in a utf8 encoded string should work assert m.string_length(u"💩".encode("utf8")) == 4