-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
bpo-44676: Add ability to serialise types.Union #27244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add ability to serialise ``types.Union`` objects. Patch provided by Yurii | ||
| Karabas. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -299,6 +299,24 @@ is_unionable(PyObject *obj) | |
| return 0; | ||
| } | ||
|
|
||
| static int | ||
| is_args_unionable(PyObject *args) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: this is extracted from L450 below. |
||
| { | ||
| Py_ssize_t nargs = PyTuple_GET_SIZE(args); | ||
| for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { | ||
| PyObject *arg = PyTuple_GET_ITEM(args, iarg); | ||
| int is_arg_unionable = is_unionable(arg); | ||
| if (is_arg_unionable <= 0) { | ||
| if (is_arg_unionable == 0) { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "Each union argument must be a type, got %.100R", arg); | ||
| } | ||
| return 0; | ||
| } | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
| PyObject * | ||
| _Py_union_type_or(PyObject* self, PyObject* other) | ||
| { | ||
|
|
@@ -418,14 +436,47 @@ union_repr(PyObject *self) | |
| return NULL; | ||
| } | ||
|
|
||
| static PyObject * | ||
| union_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) | ||
| { | ||
| unionobject *alias = (unionobject *)self; | ||
| PyObject* from_args = PyObject_GetAttrString(self, "_from_args"); | ||
| if (from_args == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
| return Py_BuildValue("O(O)", from_args, alias->args); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pablogsal and @uriyyo the reference leak is here, we need to decref
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I just catched it as well: #27332
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @Fidget-Spinner for looking at it!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Fidget-Spinner thanks for solving this issue |
||
| } | ||
|
|
||
| static PyMemberDef union_members[] = { | ||
| {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY}, | ||
| {0} | ||
| }; | ||
|
|
||
| static PyObject * | ||
| union_from_args(PyObject *cls, PyObject *args) | ||
| { | ||
| if (!PyTuple_CheckExact(args)) { | ||
| _PyArg_BadArgument("Union._from_args", "argument 'args'", "tuple", args); | ||
| return NULL; | ||
| } | ||
| if (!PyTuple_GET_SIZE(args)) { | ||
| PyErr_SetString(PyExc_ValueError, "args must be not empty"); | ||
| return NULL; | ||
| } | ||
|
|
||
| if (is_args_unionable(args) <= 0) { | ||
| return NULL; | ||
| } | ||
|
|
||
| return make_union(args); | ||
| } | ||
|
|
||
| static PyMethodDef union_methods[] = { | ||
| {"_from_args", union_from_args, METH_O | METH_CLASS}, | ||
| {"__instancecheck__", union_instancecheck, METH_O}, | ||
| {"__subclasscheck__", union_subclasscheck, METH_O}, | ||
| {"__reduce__", union_reduce, METH_NOARGS}, | ||
| {0}}; | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are good checks 👍🏻
I'd also put a plain
self.assertEqual(alias, loaded). At the moment it's redundant with comparing__args__but this might not be true later, so this will keep future-proof.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I added
self.assertEqual(alias, loaded)line to test.