Skip to content

Commit b93c0f7

Browse files
authored
Fix ambiguous str(handle) constructor for object-derived types (#5949)
* Fix ambiguous `str(handle)` constructor for object-derived types Templatize `str(handle h)` with SFINAE to exclude types derived from `object`, resolving ambiguity with `str(const object&)` when calling `py::str()` on types like `kwargs`, `dict`, etc. The template now only accepts `handle` or `PyObject*`, while all `object`-derived types use the `str(const object&)` overload. * fix(tests): CIBW test fixes from b-pass→vectorcall branch - Install multiple-interpreter test modules into wheel (CMakeLists.txt) The mod_per_interpreter_gil, mod_shared_interpreter_gil, and mod_per_interpreter_gil_with_singleton modules were being built but not installed into the wheel when using scikit-build-core. - Pin numpy 2.4.0 for Python 3.14 CI tests (requirements.txt) NumPy 2.4.0 is the first version with official Python 3.14 wheels. - Add IOS platform constant to tests/env.py - Skip subinterpreter tests on iOS (test_multiple_interpreters.py) Subinterpreters are not supported in the iOS simulator environment. - Enable pytest timeout of 120s for CIBW tests (pyproject.toml) Provides a safety net to catch hanging tests before CI job timeout. - Disable pytest-timeout for Pyodide (no signal.setitimer) Pyodide runs in WebAssembly without POSIX signals. - Add -v flag for verbose pytest output in CIBW tests
1 parent fee2527 commit b93c0f7

File tree

8 files changed

+29
-2
lines changed

8 files changed

+29
-2
lines changed

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/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,12 @@ if(NOT PYBIND11_CUDA_TESTS)
602602
endif()
603603
endforeach()
604604

605+
if(SKBUILD)
606+
foreach(mod IN LISTS PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES)
607+
install(TARGETS "${mod}" LIBRARY DESTINATION .)
608+
endforeach()
609+
endif()
610+
605611
if(PYBIND11_TEST_SMART_HOLDER)
606612
foreach(mod IN LISTS PYBIND11_MULTIPLE_INTERPRETERS_TEST_MODULES)
607613
target_compile_definitions(

tests/env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sysconfig
66

77
ANDROID = sys.platform.startswith("android")
8+
IOS = sys.platform.startswith("ios")
89
LINUX = sys.platform.startswith("linux")
910
MACOS = sys.platform.startswith("darwin")
1011
WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin")

tests/pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ PYBIND11_FINDPYTHON = true
2727

2828
[tool.cibuildwheel]
2929
test-sources = ["tests", "pyproject.toml"]
30-
test-command = "python -m pytest -o timeout=0 -p no:cacheprovider tests"
30+
test-command = "python -m pytest -v -o timeout=120 -p no:cacheprovider tests"
31+
# Pyodide doesn't have signal.setitimer, so pytest-timeout can't work with timeout > 0
32+
pyodide.test-command = "python -m pytest -v -o timeout=0 -p no:cacheprovider tests"
3133
environment.PIP_ONLY_BINARY = "numpy"
3234
environment.PIP_PREFER_BINARY = "1"
3335

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ numpy~=1.22.2; platform_python_implementation=="CPython" and python_version=="3.
1010
numpy~=1.26.0; platform_python_implementation=="CPython" and python_version>="3.11" and python_version<"3.13" and platform_machine!="ARM64"
1111
numpy~=2.3.0; platform_python_implementation=="CPython" and python_version>="3.11" and platform_machine=="ARM64"
1212
numpy~=2.2.0; platform_python_implementation=="CPython" and python_version=="3.13" and platform_machine!="ARM64"
13+
numpy==2.4.0; platform_python_implementation=="CPython" and python_version>="3.14"
1314
pytest>=6
1415
pytest-timeout
1516
scipy~=1.5.4; platform_python_implementation=="CPython" and python_version<"3.10"

tests/test_multiple_interpreters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010
import pytest
1111

12+
import env
1213
import pybind11_tests
1314

15+
if env.IOS:
16+
pytest.skip("Subinterpreters not supported on iOS", allow_module_level=True)
17+
1418
# 3.14.0b3+, though sys.implementation.supports_isolated_interpreters is being added in b4
1519
# Can be simplified when we drop support for the first three betas
1620
CONCURRENT_INTERPRETERS_SUPPORT = (

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)