Register Scalar/MessageMapContainerTypes as virtual subclasses of MutableMapping instead of inheriting directly.
This prevents these from using abc.ABCMeta metaclass to avoid deprecation warning:
```
DeprecationWarning: Type google._upb._message.MessageMapContainer uses PyType_Spec with a metaclass that has custom tp_new. This is deprecated and will no longer be allowed in Python 3.14.
```
Fixes #15077
Fixes #12186
PiperOrigin-RevId: 613029479
diff --git a/python/protobuf.c b/python/protobuf.c
index 4f53154..316b1f6 100644
--- a/python/protobuf.c
+++ b/python/protobuf.c
@@ -323,6 +323,31 @@
return (PyTypeObject*)type;
}
+PyTypeObject* PyUpb_AddClassWithRegister(PyObject* m, PyType_Spec* spec,
+ PyObject* virtual_base,
+ const char** methods) {
+ PyObject* type = PyType_FromSpec(spec);
+ PyObject* ret1 = PyObject_CallMethod(virtual_base, "register", "O", type);
+ if (!ret1) {
+ Py_XDECREF(type);
+ return NULL;
+ }
+ for (size_t i = 0; methods[i] != NULL; i++) {
+ PyObject* method = PyObject_GetAttrString(virtual_base, methods[i]);
+ if (!method) {
+ Py_XDECREF(type);
+ return NULL;
+ }
+ int ret2 = PyObject_SetAttrString(type, methods[i], method);
+ if (ret2 < 0) {
+ Py_XDECREF(type);
+ return NULL;
+ }
+ }
+
+ return (PyTypeObject*)type;
+}
+
const char* PyUpb_GetStrData(PyObject* obj) {
if (PyUnicode_Check(obj)) {
return PyUnicode_AsUTF8AndSize(obj, NULL);