diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 3bedb9d1a3..44f8e1f837 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1387,7 +1387,7 @@ struct handle_type_name { }; template <> struct handle_type_name { - static constexpr auto name = const_name("collections.abc.Buffer"); + static constexpr auto name = const_name(PYBIND11_BUFFER_TYPE_HINT); }; template <> struct handle_type_name { diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index e9d954bc49..e3df32df3e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -239,6 +239,13 @@ # define PYBIND11_SUBINTERPRETER_SUPPORT #endif +// 3.12 Compatibility +#if 0x030C0000 <= PY_VERSION_HEX +# define PYBIND11_BUFFER_TYPE_HINT "collections.abc.Buffer" +#else +# define PYBIND11_BUFFER_TYPE_HINT "typing_extensions.Buffer" +#endif + // #define PYBIND11_STR_LEGACY_PERMISSIVE // If DEFINED, pybind11::str can hold PyUnicodeObject or PyBytesObject // (probably surprising and never documented, but this was the diff --git a/tests/test_buffers.py b/tests/test_buffers.py index d335b71e96..a712f2bda4 100644 --- a/tests/test_buffers.py +++ b/tests/test_buffers.py @@ -3,6 +3,7 @@ import ctypes import io import struct +import sys import pytest @@ -228,10 +229,11 @@ def test_ctypes_from_buffer(): def test_buffer_docstring(): - assert ( - m.get_buffer_info.__doc__.strip() - == "get_buffer_info(arg0: collections.abc.Buffer) -> pybind11_tests.buffers.buffer_info" - ) + if sys.version_info >= (3, 12): + docstring = "get_buffer_info(arg0: collections.abc.Buffer) -> pybind11_tests.buffers.buffer_info" + else: + docstring = "get_buffer_info(arg0: typing_extensions.Buffer) -> pybind11_tests.buffers.buffer_info" + assert m.get_buffer_info.__doc__.strip() == docstring def test_buffer_exception():