summaryrefslogtreecommitdiffstats
path: root/Objects/unionobject.c
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-07-27 17:24:10 (GMT)
committerGitHub <noreply@github.com>2024-07-27 17:24:10 (GMT)
commitae192262ad1cffb6ece9d16e67804386c382be0c (patch)
tree301564405a80de59fd6b04e508166a759ff434d0 /Objects/unionobject.c
parentcbac8a3888411587beb026e246889154fbdd49a3 (diff)
downloadcpython-ae192262ad1cffb6ece9d16e67804386c382be0c.zip
cpython-ae192262ad1cffb6ece9d16e67804386c382be0c.tar.gz
cpython-ae192262ad1cffb6ece9d16e67804386c382be0c.tar.bz2
gh-119180: Add evaluate functions for type params and type aliases (#122212)
Diffstat (limited to 'Objects/unionobject.c')
-rw-r--r--Objects/unionobject.c66
1 files changed, 2 insertions, 64 deletions
diff --git a/Objects/unionobject.c b/Objects/unionobject.c
index 7931f43..6e65a65 100644
--- a/Objects/unionobject.c
+++ b/Objects/unionobject.c
@@ -1,11 +1,10 @@
// types.UnionType -- used to represent e.g. Union[int, str], int | str
#include "Python.h"
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
-#include "pycore_typevarobject.h" // _PyTypeAlias_Type
+#include "pycore_typevarobject.h" // _PyTypeAlias_Type, _Py_typing_type_repr
#include "pycore_unionobject.h"
-
static PyObject *make_union(PyObject *);
@@ -181,67 +180,6 @@ _Py_union_type_or(PyObject* self, PyObject* other)
return new_union;
}
-static int
-union_repr_item(PyUnicodeWriter *writer, PyObject *p)
-{
- PyObject *qualname = NULL;
- PyObject *module = NULL;
- int rc;
-
- if (p == (PyObject *)&_PyNone_Type) {
- return PyUnicodeWriter_WriteUTF8(writer, "None", 4);
- }
-
- if ((rc = PyObject_HasAttrWithError(p, &_Py_ID(__origin__))) > 0 &&
- (rc = PyObject_HasAttrWithError(p, &_Py_ID(__args__))) > 0)
- {
- // It looks like a GenericAlias
- goto use_repr;
- }
- if (rc < 0) {
- goto error;
- }
-
- if (PyObject_GetOptionalAttr(p, &_Py_ID(__qualname__), &qualname) < 0) {
- goto error;
- }
- if (qualname == NULL) {
- goto use_repr;
- }
- if (PyObject_GetOptionalAttr(p, &_Py_ID(__module__), &module) < 0) {
- goto error;
- }
- if (module == NULL || module == Py_None) {
- goto use_repr;
- }
-
- // Looks like a class
- if (PyUnicode_Check(module) &&
- _PyUnicode_EqualToASCIIString(module, "builtins"))
- {
- // builtins don't need a module name
- rc = PyUnicodeWriter_WriteStr(writer, qualname);
- goto done;
- }
- else {
- rc = PyUnicodeWriter_Format(writer, "%S.%S", module, qualname);
- goto done;
- }
-
-error:
- rc = -1;
- goto done;
-
-use_repr:
- rc = PyUnicodeWriter_WriteRepr(writer, p);
- goto done;
-
-done:
- Py_XDECREF(qualname);
- Py_XDECREF(module);
- return rc;
-}
-
static PyObject *
union_repr(PyObject *self)
{
@@ -260,7 +198,7 @@ union_repr(PyObject *self)
goto error;
}
PyObject *p = PyTuple_GET_ITEM(alias->args, i);
- if (union_repr_item(writer, p) < 0) {
+ if (_Py_typing_type_repr(writer, p) < 0) {
goto error;
}
}