Add check if str(handle) correctly converted the object, and throw py::error_already_set if not (bis) (#2477)

* Add check if `str(handle)` correctly converted the object, and throw py::error_already_set if not

* Fix tests on Python 3

* Apply @rwgk's fixes to cherry-picked commits from #2392
This commit is contained in:
Yannick Jadoul
2020-09-11 19:53:04 +02:00
committed by GitHub
parent b47efd35fb
commit fe9ee86ba8
3 changed files with 15 additions and 2 deletions

View File

@@ -104,11 +104,23 @@ def test_str(doc):
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_bytes(doc):
assert m.bytes_from_string().decode() == "foo"