Skip to content

Commit e09c222

Browse files
committed
Merge branch 'master' into b-pass→vectorcall
2 parents d828e7d + b93c0f7 commit e09c222

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

include/pybind11/pytypes.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,13 @@ class str : public object {
16901690
Return a string representation of the object. This is analogous to
16911691
the ``str()`` function in Python.
16921692
\endrst */
1693-
explicit str(handle h) : object(raw_str(h.ptr()), stolen_t{}) {
1693+
// Templatized to avoid ambiguity with str(const object&) for object-derived types.
1694+
template <typename T,
1695+
detail::enable_if_t<!std::is_base_of<object, detail::remove_cvref_t<T>>::value
1696+
&& std::is_constructible<handle, T>::value,
1697+
int>
1698+
= 0>
1699+
explicit str(T &&h) : object(raw_str(handle(std::forward<T>(h)).ptr()), stolen_t{}) {
16941700
if (!m_ptr) {
16951701
throw error_already_set();
16961702
}

tests/test_pytypes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,4 +1211,6 @@ TEST_SUBMODULE(pytypes, m) {
12111211
m.def("check_type_is", [](const py::object &x) -> py::typing::TypeIs<RealNumber> {
12121212
return py::isinstance<RealNumber>(x);
12131213
});
1214+
1215+
m.def("const_kwargs_ref_to_str", [](const py::kwargs &kwargs) { return py::str(kwargs); });
12141216
}

tests/test_pytypes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,3 +1367,8 @@ def test_arg_return_type_hints(doc, backport_typehints):
13671367
backport_typehints(doc(m.check_type_guard))
13681368
== "check_type_guard(arg0: list[object]) -> typing.TypeGuard[list[float]]"
13691369
)
1370+
1371+
1372+
def test_const_kwargs_ref_to_str():
1373+
assert m.const_kwargs_ref_to_str() == "{}"
1374+
assert m.const_kwargs_ref_to_str(a=1) == "{'a': 1}"

0 commit comments

Comments
 (0)