bpo-36144: Update MappingProxyType with PEP 584's operators (#18814)
We make `|=` raise TypeError, since it would be surprising if `C.__dict__ |= {'x': 0}` silently did nothing, while `C.__dict__.update({'x': 0})` is an error.
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index c96945b..4ebbb74 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -982,6 +982,30 @@
0, /* mp_ass_subscript */
};
+static PyObject *
+mappingproxy_or(PyObject *left, PyObject *right)
+{
+ if (PyObject_TypeCheck(left, &PyDictProxy_Type)) {
+ left = ((mappingproxyobject*)left)->mapping;
+ }
+ if (PyObject_TypeCheck(right, &PyDictProxy_Type)) {
+ right = ((mappingproxyobject*)right)->mapping;
+ }
+ return PyNumber_Or(left, right);
+}
+
+static PyObject *
+mappingproxy_ior(PyObject *self, PyObject *Py_UNUSED(other))
+{
+ return PyErr_Format(PyExc_TypeError,
+ "'|=' is not supported by %s; use '|' instead", Py_TYPE(self)->tp_name);
+}
+
+static PyNumberMethods mappingproxy_as_number = {
+ .nb_or = mappingproxy_or,
+ .nb_inplace_or = mappingproxy_ior,
+};
+
static int
mappingproxy_contains(mappingproxyobject *pp, PyObject *key)
{
@@ -1717,7 +1741,7 @@
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)mappingproxy_repr, /* tp_repr */
- 0, /* tp_as_number */
+ &mappingproxy_as_number, /* tp_as_number */
&mappingproxy_as_sequence, /* tp_as_sequence */
&mappingproxy_as_mapping, /* tp_as_mapping */
0, /* tp_hash */