Skip to content

Commit

Permalink
Fixing pybind11::bytes() ambiguous overload issue and adding missing …
Browse files Browse the repository at this point in the history
…bytes type to test_constructors(). (#2340)
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Aug 14, 2020
1 parent 5a3ff72 commit 907a57f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
8 changes: 4 additions & 4 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,9 @@ PYBIND11_NAMESPACE_END(detail)
Name(handle h, stolen_t) : Parent(h, stolen_t{}) { } \
PYBIND11_DEPRECATED("Use py::isinstance<py::python_type>(obj) instead") \
bool check() const { return m_ptr != nullptr && (bool) CheckFun(m_ptr); } \
static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); }
static bool check_(handle h) { return h.ptr() != nullptr && CheckFun(h.ptr()); } \
template <typename Policy_> \
Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }

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

#define PYBIND11_OBJECT(Name, Parent, CheckFun) \
PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \
Expand Down
3 changes: 3 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ TEST_SUBMODULE(pytypes, m) {
// test_constructors
m.def("default_constructors", []() {
return py::dict(
"bytes"_a=py::bytes(),
"str"_a=py::str(),
"bool"_a=py::bool_(),
"int"_a=py::int_(),
Expand All @@ -210,6 +211,7 @@ TEST_SUBMODULE(pytypes, m) {

m.def("converting_constructors", [](py::dict d) {
return py::dict(
"bytes"_a=py::bytes(d["bytes"]),
"str"_a=py::str(d["str"]),
"bool"_a=py::bool_(d["bool"]),
"int"_a=py::int_(d["int"]),
Expand All @@ -225,6 +227,7 @@ TEST_SUBMODULE(pytypes, m) {
m.def("cast_functions", [](py::dict d) {
// When converting between Python types, obj.cast<T>() should be the same as T(obj)
return py::dict(
"bytes"_a=d["bytes"].cast<py::bytes>(),
"str"_a=d["str"].cast<py::str>(),
"bool"_a=d["bool"].cast<py::bool_>(),
"int"_a=d["int"].cast<py::int_>(),
Expand Down
30 changes: 18 additions & 12 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,29 @@ def func(self, x, *args):

def test_constructors():
"""C++ default and converting constructors are equivalent to type calls in Python"""
types = [str, bool, int, float, tuple, list, dict, set]
types = [bytes, str, bool, int, float, tuple, list, dict, set]
expected = {t.__name__: t() for t in types}
if str is bytes: # Python 2.
# Note that bytes.__name__ == 'str' in Python 2.
# pybind11::str is unicode even under Python 2.
expected["bytes"] = bytes()
expected["str"] = u"" # flake8 complains about unicode().
assert m.default_constructors() == expected

data = {
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.__name__: v for k, v in data.items()}
expected = {k.__name__: k(v) for k, v in data.items()}
inputs = {k: v for k, v in data.items()}
expected = {k: eval(k)(v) for k, v in data.items()}

assert m.converting_constructors(inputs) == expected
assert m.cast_functions(inputs) == expected
Expand Down

0 comments on commit 907a57f

Please sign in to comment.