mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-44553 : Implement GC methods for types.Union (GH-26993)
(cherry picked from commit 1097384ce9
)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
This commit is contained in:
parent
7a2d2ed133
commit
0856134542
3 changed files with 38 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
|||
// types.Union -- used to represent e.g. Union[int, str], int | str
|
||||
#include "Python.h"
|
||||
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
|
||||
#include "pycore_unionobject.h"
|
||||
#include "structmember.h"
|
||||
|
||||
|
@ -14,10 +15,20 @@ unionobject_dealloc(PyObject *self)
|
|||
{
|
||||
unionobject *alias = (unionobject *)self;
|
||||
|
||||
_PyObject_GC_UNTRACK(self);
|
||||
|
||||
Py_XDECREF(alias->args);
|
||||
Py_TYPE(self)->tp_free(self);
|
||||
}
|
||||
|
||||
static int
|
||||
union_traverse(PyObject *self, visitproc visit, void *arg)
|
||||
{
|
||||
unionobject *alias = (unionobject *)self;
|
||||
Py_VISIT(alias->args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static Py_hash_t
|
||||
union_hash(PyObject *self)
|
||||
{
|
||||
|
@ -437,8 +448,9 @@ PyTypeObject _Py_UnionType = {
|
|||
.tp_basicsize = sizeof(unionobject),
|
||||
.tp_dealloc = unionobject_dealloc,
|
||||
.tp_alloc = PyType_GenericAlloc,
|
||||
.tp_free = PyObject_Del,
|
||||
.tp_flags = Py_TPFLAGS_DEFAULT,
|
||||
.tp_free = PyObject_GC_Del,
|
||||
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
|
||||
.tp_traverse = union_traverse,
|
||||
.tp_hash = union_hash,
|
||||
.tp_getattro = PyObject_GenericGetAttr,
|
||||
.tp_members = union_members,
|
||||
|
@ -472,15 +484,16 @@ _Py_Union(PyObject *args)
|
|||
}
|
||||
}
|
||||
|
||||
result = PyObject_New(unionobject, &_Py_UnionType);
|
||||
result = PyObject_GC_New(unionobject, &_Py_UnionType);
|
||||
if (result == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
result->args = dedup_and_flatten_args(args);
|
||||
if (result->args == NULL) {
|
||||
Py_DECREF(result);
|
||||
PyObject_GC_Del(result);
|
||||
return NULL;
|
||||
}
|
||||
_PyObject_GC_TRACK(result);
|
||||
return (PyObject*)result;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue