diff options
author | kj <28750310+Fidget-Spinner@users.noreply.github.com> | 2020-12-14 02:38:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-13 10:38:24 -0800 |
commit | 463c7d3d149283814d879a9bb8411af64e656c8e (patch) | |
tree | 204f9b460e5740291fd33d03908131bad317a922 /Objects | |
parent | bpo-30858: Improve error location for expressions with assignments (GH-23753) (diff) | |
download | cpython-463c7d3d149283814d879a9bb8411af64e656c8e.tar.gz cpython-463c7d3d149283814d879a9bb8411af64e656c8e.tar.bz2 cpython-463c7d3d149283814d879a9bb8411af64e656c8e.zip |
bpo-42195: Ensure consistency of Callable's __args__ in collections.abc and typing (GH-23060)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/genericaliasobject.c | 60 | ||||
-rw-r--r-- | Objects/unionobject.c | 6 |
2 files changed, 42 insertions, 24 deletions
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c index 51a12377b7e..756a7ce474a 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -429,8 +429,8 @@ ga_getattro(PyObject *self, PyObject *name) static PyObject * ga_richcompare(PyObject *a, PyObject *b, int op) { - if (!Py_IS_TYPE(a, &Py_GenericAliasType) || - !Py_IS_TYPE(b, &Py_GenericAliasType) || + if (!PyObject_TypeCheck(a, &Py_GenericAliasType) || + !PyObject_TypeCheck(b, &Py_GenericAliasType) || (op != Py_EQ && op != Py_NE)) { Py_RETURN_NOTIMPLEMENTED; @@ -564,6 +564,29 @@ static PyGetSetDef ga_properties[] = { {0} }; +/* A helper function to create GenericAlias' args tuple and set its attributes. + * Returns 1 on success, 0 on failure. + */ +static inline int +setup_ga(gaobject *alias, PyObject *origin, PyObject *args) { + if (!PyTuple_Check(args)) { + args = PyTuple_Pack(1, args); + if (args == NULL) { + return 0; + } + } + else { + Py_INCREF(args); + } + + Py_INCREF(origin); + alias->origin = origin; + alias->args = args; + alias->parameters = NULL; + alias->weakreflist = NULL; + return 1; +} + static PyObject * ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { @@ -575,7 +598,15 @@ ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } PyObject *origin = PyTuple_GET_ITEM(args, 0); PyObject *arguments = PyTuple_GET_ITEM(args, 1); - return Py_GenericAlias(origin, arguments); + gaobject *self = (gaobject *)type->tp_alloc(type, 0); + if (self == NULL) { + return NULL; + } + if (!setup_ga(self, origin, arguments)) { + type->tp_free((PyObject *)self); + return NULL; + } + return (PyObject *)self; } static PyNumberMethods ga_as_number = { @@ -600,7 +631,7 @@ PyTypeObject Py_GenericAliasType = { .tp_hash = ga_hash, .tp_call = ga_call, .tp_getattro = ga_getattro, - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, .tp_traverse = ga_traverse, .tp_richcompare = ga_richcompare, .tp_weaklistoffset = offsetof(gaobject, weakreflist), @@ -615,27 +646,14 @@ PyTypeObject Py_GenericAliasType = { PyObject * Py_GenericAlias(PyObject *origin, PyObject *args) { - if (!PyTuple_Check(args)) { - args = PyTuple_Pack(1, args); - if (args == NULL) { - return NULL; - } - } - else { - Py_INCREF(args); - } - gaobject *alias = PyObject_GC_New(gaobject, &Py_GenericAliasType); if (alias == NULL) { - Py_DECREF(args); return NULL; } - - Py_INCREF(origin); - alias->origin = origin; - alias->args = args; - alias->parameters = NULL; - alias->weakreflist = NULL; + if (!setup_ga(alias, origin, args)) { + PyObject_GC_Del((PyObject *)alias); + return NULL; + } _PyObject_GC_TRACK(alias); return (PyObject *)alias; } diff --git a/Objects/unionobject.c b/Objects/unionobject.c index 2308bfc9f2a..32aa5078afc 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -237,8 +237,8 @@ dedup_and_flatten_args(PyObject* args) PyObject* i_element = PyTuple_GET_ITEM(args, i); for (Py_ssize_t j = i + 1; j < arg_length; j++) { PyObject* j_element = PyTuple_GET_ITEM(args, j); - int is_ga = Py_TYPE(i_element) == &Py_GenericAliasType && - Py_TYPE(j_element) == &Py_GenericAliasType; + int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) && + PyObject_TypeCheck(j_element, &Py_GenericAliasType); // RichCompare to also deduplicate GenericAlias types (slower) is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ) : i_element == j_element; @@ -296,7 +296,7 @@ is_unionable(PyObject *obj) is_new_type(obj) || is_special_form(obj) || PyType_Check(obj) || - type == &Py_GenericAliasType || + PyObject_TypeCheck(obj, &Py_GenericAliasType) || type == &_Py_UnionType); } |