aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkj <28750310+Fidget-Spinner@users.noreply.github.com>2020-11-09 12:00:13 +0800
committerGitHub <noreply@github.com>2020-11-08 20:00:13 -0800
commit4eb41d055e8307b8206f680287e492a6db068acd (patch)
tree49f5dd4d1abac0b43945310972a97e0c943d2869 /Objects/unionobject.c
parentbpo-41754: Ignore NotADirectoryError in invocation of xdg-settings (GH-23075) (diff)
downloadcpython-4eb41d055e8307b8206f680287e492a6db068acd.tar.gz
cpython-4eb41d055e8307b8206f680287e492a6db068acd.tar.bz2
cpython-4eb41d055e8307b8206f680287e492a6db068acd.zip
bpo-42233: Add union type expression support for GenericAlias and fix de-duplicating of GenericAlias (GH-23077)
Diffstat (limited to 'Objects/unionobject.c')
-rw-r--r--Objects/unionobject.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/Objects/unionobject.c b/Objects/unionobject.c
index 1b7f8ab51a..2308bfc9f2 100644
--- a/Objects/unionobject.c
+++ b/Objects/unionobject.c
@@ -237,9 +237,19 @@ dedup_and_flatten_args(PyObject* args)
PyObject* i_element = PyTuple_GET_ITEM(args, i);
for (Py_ssize_t j = i + 1; j < arg_length; j++) {
PyObject* j_element = PyTuple_GET_ITEM(args, j);
- if (i_element == j_element) {
- is_duplicate = 1;
+ int is_ga = Py_TYPE(i_element) == &Py_GenericAliasType &&
+ Py_TYPE(j_element) == &Py_GenericAliasType;
+ // RichCompare to also deduplicate GenericAlias types (slower)
+ is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ)
+ : i_element == j_element;
+ // Should only happen if RichCompare fails
+ if (is_duplicate < 0) {
+ Py_DECREF(args);
+ Py_DECREF(new_args);
+ return NULL;
}
+ if (is_duplicate)
+ break;
}
if (!is_duplicate) {
Py_INCREF(i_element);
@@ -290,8 +300,8 @@ is_unionable(PyObject *obj)
type == &_Py_UnionType);
}
-static PyObject *
-type_or(PyTypeObject* self, PyObject* param)
+PyObject *
+_Py_union_type_or(PyObject* self, PyObject* param)
{
PyObject *tuple = PyTuple_Pack(2, self, param);
if (tuple == NULL) {
@@ -404,7 +414,7 @@ static PyMethodDef union_methods[] = {
{0}};
static PyNumberMethods union_as_number = {
- .nb_or = (binaryfunc)type_or, // Add __or__ function
+ .nb_or = _Py_union_type_or, // Add __or__ function
};
PyTypeObject _Py_UnionType = {