From aba08a4d994ce6346f6f247f7a532da7e7667156 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 13 Aug 2020 15:52:10 -0700 Subject: [PATCH] Bytes constructor test change compatible with PR #2380 (#2390) * Rolling back PR #2340 change to tests/test_pytypes.py (only this one file). The two other files changed with PR #2340 are not affected by this partial rollback. This partial rollback enables cherry-picking a commit from PR #2380. * 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. * Adding test_constructors test for bytes, on top of cherry-picked commit from PR #2380. --- tests/test_pytypes.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 5b102c4db0..86b839ecc9 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -200,19 +200,24 @@ def test_constructors(): assert m.default_constructors() == expected data = { - "bytes": b'41', # Currently no supported or working conversions. - "str": 42, - "bool": "Not empty", - "int": "42", - "float": "+1e3", - "tuple": range(3), - "list": range(3), - "dict": [("two", 2), ("one", 1), ("three", 3)], - "set": [4, 4, 5, 6, 6, 6], - "memoryview": b'abc' + bytes: b'41', # Currently no supported or working conversions. + str: 42, + bool: "Not empty", + int: "42", + float: "+1e3", + tuple: range(3), + list: range(3), + dict: [("two", 2), ("one", 1), ("three", 3)], + set: [4, 4, 5, 6, 6, 6], + memoryview: b'abc' } - inputs = {k: v for k, v in data.items()} - expected = {k: eval(k)(v) for k, v in data.items()} + 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["bytes"] = b'41' + inputs["str"] = 42 + expected["bytes"] = b'41' + expected["str"] = u"42" assert m.converting_constructors(inputs) == expected assert m.cast_functions(inputs) == expected