diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-07-18 11:59:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-18 11:59:25 (GMT) |
commit | 85b58292cf94de74d028053ac33a65f269f305cb (patch) | |
tree | 304937fd8c945ea985e8208da171bb9d097561b7 /Objects | |
parent | 03aad3049d1591c76a219dfe089e5367f88f167e (diff) | |
download | cpython-85b58292cf94de74d028053ac33a65f269f305cb.zip cpython-85b58292cf94de74d028053ac33a65f269f305cb.tar.gz cpython-85b58292cf94de74d028053ac33a65f269f305cb.tar.bz2 |
bpo-44633: Fix parameter substitution of the union type with wrong types. (GH-27218) (GH-27224)
A TypeError is now raised instead of returning NotImplemented.
(cherry picked from commit 3ea5332a4365bdd771286b3e9692495116e9ceef)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unionobject.c | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/Objects/unionobject.c b/Objects/unionobject.c index c744c87..c0c9a24 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -302,10 +302,22 @@ is_unionable(PyObject *obj) PyObject * _Py_union_type_or(PyObject* self, PyObject* other) { + int r = is_unionable(self); + if (r > 0) { + r = is_unionable(other); + } + if (r < 0) { + return NULL; + } + if (!r) { + Py_RETURN_NOTIMPLEMENTED; + } + PyObject *tuple = PyTuple_Pack(2, self, other); if (tuple == NULL) { return NULL; } + PyObject *new_union = make_union(tuple); Py_DECREF(tuple); return new_union; @@ -434,6 +446,21 @@ union_getitem(PyObject *self, PyObject *item) return NULL; } + // Check arguments are unionable. + Py_ssize_t nargs = PyTuple_GET_SIZE(newargs); + for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { + PyObject *arg = PyTuple_GET_ITEM(newargs, iarg); + int is_arg_unionable = is_unionable(arg); + if (is_arg_unionable <= 0) { + Py_DECREF(newargs); + if (is_arg_unionable == 0) { + PyErr_Format(PyExc_TypeError, + "Each union argument must be a type, got %.100R", arg); + } + return NULL; + } + } + PyObject *res = make_union(newargs); Py_DECREF(newargs); @@ -495,21 +522,6 @@ make_union(PyObject *args) { assert(PyTuple_CheckExact(args)); - unionobject* result = NULL; - - // Check arguments are unionable. - 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) { - return NULL; - } - if (!is_arg_unionable) { - Py_RETURN_NOTIMPLEMENTED; - } - } - args = dedup_and_flatten_args(args); if (args == NULL) { return NULL; @@ -521,7 +533,7 @@ make_union(PyObject *args) return result1; } - result = PyObject_GC_New(unionobject, &_PyUnion_Type); + unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type); if (result == NULL) { Py_DECREF(args); return NULL; |