Skip to content

Commit d4818c2

Browse files
committed
Adding missing bytes to test_constructors().
1 parent 8e40e38 commit d4818c2

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

include/pybind11/pytypes.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,9 @@ PYBIND11_NAMESPACE_END(detail)
802802
PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
803803
/* This is deliberately not 'explicit' to allow implicit conversion from object: */ \
804804
Name(const object &o) : Parent(o) { } \
805-
Name(object &&o) : Parent(std::move(o)) { }
805+
Name(object &&o) : Parent(std::move(o)) { } \
806+
template <typename Policy_> \
807+
Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }
806808

807809
#define PYBIND11_OBJECT_DEFAULT(Name, Parent, CheckFun) \
808810
PYBIND11_OBJECT(Name, Parent, CheckFun) \

tests/test_pytypes.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ TEST_SUBMODULE(pytypes, m) {
197197
// test_constructors
198198
m.def("default_constructors", []() {
199199
return py::dict(
200+
"bytes"_a=py::bytes(),
200201
"str"_a=py::str(),
201202
"bool"_a=py::bool_(),
202203
"int"_a=py::int_(),
@@ -210,6 +211,7 @@ TEST_SUBMODULE(pytypes, m) {
210211

211212
m.def("converting_constructors", [](py::dict d) {
212213
return py::dict(
214+
"bytes"_a=py::bytes(d["bytes"]),
213215
"str"_a=py::str(d["str"]),
214216
"bool"_a=py::bool_(d["bool"]),
215217
"int"_a=py::int_(d["int"]),
@@ -225,6 +227,7 @@ TEST_SUBMODULE(pytypes, m) {
225227
m.def("cast_functions", [](py::dict d) {
226228
// When converting between Python types, obj.cast<T>() should be the same as T(obj)
227229
return py::dict(
230+
"bytes"_a=d["bytes"].cast<py::bytes>(),
228231
"str"_a=d["str"].cast<py::str>(),
229232
"bool"_a=d["bool"].cast<py::bool_>(),
230233
"int"_a=d["int"].cast<py::int_>(),

tests/test_pytypes.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,27 @@ def func(self, x, *args):
188188

189189
def test_constructors():
190190
"""C++ default and converting constructors are equivalent to type calls in Python"""
191-
types = [str, bool, int, float, tuple, list, dict, set]
191+
types = [bytes, 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+
expected["bytes"] = bytes() # Note that bytes.__name__ == 'str' in Python 2.
195+
expected["str"] = u"" # pybind11::str uses the Python 3 naming. Using u"" because flake8 complains about unicode().
193196
assert m.default_constructors() == expected
194197

195198
data = {
196-
str: 42,
197-
bool: "Not empty",
198-
int: "42",
199-
float: "+1e3",
200-
tuple: range(3),
201-
list: range(3),
202-
dict: [("two", 2), ("one", 1), ("three", 3)],
203-
set: [4, 4, 5, 6, 6, 6],
204-
memoryview: b'abc'
199+
"bytes": b'41', # Currently no supported or working conversions.
200+
"str": 42,
201+
"bool": "Not empty",
202+
"int": "42",
203+
"float": "+1e3",
204+
"tuple": range(3),
205+
"list": range(3),
206+
"dict": [("two", 2), ("one", 1), ("three", 3)],
207+
"set": [4, 4, 5, 6, 6, 6],
208+
"memoryview": b'abc'
205209
}
206-
inputs = {k.__name__: v for k, v in data.items()}
207-
expected = {k.__name__: k(v) for k, v in data.items()}
210+
inputs = {k: v for k, v in data.items()}
211+
expected = {k: eval(k)(v) for k, v in data.items()}
208212

209213
assert m.converting_constructors(inputs) == expected
210214
assert m.cast_functions(inputs) == expected

0 commit comments

Comments
 (0)