From f3395783a1604eb1d6f50bfc7fca8c62263db170 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Wed, 14 May 2025 09:42:09 +0100 Subject: [PATCH 1/7] Fix Buffer type hint collections.abc.Buffer was added in Python 3.12. The previous behaviour should be used prior to this version. --- include/pybind11/cast.h | 2 +- include/pybind11/detail/common.h | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) 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 ab6221aff4..5124843bde 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -232,6 +232,12 @@ # define PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF #endif +// 3.13 +#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 From 4b089fcc5f7a1f6a648fa0b91b7a2a6a457a3654 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Wed, 14 May 2025 09:47:03 +0100 Subject: [PATCH 2/7] Fix comment --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 87b029b870..c8bb7c0635 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -239,7 +239,7 @@ # define PYBIND11_SUBINTERPRETER_SUPPORT #endif -// 3.13 +// 3.12 Compatibility #if 0x030C0000 <= PY_VERSION_HEX #define PYBIND11_BUFFER_TYPE_HINT "collections.abc.Buffer" #else From 9c89fd017bbd2970c1f92b54e2f497615d25aa68 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Wed, 14 May 2025 09:49:02 +0100 Subject: [PATCH 3/7] Fix indentation --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index c8bb7c0635..8ee81e1706 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -243,7 +243,7 @@ #if 0x030C0000 <= PY_VERSION_HEX #define PYBIND11_BUFFER_TYPE_HINT "collections.abc.Buffer" #else - #define PYBIND11_BUFFER_TYPE_HINT "typing_extensions.Buffer" + #define PYBIND11_BUFFER_TYPE_HINT "typing_extensions.Buffer" #endif // #define PYBIND11_STR_LEGACY_PERMISSIVE From f84a329c37b2b3824dd62bede4f7d41036f212cc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 May 2025 08:49:34 +0000 Subject: [PATCH 4/7] style: pre-commit fixes --- include/pybind11/detail/common.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 8ee81e1706..e3df32df3e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -241,9 +241,9 @@ // 3.12 Compatibility #if 0x030C0000 <= PY_VERSION_HEX - #define PYBIND11_BUFFER_TYPE_HINT "collections.abc.Buffer" +# define PYBIND11_BUFFER_TYPE_HINT "collections.abc.Buffer" #else - #define PYBIND11_BUFFER_TYPE_HINT "typing_extensions.Buffer" +# define PYBIND11_BUFFER_TYPE_HINT "typing_extensions.Buffer" #endif // #define PYBIND11_STR_LEGACY_PERMISSIVE From c2dde7f7e009352446d876cc92d4ca5bd177cc92 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Wed, 14 May 2025 10:06:11 +0100 Subject: [PATCH 5/7] Fix test --- tests/test_buffers.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_buffers.py b/tests/test_buffers.py index d335b71e96..7df6a00805 100644 --- a/tests/test_buffers.py +++ b/tests/test_buffers.py @@ -228,10 +228,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 (3, 12) <= sys.version_info: + 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(): From 44f34376795ca08647852632b16e6ed7de74dcf2 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Wed, 14 May 2025 10:06:50 +0100 Subject: [PATCH 6/7] Add missing import --- tests/test_buffers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_buffers.py b/tests/test_buffers.py index 7df6a00805..07600438f4 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 From 37f5be48bb3728d90ff17e437b09839de83fb0f2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 14 May 2025 09:07:04 +0000 Subject: [PATCH 7/7] style: pre-commit fixes --- tests/test_buffers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_buffers.py b/tests/test_buffers.py index 07600438f4..a712f2bda4 100644 --- a/tests/test_buffers.py +++ b/tests/test_buffers.py @@ -229,11 +229,11 @@ def test_ctypes_from_buffer(): def test_buffer_docstring(): - if (3, 12) <= sys.version_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) + assert m.get_buffer_info.__doc__.strip() == docstring def test_buffer_exception():