Skip to content

Commit

Permalink
Adding str_from_handle() to tests/test_pytypes.
Browse files Browse the repository at this point in the history
Expanding test coverage only, no behavior change.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Sep 8, 2020
1 parent 37f845a commit 44b3941
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ TEST_SUBMODULE(pytypes, m) {
m.def("str_from_bytes", []() { return py::str(py::bytes("boo", 3)); });
m.def("str_from_object", [](const py::object& obj) { return py::str(obj); });
m.def("repr_from_object", [](const py::object& obj) { return py::repr(obj); });
m.def("str_from_handle", [](py::handle h) { return py::str(h); });

m.def("str_format", []() {
auto s1 = "{} + {} = {}"_s.format(1, 2, 3);
Expand Down
10 changes: 10 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ def __repr__(self):
assert s1 == "1 + 2 = 3"
assert s1 == s2

assert m.str_from_handle(A()) == "this is a str"

malformed_utf8 = b"\x80"
assert m.str_from_object(malformed_utf8) is malformed_utf8 # Probably surprising.
if env.PY2:
with pytest.raises(TypeError):
m.str_from_handle(malformed_utf8)
else:
assert m.str_from_handle(malformed_utf8) == "b'\\x80'"


def test_bytes(doc):
assert m.bytes_from_string().decode() == "foo"
Expand Down

0 comments on commit 44b3941

Please sign in to comment.