From 4eb41d055e8307b8206f680287e492a6db068acd Mon Sep 17 00:00:00 2001 From: kj <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 9 Nov 2020 12:00:13 +0800 Subject: bpo-42233: Add union type expression support for GenericAlias and fix de-duplicating of GenericAlias (GH-23077) --- Objects/unionobject.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'Objects/unionobject.c') 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 = { -- cgit v1.2.1 From 463c7d3d149283814d879a9bb8411af64e656c8e Mon Sep 17 00:00:00 2001 From: kj <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 14 Dec 2020 02:38:24 +0800 Subject: bpo-42195: Ensure consistency of Callable's __args__ in collections.abc and typing (GH-23060) --- Objects/unionobject.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Objects/unionobject.c') diff --git a/Objects/unionobject.c b/Objects/unionobject.c index 2308bfc9f2..32aa5078af 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -237,8 +237,8 @@ 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); - int is_ga = Py_TYPE(i_element) == &Py_GenericAliasType && - Py_TYPE(j_element) == &Py_GenericAliasType; + int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) && + PyObject_TypeCheck(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; @@ -296,7 +296,7 @@ is_unionable(PyObject *obj) is_new_type(obj) || is_special_form(obj) || PyType_Check(obj) || - type == &Py_GenericAliasType || + PyObject_TypeCheck(obj, &Py_GenericAliasType) || type == &_Py_UnionType); } -- cgit v1.2.1