Skip to content

Commit bbbfe74

Browse files
committed
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.
1 parent e59a82c commit bbbfe74

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

tests/test_pytypes.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,24 @@ def test_constructors():
198198
assert m.default_constructors() == expected
199199

200200
data = {
201-
"bytes": b'41', # Currently no supported or working conversions.
202-
"str": 42,
203-
"bool": "Not empty",
204-
"int": "42",
205-
"float": "+1e3",
206-
"tuple": range(3),
207-
"list": range(3),
208-
"dict": [("two", 2), ("one", 1), ("three", 3)],
209-
"set": [4, 4, 5, 6, 6, 6],
210-
"memoryview": b'abc'
201+
bytes: b'41', # Currently no supported or working conversions.
202+
str: 42,
203+
bool: "Not empty",
204+
int: "42",
205+
float: "+1e3",
206+
tuple: range(3),
207+
list: range(3),
208+
dict: [("two", 2), ("one", 1), ("three", 3)],
209+
set: [4, 4, 5, 6, 6, 6],
210+
memoryview: b'abc'
211211
}
212-
inputs = {k: v for k, v in data.items()}
213-
expected = {k: eval(k)(v) for k, v in data.items()}
212+
inputs = {k.__name__: v for k, v in data.items()}
213+
expected = {k.__name__: k(v) for k, v in data.items()}
214+
if str is bytes: # Similar to the above. See comments above.
215+
inputs["bytes"] = b'41'
216+
inputs["str"] = 42
217+
expected["bytes"] = b'41'
218+
expected["str"] = u"42"
214219

215220
assert m.converting_constructors(inputs) == expected
216221
assert m.cast_functions(inputs) == expected

0 commit comments

Comments
 (0)