def test_str(doc): assert m.str_from_string().encode().decode() == "baz" assert m.str_from_bytes().encode().decode() == "boo" assert doc(m.str_from_bytes) == "str_from_bytes() -> str" class A(object): def __str__(self): return "this is a str" def __repr__(self): return "this is a repr" assert m.str_from_object(A()) == "this is a str" assert m.repr_from_object(A()) == "this is a repr" assert m.str_from_handle(A()) == "this is a str" s1, s2 = m.str_format() assert s1 == "1 + 2 = 3" assert s1 == s2 malformed_utf8 = b"\x80" assert m.str_from_object( malformed_utf8) is malformed_utf8 # To be fixed; see #2380 if env.PY2: # with pytest.raises(UnicodeDecodeError): # m.str_from_object(malformed_utf8) with pytest.raises(UnicodeDecodeError): m.str_from_handle(malformed_utf8) else: # assert m.str_from_object(malformed_utf8) == "b'\\x80'" assert m.str_from_handle(malformed_utf8) == "b'\\x80'"
def test_str(doc): assert m.str_from_string().encode().decode() == "baz" assert m.str_from_bytes().encode().decode() == "boo" assert doc(m.str_from_bytes) == "str_from_bytes() -> str" class A(object): def __str__(self): return "this is a str" def __repr__(self): return "this is a repr" assert m.str_from_object(A()) == "this is a str" assert m.repr_from_object(A()) == "this is a repr" assert m.str_from_handle(A()) == "this is a str" s1, s2 = m.str_format() assert s1 == "1 + 2 = 3" assert s1 == s2 malformed_utf8 = b"\x80" if hasattr(m, "PYBIND11_STR_LEGACY_PERMISSIVE"): assert m.str_from_object(malformed_utf8) is malformed_utf8 elif env.PY2: with pytest.raises(UnicodeDecodeError): m.str_from_object(malformed_utf8) else: assert m.str_from_object(malformed_utf8) == "b'\\x80'" if env.PY2: with pytest.raises(UnicodeDecodeError): m.str_from_handle(malformed_utf8) else: assert m.str_from_handle(malformed_utf8) == "b'\\x80'"
def test_str(doc): assert m.str_from_char_ssize_t().encode().decode() == "red" assert m.str_from_char_size_t().encode().decode() == "blue" assert m.str_from_string().encode().decode() == "baz" assert m.str_from_bytes().encode().decode() == "boo" assert doc(m.str_from_bytes) == "str_from_bytes() -> str" class A: def __str__(self): return "this is a str" def __repr__(self): return "this is a repr" assert m.str_from_object(A()) == "this is a str" assert m.repr_from_object(A()) == "this is a repr" assert m.str_from_handle(A()) == "this is a str" s1, s2 = m.str_format() assert s1 == "1 + 2 = 3" assert s1 == s2 malformed_utf8 = b"\x80" if hasattr(m, "PYBIND11_STR_LEGACY_PERMISSIVE"): assert m.str_from_object(malformed_utf8) is malformed_utf8 else: assert m.str_from_object(malformed_utf8) == "b'\\x80'" assert m.str_from_handle(malformed_utf8) == "b'\\x80'" assert m.str_from_string_from_str("this is a str") == "this is a str" ucs_surrogates_str = "\udcc3" with pytest.raises(UnicodeEncodeError): m.str_from_string_from_str(ucs_surrogates_str)
def test_str(doc): assert m.str_from_string().encode().decode() == "baz" assert m.str_from_bytes().encode().decode() == "boo" assert doc(m.str_from_bytes) == "str_from_bytes() -> str" class A(object): def __str__(self): return "this is a str" def __repr__(self): return "this is a repr" assert m.str_from_object(A()) == "this is a str" assert m.repr_from_object(A()) == "this is a repr" s1, s2 = m.str_format() assert s1 == "1 + 2 = 3" assert s1 == s2