summaryrefslogtreecommitdiffstats
path: root/Objects/descrobject.c
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@gmail.com>2020-03-07 19:03:09 (GMT)
committerGitHub <noreply@github.com>2020-03-07 19:03:09 (GMT)
commit4663f66f3554dd8e2ec130e40f6abb3c6a514775 (patch)
tree479bda23ce4895419248e76431066a81d9088c94 /Objects/descrobject.c
parent8f130536926a30237b5297780d61ef4232e88577 (diff)
downloadcpython-4663f66f3554dd8e2ec130e40f6abb3c6a514775.zip
cpython-4663f66f3554dd8e2ec130e40f6abb3c6a514775.tar.gz
cpython-4663f66f3554dd8e2ec130e40f6abb3c6a514775.tar.bz2
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.
Diffstat (limited to 'Objects/descrobject.c')
-rw-r--r--Objects/descrobject.c26
1 files changed, 25 insertions, 1 deletions
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 @@ static PyMappingMethods mappingproxy_as_mapping = {
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 @@ PyTypeObject PyDictProxy_Type = {
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 */