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
|
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 int
|
2021-07-17 16:44:10 -03:00
|
|
|
is_generic_alias_in_args(PyObject *args)
|
|
|
|
{
|
2020-09-09 17:23:24 -03:00
|
|
|
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
|
|
|
|
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
|
|
|
|
PyObject *arg = PyTuple_GET_ITEM(args, iarg);
|
2021-07-17 16:44:10 -03:00
|
|
|
if (_PyGenericAlias_Check(arg)) {
|
2020-09-09 17:23:24 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
union_instancecheck(PyObject *self, PyObject *instance)
|
|
|
|
{
|
|
|
|
unionobject *alias = (unionobject *) self;
|
|
|
|
Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
|
|
|
|
if (!is_generic_alias_in_args(alias->args)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"isinstance() argument 2 cannot contain a parameterized generic");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
|
|
|
|
PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
|
2021-07-14 01:35:39 -03:00
|
|
|
if (PyType_Check(arg)) {
|
|
|
|
int res = PyObject_IsInstance(instance, arg);
|
|
|
|
if (res < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (res) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
union_subclasscheck(PyObject *self, PyObject *instance)
|
|
|
|
{
|
|
|
|
if (!PyType_Check(instance)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
unionobject *alias = (unionobject *)self;
|
|
|
|
if (!is_generic_alias_in_args(alias->args)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"issubclass() argument 2 cannot contain a parameterized generic");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
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);
|
2021-07-14 01:35:39 -03:00
|
|
|
if (PyType_Check(arg)) {
|
|
|
|
int res = PyObject_IsSubclass(instance, arg);
|
|
|
|
if (res < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (res) {
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
}
|
2021-07-17 16:44:10 -03:00
|
|
|
Py_RETURN_FALSE;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
flatten_args(PyObject* args)
|
|
|
|
{
|
2020-09-23 18:25:54 -03:00
|
|
|
Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
|
|
|
|
Py_ssize_t total_args = 0;
|
2020-09-09 17:23:24 -03:00
|
|
|
// Get number of total args once it's flattened.
|
|
|
|
for (Py_ssize_t i = 0; i < arg_length; i++) {
|
|
|
|
PyObject *arg = PyTuple_GET_ITEM(args, i);
|
2021-07-17 16:44:10 -03:00
|
|
|
if (_PyUnion_Check(arg)) {
|
2020-09-09 17:23:24 -03:00
|
|
|
total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args);
|
|
|
|
} else {
|
|
|
|
total_args++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create new tuple of flattened args.
|
|
|
|
PyObject *flattened_args = PyTuple_New(total_args);
|
|
|
|
if (flattened_args == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_ssize_t pos = 0;
|
|
|
|
for (Py_ssize_t i = 0; i < arg_length; i++) {
|
|
|
|
PyObject *arg = PyTuple_GET_ITEM(args, i);
|
2021-07-17 16:44:10 -03:00
|
|
|
if (_PyUnion_Check(arg)) {
|
2020-09-09 17:23:24 -03:00
|
|
|
PyObject* nested_args = ((unionobject*)arg)->args;
|
2020-09-26 07:48:41 -03:00
|
|
|
Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args);
|
|
|
|
for (Py_ssize_t j = 0; j < nested_arg_length; j++) {
|
2020-09-09 17:23:24 -03:00
|
|
|
PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j);
|
|
|
|
Py_INCREF(nested_arg);
|
|
|
|
PyTuple_SET_ITEM(flattened_args, pos, nested_arg);
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-14 13:54:54 -03:00
|
|
|
if (arg == Py_None) {
|
|
|
|
arg = (PyObject *)&_PyNone_Type;
|
|
|
|
}
|
2020-09-09 17:23:24 -03:00
|
|
|
Py_INCREF(arg);
|
|
|
|
PyTuple_SET_ITEM(flattened_args, pos, arg);
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 16:44:10 -03:00
|
|
|
assert(pos == total_args);
|
2020-09-09 17:23:24 -03:00
|
|
|
return flattened_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
dedup_and_flatten_args(PyObject* args)
|
|
|
|
{
|
|
|
|
args = flatten_args(args);
|
|
|
|
if (args == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
|
|
|
|
PyObject *new_args = PyTuple_New(arg_length);
|
|
|
|
if (new_args == NULL) {
|
2021-07-17 16:44:10 -03:00
|
|
|
Py_DECREF(args);
|
2020-09-09 17:23:24 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Add unique elements to an array.
|
2020-09-26 07:48:41 -03:00
|
|
|
Py_ssize_t added_items = 0;
|
2020-09-09 17:23:24 -03:00
|
|
|
for (Py_ssize_t i = 0; i < arg_length; i++) {
|
|
|
|
int is_duplicate = 0;
|
|
|
|
PyObject* i_element = PyTuple_GET_ITEM(args, i);
|
2021-07-16 10:11:30 -03:00
|
|
|
for (Py_ssize_t j = 0; j < added_items; j++) {
|
|
|
|
PyObject* j_element = PyTuple_GET_ITEM(new_args, j);
|
2021-07-17 16:44:10 -03:00
|
|
|
int is_ga = _PyGenericAlias_Check(i_element) &&
|
|
|
|
_PyGenericAlias_Check(j_element);
|
2020-11-09 00:00:13 -04:00
|
|
|
// 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;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
2020-11-09 00:00:13 -04:00
|
|
|
if (is_duplicate)
|
|
|
|
break;
|
2020-09-09 17:23:24 -03:00
|
|
|
}
|
|
|
|
if (!is_duplicate) {
|
|
|
|
Py_INCREF(i_element);
|
|
|
|
PyTuple_SET_ITEM(new_args, added_items, i_element);
|
|
|
|
added_items++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_DECREF(args);
|
|
|
|
_PyTuple_Resize(&new_args, added_items);
|
|
|
|
return new_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
is_unionable(PyObject *obj)
|
|
|
|
{
|
2021-07-24 10:34:48 -03:00
|
|
|
return (obj == Py_None ||
|
2021-07-17 16:44:10 -03:00
|
|
|
PyType_Check(obj) ||
|
|
|
|
_PyGenericAlias_Check(obj) ||
|
2021-07-24 10:34:48 -03:00
|
|
|
_PyUnion_Check(obj));
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-17 16:44:10 -03:00
|
|
|
PyObject *tuple = PyTuple_Pack(2, self, other);
|
2020-09-09 17:23:24 -03:00
|
|
|
if (tuple == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
_Py_IDENTIFIER(__module__);
|
|
|
|
_Py_IDENTIFIER(__qualname__);
|
|
|
|
_Py_IDENTIFIER(__origin__);
|
|
|
|
_Py_IDENTIFIER(__args__);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:23:42 -03:00
|
|
|
if (_PyObject_LookupAttrId(p, &PyId___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);
|
|
|
|
if (_PyObject_LookupAttrId(p, &PyId___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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (qualname == NULL) {
|
|
|
|
goto use_repr;
|
|
|
|
}
|
|
|
|
if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
|
|
|
|
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}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMethodDef union_methods[] = {
|
|
|
|
{"__instancecheck__", union_instancecheck, METH_O},
|
|
|
|
{"__subclasscheck__", union_subclasscheck, METH_O},
|
|
|
|
{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 {
|
|
|
|
res = PyTuple_GET_ITEM(newargs, 0);
|
|
|
|
Py_INCREF(res);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_INCREF(alias->parameters);
|
|
|
|
return alias->parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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",
|
2020-09-09 17:23:24 -03:00
|
|
|
.tp_doc = "Represent a PEP 604 union type\n"
|
|
|
|
"\n"
|
|
|
|
"E.g. for int | str",
|
|
|
|
.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,
|
|
|
|
.tp_getattro = PyObject_GenericGetAttr,
|
|
|
|
.tp_members = union_members,
|
|
|
|
.tp_methods = union_methods,
|
|
|
|
.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-16 06:49:33 -03:00
|
|
|
args = dedup_and_flatten_args(args);
|
|
|
|
if (args == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyTuple_GET_SIZE(args) == 1) {
|
|
|
|
PyObject *result1 = PyTuple_GET_ITEM(args, 0);
|
|
|
|
Py_INCREF(result1);
|
|
|
|
Py_DECREF(args);
|
|
|
|
return result1;
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-07-16 06:49:33 -03:00
|
|
|
Py_DECREF(args);
|
2020-09-09 17:23:24 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:04:33 -03:00
|
|
|
result->parameters = NULL;
|
2021-07-16 06:49:33 -03:00
|
|
|
result->args = args;
|
2021-07-03 17:00:28 -03:00
|
|
|
_PyObject_GC_TRACK(result);
|
2020-09-09 17:23:24 -03:00
|
|
|
return (PyObject*)result;
|
|
|
|
}
|