summaryrefslogtreecommitdiffstats
path: root/Objects/unionobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-07-16 09:49:33 (GMT)
committerGitHub <noreply@github.com>2021-07-16 09:49:33 (GMT)
commitd9f923280f204204f8703756aef4f655b579b4b8 (patch)
treea7f4bae4b05868d51cbb81a5faa1812f1df47106 /Objects/unionobject.c
parentaeaa553d650786afc6e68df1f4813ae1a5b71d05 (diff)
downloadcpython-d9f923280f204204f8703756aef4f655b579b4b8.zip
cpython-d9f923280f204204f8703756aef4f655b579b4b8.tar.gz
cpython-d9f923280f204204f8703756aef4f655b579b4b8.tar.bz2
bpo-44636: Collapse union of equal types (GH-27178)
The result of `int | int` is now `int`. Fix comparison of the union type with non-hashable objects. `int | str == {}` no longer raises a TypeError.
Diffstat (limited to 'Objects/unionobject.c')
-rw-r--r--Objects/unionobject.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/Objects/unionobject.c b/Objects/unionobject.c
index 85092e6..dad26c3 100644
--- a/Objects/unionobject.c
+++ b/Objects/unionobject.c
@@ -187,9 +187,9 @@ union_richcompare(PyObject *a, PyObject *b, int op)
}
}
} else {
- if (PySet_Add(b_set, b) == -1) {
- goto exit;
- }
+ Py_DECREF(a_set);
+ Py_DECREF(b_set);
+ Py_RETURN_NOTIMPLEMENTED;
}
result = PyObject_RichCompare(a_set, b_set, op);
exit:
@@ -551,17 +551,25 @@ _Py_Union(PyObject *args)
}
}
+ args = dedup_and_flatten_args(args);
+ if (args == NULL) {
+ return NULL;
+ }
+ if (PyTuple_GET_SIZE(args) == 1) {
+ PyObject *result1 = PyTuple_GET_ITEM(args, 0);
+ Py_INCREF(result1);
+ Py_DECREF(args);
+ return result1;
+ }
+
result = PyObject_GC_New(unionobject, &_Py_UnionType);
if (result == NULL) {
+ Py_DECREF(args);
return NULL;
}
result->parameters = NULL;
- result->args = dedup_and_flatten_args(args);
+ result->args = args;
_PyObject_GC_TRACK(result);
- if (result->args == NULL) {
- Py_DECREF(result);
- return NULL;
- }
return (PyObject*)result;
}