From 0b818ac1c93eac7eab0e3071a25f7ffb63aecf84 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 31 Jan 2024 23:10:34 -0800 Subject: [PATCH 1/6] Enable `py::metaclass(PyType_Type)` --- include/pybind11/attr.h | 7 +++++++ tests/test_methods_and_attributes.cpp | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 1044db94d9..28535c58fd 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -82,6 +82,7 @@ struct dynamic_attr {}; struct buffer_protocol {}; /// Annotation which requests that a special metaclass is created for a type +/// NOTE: pybind11's default metaclass is not compatible with abc.ABCMeta struct metaclass { handle value; @@ -90,6 +91,12 @@ struct metaclass { /// Override pybind11's default metaclass explicit metaclass(handle value) : value(value) {} + + /// Example usage: py::metaclass(PyType_Type) + /// PyType_Type is recommended if compatibility with abc.ABCMeta is required. + /// The only potential downside is that static properties behave differently + // (see pybind/pybind11#679 for background). + explicit metaclass(PyTypeObject &type_obj) : value(reinterpret_cast(&type_obj)) {} }; /// Specifies a custom callback with signature `void (PyHeapTypeObject*)` that diff --git a/tests/test_methods_and_attributes.cpp b/tests/test_methods_and_attributes.cpp index 31d46eb7ed..53fc747121 100644 --- a/tests/test_methods_and_attributes.cpp +++ b/tests/test_methods_and_attributes.cpp @@ -367,7 +367,7 @@ TEST_SUBMODULE(methods_and_attributes, m) { // test_metaclass_override struct MetaclassOverride {}; - py::class_(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type)) + py::class_(m, "MetaclassOverride", py::metaclass(PyType_Type)) .def_property_readonly_static("readonly", [](const py::object &) { return 1; }); // test_overload_ordering From b2b6663be0b45dea1fe6f52a7790e42183443d50 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 31 Jan 2024 23:16:04 -0800 Subject: [PATCH 2/6] Backport https://github.com/google/pywrapcc/pull/30094/files#diff-9973fac8af12c04b84a77049b3bd0be619441caac64ec75658d3939add4f0e00 --- tests/test_methods_and_attributes.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index 7fdf4e3af9..ad8714ea36 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -1,3 +1,4 @@ +import abc import sys import pytest @@ -227,6 +228,22 @@ def test_metaclass_override(): assert isinstance(m.MetaclassOverride.__dict__["readonly"], int) +def test_abc_meta_incompatibility(): # Mostly to clearly expose the behavior. + with pytest.raises(TypeError) as exc_info: + + class ExampleMandAABC(m.ExampleMandA, metaclass=abc.ABCMeta): + pass + + assert "metaclass conflict" in str(exc_info.value) + + +def test_abc_meta_compatibility(): + class MetaclassOverrideABC(m.MetaclassOverride, metaclass=abc.ABCMeta): + pass + + assert type(MetaclassOverrideABC).__name__ == "ABCMeta" + + def test_no_mixed_overloads(): from pybind11_tests import detailed_error_messages_enabled From cf230fb98698e4633b56dbfb47db0861e167eb43 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 1 Feb 2024 09:39:22 -0800 Subject: [PATCH 3/6] Add a note in docs/classes.rst --- docs/classes.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/classes.rst b/docs/classes.rst index 4f2167dac1..49e84d210a 100644 --- a/docs/classes.rst +++ b/docs/classes.rst @@ -58,6 +58,18 @@ interactive Python session demonstrating this example is shown below: Static member functions can be bound in the same way using :func:`class_::def_static`. +.. note:: + + By default pybind11 uses a custom metaclass which is known to be + incompatible with + `abc.ABCMeta `_ + and can also lead to other surprising issues. In such cases, + using ``py::metaclass(PyType_Type)`` is often a good solution + (e.g. ``py::class_(m, "Pet", py::metaclass(PyType_Type))``). + Please see + `#5015 `_ + for more background. + .. note:: Binding C++ types in unnamed namespaces (also known as anonymous namespaces) From 13de380ec1c076ed87d5f85d5b06eddc22a1da4f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 2 Feb 2024 06:27:51 -0800 Subject: [PATCH 4/6] Replace "issues" with "side effects" --- docs/classes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/classes.rst b/docs/classes.rst index 49e84d210a..e53384f5e0 100644 --- a/docs/classes.rst +++ b/docs/classes.rst @@ -63,7 +63,7 @@ interactive Python session demonstrating this example is shown below: By default pybind11 uses a custom metaclass which is known to be incompatible with `abc.ABCMeta `_ - and can also lead to other surprising issues. In such cases, + and can also lead to other surprising side effects. In such cases, using ``py::metaclass(PyType_Type)`` is often a good solution (e.g. ``py::class_(m, "Pet", py::metaclass(PyType_Type))``). Please see From 0d29c62620b22db281b0fcb91b318d657cd4793b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 2 Feb 2024 06:37:40 -0800 Subject: [PATCH 5/6] Add missing slash accidentally left out --- include/pybind11/attr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 28535c58fd..49e6965118 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -95,7 +95,7 @@ struct metaclass { /// Example usage: py::metaclass(PyType_Type) /// PyType_Type is recommended if compatibility with abc.ABCMeta is required. /// The only potential downside is that static properties behave differently - // (see pybind/pybind11#679 for background). + /// (see pybind/pybind11#679 for background). explicit metaclass(PyTypeObject &type_obj) : value(reinterpret_cast(&type_obj)) {} }; From db53f4af7dfff40eb10351a4dbd8b8b64c6b236c Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 5 Feb 2024 08:26:51 -0800 Subject: [PATCH 6/6] Also test `abc.ABC` compatibilities. See also: https://docs.astral.sh/ruff/rules/meta-class-abc-meta/ --- tests/test_methods_and_attributes.py | 30 ++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index ad8714ea36..b0b5aa870d 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -229,12 +229,24 @@ def test_metaclass_override(): def test_abc_meta_incompatibility(): # Mostly to clearly expose the behavior. - with pytest.raises(TypeError) as exc_info: + with pytest.raises(TypeError, match="metaclass conflict"): class ExampleMandAABC(m.ExampleMandA, metaclass=abc.ABCMeta): pass - assert "metaclass conflict" in str(exc_info.value) + +def test_abc_abc_1st_incompatibility(): # Mostly to clearly expose the behavior. + with pytest.raises(TypeError, match="metaclass conflict"): + + class ABCExampleMandA(abc.ABC, m.ExampleMandA): + pass + + +def test_abc_abc_2nd_incompatibility(): # Mostly to clearly expose the behavior. + with pytest.raises(TypeError, match="metaclass conflict"): + + class ExampleMandAABC(m.ExampleMandA, abc.ABC): + pass def test_abc_meta_compatibility(): @@ -244,6 +256,20 @@ class MetaclassOverrideABC(m.MetaclassOverride, metaclass=abc.ABCMeta): assert type(MetaclassOverrideABC).__name__ == "ABCMeta" +def test_abc_abc_1st_compatibility(): + class ABCMetaclassOverride(abc.ABC, m.MetaclassOverride): + pass + + assert type(ABCMetaclassOverride).__name__ == "ABCMeta" + + +def test_abc_abc_2nd_compatibility(): + class MetaclassOverrideABC(m.MetaclassOverride, abc.ABC): + pass + + assert type(MetaclassOverrideABC).__name__ == "ABCMeta" + + def test_no_mixed_overloads(): from pybind11_tests import detailed_error_messages_enabled