Skip to content

Commit

Permalink
Bytes constructor test change compatible with PR #2380 (#2390)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Ralf W. Grosse-Kunstleve authored Aug 13, 2020
1 parent 0e4e64a commit 3c0f566
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,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
Expand Down

0 comments on commit 3c0f566

Please sign in to comment.