mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
gh-119180: Add evaluate functions for type params and type aliases (#122212)
This commit is contained in:
parent
cbac8a3888
commit
ae192262ad
11 changed files with 385 additions and 159 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue