-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[QUESTION] Defining and using metaclasses with pybind11 #2696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I managed to get it working using plain CPython APIs:
Is it possible to define metaclasses with pybind11 directly, though? In this case it ended up being pretty easy to define with the CPython API directly so I suppose there isn't really a need for that. |
FWIW I'm briefly trying out something semi-related for #2332; at the moment, I'm just trying to write it in pure Python, but may fall back to CPython API like you tried. Can I ask how you passed the result of EDIT: I think that's about what I did, but for reinterpret_borrow<py::object>(py::reinterpret_cast<PyObject*>(py::detail::get_internals().default_metaclass)) |
Yes, I did just cast it to |
Hi! I'm trying to use For what I need to do, you can look at this SO question. In a few words: I'm trying to define How is it supposed to be done? |
I don't believe there is any built-in support in pybind11 for defining a metaclass --- instead you have to use the Python C API directly to define the metaclass, as I did here: |
I see, thanks. I'm not familiar at all with the C API. Can you help me understand your code and maybe adapt it to |
On the metaclass, you'd have to define |
Summary: - Customize the metaclass of `torch.distributed.distributed_c10d.ReduceOp` for the sake of custom `__instancecheck__` - Add `copy.copy`, `copy.deepcopy`, and `pickle` support with tests Rel: - #81272 - #84243 - #87191 - #87303 - #87555 Ref: - pybind/pybind11#2696 Pull Request resolved: #88275 Approved by: https://github.com/wanchaol
) Summary: - Customize the metaclass of `torch.distributed.distributed_c10d.ReduceOp` for the sake of custom `__instancecheck__` - Add `copy.copy`, `copy.deepcopy`, and `pickle` support with tests Rel: - pytorch#81272 - pytorch#84243 - pytorch#87191 - pytorch#87303 - pytorch#87555 Ref: - pybind/pybind11#2696 Pull Request resolved: pytorch#88275 Approved by: https://github.com/wanchaol
I am aware of the
pybind11::metaclass
option that can be passed topybind11::class_
, but I can't find any documentation on how it is supposed to be used. There is a single test case here:pybind11/tests/test_methods_and_attributes.cpp
Line 282 in 028812a
but it isn't clear what that case is showing, and I haven't been able to find a single other example of code that uses
pybind11::metaclass
.What I'd like to accomplish is to make
__class_getitem__
work on Python < 3.7, equivalent to the following pure python code:Note that in Python >= 3.7, this example also works if we eliminate
Parent
and just usetype
as the metaclass ofChild
.I'd like to accomplish this same thing, where both
Parent
andChild
are defined using pybind11 rather than pure Python.My initial attempt was:
but that crashes while creating
cls_parent
: due to thet_size >= b_size
condition in the extra_ivars function in typeobject.c.The text was updated successfully, but these errors were encountered: