From 631a27b5af7d2977fcc825edd1c14fe46f7bcb87 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 29 Jul 2020 23:56:42 -0700 Subject: [PATCH] additional assert demonstrating that pybind::str can also by bytes indeed (proving isinstance right) --- tests/test_pytypes.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 4cfc707a32..dc45ad02fb 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -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, @@ -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."""