Skip to content

Commit 0b818ac

Browse files
committed
Enable py::metaclass(PyType_Type)
1 parent 768cebe commit 0b818ac

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

include/pybind11/attr.h

+7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct dynamic_attr {};
8282
struct buffer_protocol {};
8383

8484
/// Annotation which requests that a special metaclass is created for a type
85+
/// NOTE: pybind11's default metaclass is not compatible with abc.ABCMeta
8586
struct metaclass {
8687
handle value;
8788

@@ -90,6 +91,12 @@ struct metaclass {
9091

9192
/// Override pybind11's default metaclass
9293
explicit metaclass(handle value) : value(value) {}
94+
95+
/// Example usage: py::metaclass(PyType_Type)
96+
/// PyType_Type is recommended if compatibility with abc.ABCMeta is required.
97+
/// The only potential downside is that static properties behave differently
98+
// (see pybind/pybind11#679 for background).
99+
explicit metaclass(PyTypeObject &type_obj) : value(reinterpret_cast<PyObject *>(&type_obj)) {}
93100
};
94101

95102
/// Specifies a custom callback with signature `void (PyHeapTypeObject*)` that

tests/test_methods_and_attributes.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ TEST_SUBMODULE(methods_and_attributes, m) {
367367

368368
// test_metaclass_override
369369
struct MetaclassOverride {};
370-
py::class_<MetaclassOverride>(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type))
370+
py::class_<MetaclassOverride>(m, "MetaclassOverride", py::metaclass(PyType_Type))
371371
.def_property_readonly_static("readonly", [](const py::object &) { return 1; });
372372

373373
// test_overload_ordering

0 commit comments

Comments
 (0)