unionobject.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // types.UnionType -- used to represent e.g. Union[int, str], int | str
  2. #include "Python.h"
  3. #include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
  4. #include "pycore_typevarobject.h" // _PyTypeAlias_Type
  5. #include "pycore_unionobject.h"
  6. #include "structmember.h"
  7. static PyObject *make_union(PyObject *);
  8. typedef struct {
  9. PyObject_HEAD
  10. PyObject *args;
  11. PyObject *parameters;
  12. } unionobject;
  13. static void
  14. unionobject_dealloc(PyObject *self)
  15. {
  16. unionobject *alias = (unionobject *)self;
  17. _PyObject_GC_UNTRACK(self);
  18. Py_XDECREF(alias->args);
  19. Py_XDECREF(alias->parameters);
  20. Py_TYPE(self)->tp_free(self);
  21. }
  22. static int
  23. union_traverse(PyObject *self, visitproc visit, void *arg)
  24. {
  25. unionobject *alias = (unionobject *)self;
  26. Py_VISIT(alias->args);
  27. Py_VISIT(alias->parameters);
  28. return 0;
  29. }
  30. static Py_hash_t
  31. union_hash(PyObject *self)
  32. {
  33. unionobject *alias = (unionobject *)self;
  34. PyObject *args = PyFrozenSet_New(alias->args);
  35. if (args == NULL) {
  36. return (Py_hash_t)-1;
  37. }
  38. Py_hash_t hash = PyObject_Hash(args);
  39. Py_DECREF(args);
  40. return hash;
  41. }
  42. static PyObject *
  43. union_richcompare(PyObject *a, PyObject *b, int op)
  44. {
  45. if (!_PyUnion_Check(b) || (op != Py_EQ && op != Py_NE)) {
  46. Py_RETURN_NOTIMPLEMENTED;
  47. }
  48. PyObject *a_set = PySet_New(((unionobject*)a)->args);
  49. if (a_set == NULL) {
  50. return NULL;
  51. }
  52. PyObject *b_set = PySet_New(((unionobject*)b)->args);
  53. if (b_set == NULL) {
  54. Py_DECREF(a_set);
  55. return NULL;
  56. }
  57. PyObject *result = PyObject_RichCompare(a_set, b_set, op);
  58. Py_DECREF(b_set);
  59. Py_DECREF(a_set);
  60. return result;
  61. }
  62. static int
  63. is_same(PyObject *left, PyObject *right)
  64. {
  65. int is_ga = _PyGenericAlias_Check(left) && _PyGenericAlias_Check(right);
  66. return is_ga ? PyObject_RichCompareBool(left, right, Py_EQ) : left == right;
  67. }
  68. static int
  69. contains(PyObject **items, Py_ssize_t size, PyObject *obj)
  70. {
  71. for (int i = 0; i < size; i++) {
  72. int is_duplicate = is_same(items[i], obj);
  73. if (is_duplicate) { // -1 or 1
  74. return is_duplicate;
  75. }
  76. }
  77. return 0;
  78. }
  79. static PyObject *
  80. merge(PyObject **items1, Py_ssize_t size1,
  81. PyObject **items2, Py_ssize_t size2)
  82. {
  83. PyObject *tuple = NULL;
  84. Py_ssize_t pos = 0;
  85. for (int i = 0; i < size2; i++) {
  86. PyObject *arg = items2[i];
  87. int is_duplicate = contains(items1, size1, arg);
  88. if (is_duplicate < 0) {
  89. Py_XDECREF(tuple);
  90. return NULL;
  91. }
  92. if (is_duplicate) {
  93. continue;
  94. }
  95. if (tuple == NULL) {
  96. tuple = PyTuple_New(size1 + size2 - i);
  97. if (tuple == NULL) {
  98. return NULL;
  99. }
  100. for (; pos < size1; pos++) {
  101. PyObject *a = items1[pos];
  102. PyTuple_SET_ITEM(tuple, pos, Py_NewRef(a));
  103. }
  104. }
  105. PyTuple_SET_ITEM(tuple, pos, Py_NewRef(arg));
  106. pos++;
  107. }
  108. if (tuple) {
  109. (void) _PyTuple_Resize(&tuple, pos);
  110. }
  111. return tuple;
  112. }
  113. static PyObject **
  114. get_types(PyObject **obj, Py_ssize_t *size)
  115. {
  116. if (*obj == Py_None) {
  117. *obj = (PyObject *)&_PyNone_Type;
  118. }
  119. if (_PyUnion_Check(*obj)) {
  120. PyObject *args = ((unionobject *) *obj)->args;
  121. *size = PyTuple_GET_SIZE(args);
  122. return &PyTuple_GET_ITEM(args, 0);
  123. }
  124. else {
  125. *size = 1;
  126. return obj;
  127. }
  128. }
  129. static int
  130. is_unionable(PyObject *obj)
  131. {
  132. if (obj == Py_None ||
  133. PyType_Check(obj) ||
  134. _PyGenericAlias_Check(obj) ||
  135. _PyUnion_Check(obj) ||
  136. Py_IS_TYPE(obj, &_PyTypeAlias_Type)) {
  137. return 1;
  138. }
  139. return 0;
  140. }
  141. PyObject *
  142. _Py_union_type_or(PyObject* self, PyObject* other)
  143. {
  144. if (!is_unionable(self) || !is_unionable(other)) {
  145. Py_RETURN_NOTIMPLEMENTED;
  146. }
  147. Py_ssize_t size1, size2;
  148. PyObject **items1 = get_types(&self, &size1);
  149. PyObject **items2 = get_types(&other, &size2);
  150. PyObject *tuple = merge(items1, size1, items2, size2);
  151. if (tuple == NULL) {
  152. if (PyErr_Occurred()) {
  153. return NULL;
  154. }
  155. return Py_NewRef(self);
  156. }
  157. PyObject *new_union = make_union(tuple);
  158. Py_DECREF(tuple);
  159. return new_union;
  160. }
  161. static int
  162. union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
  163. {
  164. PyObject *qualname = NULL;
  165. PyObject *module = NULL;
  166. PyObject *tmp;
  167. PyObject *r = NULL;
  168. int err;
  169. if (p == (PyObject *)&_PyNone_Type) {
  170. return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
  171. }
  172. if (_PyObject_LookupAttr(p, &_Py_ID(__origin__), &tmp) < 0) {
  173. goto exit;
  174. }
  175. if (tmp) {
  176. Py_DECREF(tmp);
  177. if (_PyObject_LookupAttr(p, &_Py_ID(__args__), &tmp) < 0) {
  178. goto exit;
  179. }
  180. if (tmp) {
  181. // It looks like a GenericAlias
  182. Py_DECREF(tmp);
  183. goto use_repr;
  184. }
  185. }
  186. if (_PyObject_LookupAttr(p, &_Py_ID(__qualname__), &qualname) < 0) {
  187. goto exit;
  188. }
  189. if (qualname == NULL) {
  190. goto use_repr;
  191. }
  192. if (_PyObject_LookupAttr(p, &_Py_ID(__module__), &module) < 0) {
  193. goto exit;
  194. }
  195. if (module == NULL || module == Py_None) {
  196. goto use_repr;
  197. }
  198. // Looks like a class
  199. if (PyUnicode_Check(module) &&
  200. _PyUnicode_EqualToASCIIString(module, "builtins"))
  201. {
  202. // builtins don't need a module name
  203. r = PyObject_Str(qualname);
  204. goto exit;
  205. }
  206. else {
  207. r = PyUnicode_FromFormat("%S.%S", module, qualname);
  208. goto exit;
  209. }
  210. use_repr:
  211. r = PyObject_Repr(p);
  212. exit:
  213. Py_XDECREF(qualname);
  214. Py_XDECREF(module);
  215. if (r == NULL) {
  216. return -1;
  217. }
  218. err = _PyUnicodeWriter_WriteStr(writer, r);
  219. Py_DECREF(r);
  220. return err;
  221. }
  222. static PyObject *
  223. union_repr(PyObject *self)
  224. {
  225. unionobject *alias = (unionobject *)self;
  226. Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
  227. _PyUnicodeWriter writer;
  228. _PyUnicodeWriter_Init(&writer);
  229. for (Py_ssize_t i = 0; i < len; i++) {
  230. if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
  231. goto error;
  232. }
  233. PyObject *p = PyTuple_GET_ITEM(alias->args, i);
  234. if (union_repr_item(&writer, p) < 0) {
  235. goto error;
  236. }
  237. }
  238. return _PyUnicodeWriter_Finish(&writer);
  239. error:
  240. _PyUnicodeWriter_Dealloc(&writer);
  241. return NULL;
  242. }
  243. static PyMemberDef union_members[] = {
  244. {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
  245. {0}
  246. };
  247. static PyObject *
  248. union_getitem(PyObject *self, PyObject *item)
  249. {
  250. unionobject *alias = (unionobject *)self;
  251. // Populate __parameters__ if needed.
  252. if (alias->parameters == NULL) {
  253. alias->parameters = _Py_make_parameters(alias->args);
  254. if (alias->parameters == NULL) {
  255. return NULL;
  256. }
  257. }
  258. PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
  259. if (newargs == NULL) {
  260. return NULL;
  261. }
  262. PyObject *res;
  263. Py_ssize_t nargs = PyTuple_GET_SIZE(newargs);
  264. if (nargs == 0) {
  265. res = make_union(newargs);
  266. }
  267. else {
  268. res = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
  269. for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
  270. PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
  271. Py_SETREF(res, PyNumber_Or(res, arg));
  272. if (res == NULL) {
  273. break;
  274. }
  275. }
  276. }
  277. Py_DECREF(newargs);
  278. return res;
  279. }
  280. static PyMappingMethods union_as_mapping = {
  281. .mp_subscript = union_getitem,
  282. };
  283. static PyObject *
  284. union_parameters(PyObject *self, void *Py_UNUSED(unused))
  285. {
  286. unionobject *alias = (unionobject *)self;
  287. if (alias->parameters == NULL) {
  288. alias->parameters = _Py_make_parameters(alias->args);
  289. if (alias->parameters == NULL) {
  290. return NULL;
  291. }
  292. }
  293. return Py_NewRef(alias->parameters);
  294. }
  295. static PyGetSetDef union_properties[] = {
  296. {"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.UnionType.", NULL},
  297. {0}
  298. };
  299. static PyNumberMethods union_as_number = {
  300. .nb_or = _Py_union_type_or, // Add __or__ function
  301. };
  302. static const char* const cls_attrs[] = {
  303. "__module__", // Required for compatibility with typing module
  304. NULL,
  305. };
  306. static PyObject *
  307. union_getattro(PyObject *self, PyObject *name)
  308. {
  309. unionobject *alias = (unionobject *)self;
  310. if (PyUnicode_Check(name)) {
  311. for (const char * const *p = cls_attrs; ; p++) {
  312. if (*p == NULL) {
  313. break;
  314. }
  315. if (_PyUnicode_EqualToASCIIString(name, *p)) {
  316. return PyObject_GetAttr((PyObject *) Py_TYPE(alias), name);
  317. }
  318. }
  319. }
  320. return PyObject_GenericGetAttr(self, name);
  321. }
  322. PyObject *
  323. _Py_union_args(PyObject *self)
  324. {
  325. assert(_PyUnion_Check(self));
  326. return ((unionobject *) self)->args;
  327. }
  328. PyTypeObject _PyUnion_Type = {
  329. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  330. .tp_name = "types.UnionType",
  331. .tp_doc = PyDoc_STR("Represent a PEP 604 union type\n"
  332. "\n"
  333. "E.g. for int | str"),
  334. .tp_basicsize = sizeof(unionobject),
  335. .tp_dealloc = unionobject_dealloc,
  336. .tp_alloc = PyType_GenericAlloc,
  337. .tp_free = PyObject_GC_Del,
  338. .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
  339. .tp_traverse = union_traverse,
  340. .tp_hash = union_hash,
  341. .tp_getattro = union_getattro,
  342. .tp_members = union_members,
  343. .tp_richcompare = union_richcompare,
  344. .tp_as_mapping = &union_as_mapping,
  345. .tp_as_number = &union_as_number,
  346. .tp_repr = union_repr,
  347. .tp_getset = union_properties,
  348. };
  349. static PyObject *
  350. make_union(PyObject *args)
  351. {
  352. assert(PyTuple_CheckExact(args));
  353. unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type);
  354. if (result == NULL) {
  355. return NULL;
  356. }
  357. result->parameters = NULL;
  358. result->args = Py_NewRef(args);
  359. _PyObject_GC_TRACK(result);
  360. return (PyObject*)result;
  361. }