Skip to content

Commit a5d1aaa

Browse files
committed
test_constructors() fix for Python 2.
Preparation for changing `pybind11::str` to only hold `PyUnicodeObject` (NOT also `bytes`). Currently test_constructors passes with Python 2 only because `pybind11::str` can also hold a Python 2 `PyStringObject` (or the equivalent `PyBytesObject` in Python 3). Changing the test to exercise conversions for `PyUnicodeObject` makes it consistent between Python 2 and 3, and removes this small obstacle to the planned `pybind11::str` change. Tests for `bytes` conversions will be added separately.
1 parent 611f7db commit a5d1aaa

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

tests/test_pytypes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ def test_constructors():
190190
"""C++ default and converting constructors are equivalent to type calls in Python"""
191191
types = [str, bool, int, float, tuple, list, dict, set]
192192
expected = {t.__name__: t() for t in types}
193+
if str is bytes: # Python 2.
194+
# pybind11::str is unicode even under Python 2.
195+
expected["str"] = u"" # flake8 complains about unicode().
193196
assert m.default_constructors() == expected
194197

195198
data = {
@@ -205,6 +208,9 @@ def test_constructors():
205208
}
206209
inputs = {k.__name__: v for k, v in data.items()}
207210
expected = {k.__name__: k(v) for k, v in data.items()}
211+
if str is bytes: # Similar to the above. See comments above.
212+
inputs["str"] = 42
213+
expected["str"] = u"42"
208214

209215
assert m.converting_constructors(inputs) == expected
210216
assert m.cast_functions(inputs) == expected

0 commit comments

Comments
 (0)