[3.10] bpo-44635: Convert None to NoneType in the union type constructor (GH-27136). (GH-27142)
(cherry picked from commit b81cac05606c84958b52ada09f690463a3c7e949)
diff --git a/Objects/unionobject.c b/Objects/unionobject.c
index bcba0f6..98db27d 100644
--- a/Objects/unionobject.c
+++ b/Objects/unionobject.c
@@ -64,9 +64,6 @@ union_instancecheck(PyObject *self, PyObject *instance)
}
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
- if (arg == Py_None) {
- arg = (PyObject *)&_PyNone_Type;
- }
if (PyType_Check(arg)) {
int res = PyObject_IsInstance(instance, arg);
if (res < 0) {
@@ -96,9 +93,6 @@ union_subclasscheck(PyObject *self, PyObject *instance)
Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
- if (arg == Py_None) {
- arg = (PyObject *)&_PyNone_Type;
- }
if (PyType_Check(arg)) {
int res = PyObject_IsSubclass(instance, arg);
if (res < 0) {
@@ -172,9 +166,6 @@ union_richcompare(PyObject *a, PyObject *b, int op)
Py_ssize_t b_arg_length = PyTuple_GET_SIZE(b_args);
for (Py_ssize_t i = 0; i < b_arg_length; i++) {
PyObject* arg = PyTuple_GET_ITEM(b_args, i);
- if (arg == (PyObject *)&_PyNone_Type) {
- arg = Py_None;
- }
if (PySet_Add(b_set, arg) == -1) {
Py_DECREF(b_args);
goto exit;
@@ -236,6 +227,9 @@ flatten_args(PyObject* args)
pos++;
}
} else {
+ if (arg == Py_None) {
+ arg = (PyObject *)&_PyNone_Type;
+ }
Py_INCREF(arg);
PyTuple_SET_ITEM(flattened_args, pos, arg);
pos++;
@@ -362,6 +356,10 @@ union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
PyObject *r = NULL;
int err;
+ if (p == (PyObject *)&_PyNone_Type) {
+ return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
+ }
+
if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
goto exit;
}