Skip to content

Commit

Permalink
test_constructors() fix for Python 2.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Aug 10, 2020
1 parent 611f7db commit a5d1aaa
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ 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}
if str is bytes: # Python 2.
# pybind11::str is unicode even under Python 2.
expected["str"] = u"" # flake8 complains about unicode().
assert m.default_constructors() == expected

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

assert m.converting_constructors(inputs) == expected
assert m.cast_functions(inputs) == expected
Expand Down

0 comments on commit a5d1aaa

Please sign in to comment.