Skip to content

Commit

Permalink
adding tests specifically to exercise pybind11::str::raw_str
Browse files Browse the repository at this point in the history
Preparation for simplification of pybind11::str::raw_str:
These tests assert current behavior, which is meant to not change in the subsequent simplification of pybind11::str::raw_str.

These tests will also alert us to any behavior changes across Python and PyPy versions.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Aug 5, 2020
1 parent 3e448c0 commit 3a90695
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ TEST_SUBMODULE(pytypes, m) {
);
});

m.def("convert_to_pybind11_str", [](py::object o) { return py::str(o); });

m.def("get_implicit_casting", []() {
py::dict d;
d["char*_i1"] = "abc";
Expand Down
21 changes: 21 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,27 @@ def test_constructors():
assert noconv2[k] is expected[k]


def test_pybind11_str_raw_str():
# specifically to exercise pybind11::str::raw_str
cvt = m.convert_to_pybind11_str
assert cvt(u"Str") == u"Str"
assert cvt(b"Bytes") == u"Bytes" if str is bytes else "b'Bytes'"
assert cvt(None) == u"None"
assert cvt(False) == u"False"
assert cvt(True) == u"True"
assert cvt(42) == u"42"
assert cvt(2**65) == u"36893488147419103232"
assert cvt(-1.50) == u"-1.5"
assert cvt(()) == u"()"
assert cvt((18,)) == u"(18,)"
assert cvt([]) == u"[]"
assert cvt([28]) == u"[28]"
assert cvt({}) == u"{}"
assert cvt({3: 4}) == u"{3: 4}"
assert cvt(set()) == u"set([])" if str is bytes else "set()"
assert cvt({3, 3}) == u"set([3])" if str is bytes else "{3}"


def test_implicit_casting():
"""Tests implicit casting when assigning or appending to dicts and lists."""
z = m.get_implicit_casting()
Expand Down

0 comments on commit 3a90695

Please sign in to comment.