Skip to content

Commit dcc41ef

Browse files
committed
Fixing pybind11::bytes() ambiguous overload issue and adding missing bytes type to test_constructors(). (#2340)
1 parent 227170d commit dcc41ef

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

include/pybind11/pytypes.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,9 @@ PYBIND11_NAMESPACE_END(detail)
782782
Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \
783783
PYBIND11_DEPRECATED("Use py::isinstance<py::python_type>(obj) instead") \
784784
bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \
785-
static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); }
785+
static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \
786+
template <typename Policy_> \
787+
Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }
786788

787789
#define PYBIND11_OBJECT_CVT(Name, Parent, CheckFun, ConvertFun) \
788790
PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
@@ -792,9 +794,7 @@ PYBIND11_NAMESPACE_END(detail)
792794
{ if (!m_ptr) throw error_already_set(); } \
793795
Name(object &&o) \
794796
: Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) \
795-
{ if (!m_ptr) throw error_already_set(); } \
796-
template <typename Policy_> \
797-
Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }
797+
{ if (!m_ptr) throw error_already_set(); }
798798

799799
#define PYBIND11_OBJECT(Name, Parent, CheckFun) \
800800
PYBIND11_OBJECT_COMMON(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: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,29 @@ 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+
# Note that bytes.__name__ == 'str' in Python 2.
195+
# pybind11::str is unicode even under Python 2.
196+
expected["bytes"] = bytes()
197+
expected["str"] = u"" # flake8 complains about unicode().
193198
assert m.default_constructors() == expected
194199

195200
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'
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'
205211
}
206-
inputs = {k.__name__: v for k, v in data.items()}
207-
expected = {k.__name__: k(v) for k, v in data.items()}
212+
inputs = {k: v for k, v in data.items()}
213+
expected = {k: eval(k)(v) for k, v in data.items()}
208214

209215
assert m.converting_constructors(inputs) == expected
210216
assert m.cast_functions(inputs) == expected

0 commit comments

Comments
 (0)