Skip to content

additional assert demonstrating that pybind::str can also by bytes in… #2343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ def test_constructors():
"""C++ default and converting constructors are equivalent to type calls in Python"""
types = [str, bool, int, float, tuple, list, dict, set]
expected = {t.__name__: t() for t in types}
assert m.default_constructors() == expected
default_constructed = m.default_constructors()
assert default_constructed == expected

data = {
str: 42,
Expand Down Expand Up @@ -219,6 +220,23 @@ def test_constructors():
for k in noconv2:
assert noconv2[k] is expected[k]

if str is bytes: # Python 2
# pybind11::str passes through bytes unchanged.
assert isinstance(default_constructed["str"], unicode) # NOT str
assert isinstance(noconv1["str"], str) # NOT unicode
assert isinstance(noconv2["str"], str) # NOT unicode
else:
assert isinstance(default_constructed["str"], str) # unicode
assert isinstance(noconv1["str"], str) # unicode
assert isinstance(noconv2["str"], str) # unicode

expected["str"] = b"actual bytes"
noconv1b = m.converting_constructors(expected)
noconv2b = m.cast_functions(expected)
# Even with Python 3, pybind11::str passes through bytes unchanged.
assert isinstance(noconv1b["str"], bytes) # NOT str
assert isinstance(noconv2b["str"], bytes) # NOT str


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