2021-07-26 13:00:21 -03:00
|
|
|
// types.UnionType -- used to represent e.g. Union[int, str], int | str
|
2020-09-09 17:23:24 -03:00
|
|
|
#include "Python.h"
|
2021-07-03 09:12:11 -03:00
|
|
|
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
|
2023-05-18 19:56:15 -03:00
|
|
|
#include "pycore_typevarobject.h" // _PyTypeAlias_Type
|
2020-09-09 17:23:24 -03:00
|
|
|
#include "pycore_unionobject.h"
|
|
|
|
#include "structmember.h"
|
|
|
|
|
|
|
|
|
2021-07-17 16:44:10 -03:00
|
|
|
static PyObject *make_union(PyObject *);
|
|
|
|
|
|
|
|
|
2020-09-09 17:23:24 -03:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *args;
|
2021-07-06 15:04:33 -03:00
|
|
|
PyObject *parameters;
|
2020-09-09 17:23:24 -03:00
|
|
|
} unionobject;
|
|
|
|
|
|
|
|
static void
|
|
|
|
unionobject_dealloc(PyObject *self)
|
|
|
|
{
|
|
|
|
unionobject *alias = (unionobject *)self;
|
|
|
|
|
2021-07-03 09:12:11 -03:00
|
|
|
_PyObject_GC_UNTRACK(self);
|
|
|
|
|
2020-09-09 17:23:24 -03:00
|
|
|
Py_XDECREF(alias->args);
|
2021-07-06 15:04:33 -03:00
|
|
|
Py_XDECREF(alias->parameters);
|
2020-10-27 15:55:52 -03:00
|
|
|
Py_TYPE(self)->tp_free(self);
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
|
2021-07-03 09:12:11 -03:00
|
|
|
static int
|
|
|
|
union_traverse(PyObject *self, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
unionobject *alias = (unionobject *)self;
|
|
|
|
Py_VISIT(alias->args);
|
2021-07-06 15:04:33 -03:00
|
|
|
Py_VISIT(alias->parameters);
|
2021-07-03 09:12:11 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-09 17:23:24 -03:00
|
|
|
static Py_hash_t
|
|
|
|
union_hash(PyObject *self)
|
|
|
|
{
|
|
|
|
unionobject *alias = (unionobject *)self;
|
2021-07-16 05:34:56 -03:00
|
|
|
PyObject *args = PyFrozenSet_New(alias->args);
|
|
|
|
if (args == NULL) {
|
|
|
|
return (Py_hash_t)-1;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2021-07-16 05:34:56 -03:00
|
|
|
Py_hash_t hash = PyObject_Hash(args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
return hash;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
union_richcompare(PyObject *a, PyObject *b, int op)
|
|
|
|
{
|
2021-07-17 16:44:10 -03:00
|
|
|
if (!_PyUnion_Check(b) || (op != Py_EQ && op != Py_NE)) {
|
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
|
2021-07-17 16:44:10 -03:00
|
|
|
PyObject *a_set = PySet_New(((unionobject*)a)->args);
|
2020-09-09 17:23:24 -03:00
|
|
|
if (a_set == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-07-17 16:44:10 -03:00
|
|
|
PyObject *b_set = PySet_New(((unionobject*)b)->args);
|
2020-09-09 17:23:24 -03:00
|
|
|
if (b_set == NULL) {
|
2021-07-16 06:49:33 -03:00
|
|
|
Py_DECREF(a_set);
|
2021-07-17 16:44:10 -03:00
|
|
|
return NULL;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2021-07-17 16:44:10 -03:00
|
|
|
PyObject *result = PyObject_RichCompare(a_set, b_set, op);
|
|
|
|
Py_DECREF(b_set);
|
|
|
|
Py_DECREF(a_set);
|
2020-09-09 17:23:24 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-28 07:25:33 -03:00
|
|
|
static int
|
|
|
|
is_same(PyObject *left, PyObject *right)
|
|
|
|
{
|
|
|
|
int is_ga = _PyGenericAlias_Check(left) && _PyGenericAlias_Check(right);
|
|
|
|
return is_ga ? PyObject_RichCompareBool(left, right, Py_EQ) : left == right;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
contains(PyObject **items, Py_ssize_t size, PyObject *obj)
|
2020-09-09 17:23:24 -03:00
|
|
|
{
|
2022-04-28 07:25:33 -03:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
int is_duplicate = is_same(items[i], obj);
|
|
|
|
if (is_duplicate) { // -1 or 1
|
|
|
|
return is_duplicate;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 07:25:33 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
merge(PyObject **items1, Py_ssize_t size1,
|
|
|
|
PyObject **items2, Py_ssize_t size2)
|
|
|
|
{
|
|
|
|
PyObject *tuple = NULL;
|
2020-09-09 17:23:24 -03:00
|
|
|
Py_ssize_t pos = 0;
|
2022-04-28 07:25:33 -03:00
|
|
|
|
|
|
|
for (int i = 0; i < size2; i++) {
|
|
|
|
PyObject *arg = items2[i];
|
|
|
|
int is_duplicate = contains(items1, size1, arg);
|
|
|
|
if (is_duplicate < 0) {
|
|
|
|
Py_XDECREF(tuple);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (is_duplicate) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tuple == NULL) {
|
|
|
|
tuple = PyTuple_New(size1 + size2 - i);
|
|
|
|
if (tuple == NULL) {
|
|
|
|
return NULL;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2022-04-28 07:25:33 -03:00
|
|
|
for (; pos < size1; pos++) {
|
|
|
|
PyObject *a = items1[pos];
|
2022-11-10 18:58:07 -04:00
|
|
|
PyTuple_SET_ITEM(tuple, pos, Py_NewRef(a));
|
2021-07-14 13:54:54 -03:00
|
|
|
}
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2022-11-10 18:58:07 -04:00
|
|
|
PyTuple_SET_ITEM(tuple, pos, Py_NewRef(arg));
|
2022-04-28 07:25:33 -03:00
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tuple) {
|
|
|
|
(void) _PyTuple_Resize(&tuple, pos);
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2022-04-28 07:25:33 -03:00
|
|
|
return tuple;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
|
2022-04-28 07:25:33 -03:00
|
|
|
static PyObject **
|
|
|
|
get_types(PyObject **obj, Py_ssize_t *size)
|
2020-09-09 17:23:24 -03:00
|
|
|
{
|
2022-04-28 07:25:33 -03:00
|
|
|
if (*obj == Py_None) {
|
|
|
|
*obj = (PyObject *)&_PyNone_Type;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2022-04-28 07:25:33 -03:00
|
|
|
if (_PyUnion_Check(*obj)) {
|
|
|
|
PyObject *args = ((unionobject *) *obj)->args;
|
|
|
|
*size = PyTuple_GET_SIZE(args);
|
|
|
|
return &PyTuple_GET_ITEM(args, 0);
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2022-04-28 07:25:33 -03:00
|
|
|
else {
|
|
|
|
*size = 1;
|
|
|
|
return obj;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
is_unionable(PyObject *obj)
|
|
|
|
{
|
2023-05-16 00:36:23 -03:00
|
|
|
if (obj == Py_None ||
|
2021-07-17 16:44:10 -03:00
|
|
|
PyType_Check(obj) ||
|
|
|
|
_PyGenericAlias_Check(obj) ||
|
2023-05-18 19:56:15 -03:00
|
|
|
_PyUnion_Check(obj) ||
|
|
|
|
Py_IS_TYPE(obj, &_PyTypeAlias_Type)) {
|
2023-05-16 00:36:23 -03:00
|
|
|
return 1;
|
|
|
|
}
|
2023-05-18 19:56:15 -03:00
|
|
|
return 0;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
|
2020-11-09 00:00:13 -04:00
|
|
|
PyObject *
|
2021-07-17 16:44:10 -03:00
|
|
|
_Py_union_type_or(PyObject* self, PyObject* other)
|
2020-09-09 17:23:24 -03:00
|
|
|
{
|
2021-07-24 10:34:48 -03:00
|
|
|
if (!is_unionable(self) || !is_unionable(other)) {
|
2021-07-18 06:10:19 -03:00
|
|
|
Py_RETURN_NOTIMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2022-04-28 07:25:33 -03:00
|
|
|
Py_ssize_t size1, size2;
|
|
|
|
PyObject **items1 = get_types(&self, &size1);
|
|
|
|
PyObject **items2 = get_types(&other, &size2);
|
|
|
|
PyObject *tuple = merge(items1, size1, items2, size2);
|
2020-09-09 17:23:24 -03:00
|
|
|
if (tuple == NULL) {
|
2022-04-28 07:25:33 -03:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-10 18:58:07 -04:00
|
|
|
return Py_NewRef(self);
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2021-07-18 06:10:19 -03:00
|
|
|
|
2021-07-17 16:44:10 -03:00
|
|
|
PyObject *new_union = make_union(tuple);
|
2020-09-09 17:23:24 -03:00
|
|
|
Py_DECREF(tuple);
|
|
|
|
return new_union;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
|
|
|
|
{
|
|
|
|
PyObject *qualname = NULL;
|
|
|
|
PyObject *module = NULL;
|
2020-10-10 16:23:42 -03:00
|
|
|
PyObject *tmp;
|
2020-09-09 17:23:24 -03:00
|
|
|
PyObject *r = NULL;
|
|
|
|
int err;
|
|
|
|
|
2021-07-14 13:54:54 -03:00
|
|
|
if (p == (PyObject *)&_PyNone_Type) {
|
|
|
|
return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
if (_PyObject_LookupAttr(p, &_Py_ID(__origin__), &tmp) < 0) {
|
2020-09-09 17:23:24 -03:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:23:42 -03:00
|
|
|
if (tmp) {
|
|
|
|
Py_DECREF(tmp);
|
2022-02-08 16:39:07 -04:00
|
|
|
if (_PyObject_LookupAttr(p, &_Py_ID(__args__), &tmp) < 0) {
|
2020-09-09 17:23:24 -03:00
|
|
|
goto exit;
|
|
|
|
}
|
2020-10-10 16:23:42 -03:00
|
|
|
if (tmp) {
|
2020-09-09 17:23:24 -03:00
|
|
|
// It looks like a GenericAlias
|
2020-10-10 16:23:42 -03:00
|
|
|
Py_DECREF(tmp);
|
2020-09-09 17:23:24 -03:00
|
|
|
goto use_repr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
if (_PyObject_LookupAttr(p, &_Py_ID(__qualname__), &qualname) < 0) {
|
2020-09-09 17:23:24 -03:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (qualname == NULL) {
|
|
|
|
goto use_repr;
|
|
|
|
}
|
2022-02-08 16:39:07 -04:00
|
|
|
if (_PyObject_LookupAttr(p, &_Py_ID(__module__), &module) < 0) {
|
2020-09-09 17:23:24 -03:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
r = PyObject_Str(qualname);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r = PyUnicode_FromFormat("%S.%S", module, qualname);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
use_repr:
|
|
|
|
r = PyObject_Repr(p);
|
|
|
|
exit:
|
|
|
|
Py_XDECREF(qualname);
|
|
|
|
Py_XDECREF(module);
|
|
|
|
if (r == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
err = _PyUnicodeWriter_WriteStr(writer, r);
|
|
|
|
Py_DECREF(r);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
union_repr(PyObject *self)
|
|
|
|
{
|
|
|
|
unionobject *alias = (unionobject *)self;
|
|
|
|
Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
|
|
|
|
|
|
|
|
_PyUnicodeWriter writer;
|
|
|
|
_PyUnicodeWriter_Init(&writer);
|
|
|
|
for (Py_ssize_t i = 0; i < len; i++) {
|
|
|
|
if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyObject *p = PyTuple_GET_ITEM(alias->args, i);
|
|
|
|
if (union_repr_item(&writer, p) < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _PyUnicodeWriter_Finish(&writer);
|
|
|
|
error:
|
|
|
|
_PyUnicodeWriter_Dealloc(&writer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMemberDef union_members[] = {
|
|
|
|
{"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2021-07-06 15:04:33 -03:00
|
|
|
static PyObject *
|
|
|
|
union_getitem(PyObject *self, PyObject *item)
|
|
|
|
{
|
|
|
|
unionobject *alias = (unionobject *)self;
|
|
|
|
// Populate __parameters__ if needed.
|
|
|
|
if (alias->parameters == NULL) {
|
|
|
|
alias->parameters = _Py_make_parameters(alias->args);
|
|
|
|
if (alias->parameters == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
|
|
|
|
if (newargs == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:57:06 -03:00
|
|
|
PyObject *res;
|
2021-07-18 06:10:19 -03:00
|
|
|
Py_ssize_t nargs = PyTuple_GET_SIZE(newargs);
|
2021-07-22 18:57:06 -03:00
|
|
|
if (nargs == 0) {
|
|
|
|
res = make_union(newargs);
|
|
|
|
}
|
|
|
|
else {
|
2022-11-16 13:34:24 -04:00
|
|
|
res = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
|
2021-07-22 18:57:06 -03:00
|
|
|
for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
|
|
|
|
PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
|
|
|
|
Py_SETREF(res, PyNumber_Or(res, arg));
|
|
|
|
if (res == NULL) {
|
|
|
|
break;
|
2021-07-18 06:10:19 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 15:04:33 -03:00
|
|
|
Py_DECREF(newargs);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMappingMethods union_as_mapping = {
|
|
|
|
.mp_subscript = union_getitem,
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
union_parameters(PyObject *self, void *Py_UNUSED(unused))
|
|
|
|
{
|
|
|
|
unionobject *alias = (unionobject *)self;
|
|
|
|
if (alias->parameters == NULL) {
|
|
|
|
alias->parameters = _Py_make_parameters(alias->args);
|
|
|
|
if (alias->parameters == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 18:58:07 -04:00
|
|
|
return Py_NewRef(alias->parameters);
|
2021-07-06 15:04:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef union_properties[] = {
|
2021-07-26 13:00:21 -03:00
|
|
|
{"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.UnionType.", NULL},
|
2021-07-06 15:04:33 -03:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2020-09-09 17:23:24 -03:00
|
|
|
static PyNumberMethods union_as_number = {
|
2020-11-09 00:00:13 -04:00
|
|
|
.nb_or = _Py_union_type_or, // Add __or__ function
|
2020-09-09 17:23:24 -03:00
|
|
|
};
|
|
|
|
|
2021-07-29 16:44:48 -03:00
|
|
|
static const char* const cls_attrs[] = {
|
|
|
|
"__module__", // Required for compatibility with typing module
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
union_getattro(PyObject *self, PyObject *name)
|
|
|
|
{
|
|
|
|
unionobject *alias = (unionobject *)self;
|
|
|
|
if (PyUnicode_Check(name)) {
|
|
|
|
for (const char * const *p = cls_attrs; ; p++) {
|
|
|
|
if (*p == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (_PyUnicode_EqualToASCIIString(name, *p)) {
|
|
|
|
return PyObject_GetAttr((PyObject *) Py_TYPE(alias), name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PyObject_GenericGetAttr(self, name);
|
|
|
|
}
|
|
|
|
|
2022-04-28 12:24:19 -03:00
|
|
|
PyObject *
|
|
|
|
_Py_union_args(PyObject *self)
|
|
|
|
{
|
|
|
|
assert(_PyUnion_Check(self));
|
|
|
|
return ((unionobject *) self)->args;
|
|
|
|
}
|
|
|
|
|
2021-07-17 16:44:10 -03:00
|
|
|
PyTypeObject _PyUnion_Type = {
|
2020-09-09 17:23:24 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2021-07-26 13:00:21 -03:00
|
|
|
.tp_name = "types.UnionType",
|
2022-04-18 00:39:32 -03:00
|
|
|
.tp_doc = PyDoc_STR("Represent a PEP 604 union type\n"
|
2020-09-09 17:23:24 -03:00
|
|
|
"\n"
|
2022-04-18 00:39:32 -03:00
|
|
|
"E.g. for int | str"),
|
2020-09-09 17:23:24 -03:00
|
|
|
.tp_basicsize = sizeof(unionobject),
|
|
|
|
.tp_dealloc = unionobject_dealloc,
|
|
|
|
.tp_alloc = PyType_GenericAlloc,
|
2021-07-03 09:12:11 -03:00
|
|
|
.tp_free = PyObject_GC_Del,
|
|
|
|
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
|
|
|
.tp_traverse = union_traverse,
|
2020-09-09 17:23:24 -03:00
|
|
|
.tp_hash = union_hash,
|
2021-07-29 16:44:48 -03:00
|
|
|
.tp_getattro = union_getattro,
|
2020-09-09 17:23:24 -03:00
|
|
|
.tp_members = union_members,
|
|
|
|
.tp_richcompare = union_richcompare,
|
2021-07-06 15:04:33 -03:00
|
|
|
.tp_as_mapping = &union_as_mapping,
|
2020-09-09 17:23:24 -03:00
|
|
|
.tp_as_number = &union_as_number,
|
|
|
|
.tp_repr = union_repr,
|
2021-07-06 15:04:33 -03:00
|
|
|
.tp_getset = union_properties,
|
2020-09-09 17:23:24 -03:00
|
|
|
};
|
|
|
|
|
2021-07-17 16:44:10 -03:00
|
|
|
static PyObject *
|
|
|
|
make_union(PyObject *args)
|
2020-09-09 17:23:24 -03:00
|
|
|
{
|
|
|
|
assert(PyTuple_CheckExact(args));
|
|
|
|
|
2021-07-18 06:10:19 -03:00
|
|
|
unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type);
|
2020-09-09 17:23:24 -03:00
|
|
|
if (result == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:04:33 -03:00
|
|
|
result->parameters = NULL;
|
2022-11-10 18:58:07 -04:00
|
|
|
result->args = Py_NewRef(args);
|
2021-07-03 17:00:28 -03:00
|
|
|
_PyObject_GC_TRACK(result);
|
2020-09-09 17:23:24 -03:00
|
|
|
return (PyObject*)result;
|
|
|
|
}
|