Skip to content

[3.10] bpo-44490: Partially backport GH-26980's refactoring for easier bugfix backports #27203

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Include/genericaliasobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
extern "C" {
#endif

#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _Py_subs_parameters(PyObject *, PyObject *, PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _Py_make_parameters(PyObject *);
#endif

PyAPI_FUNC(PyObject *) Py_GenericAlias(PyObject *, PyObject *);
PyAPI_DATA(PyTypeObject) Py_GenericAliasType;

Expand Down
50 changes: 31 additions & 19 deletions Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ tuple_add(PyObject *self, Py_ssize_t len, PyObject *item)
return 0;
}

static PyObject *
make_parameters(PyObject *args)
PyObject *
_Py_make_parameters(PyObject *args)
{
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Py_ssize_t len = nargs;
Expand Down Expand Up @@ -294,18 +294,10 @@ subs_tvars(PyObject *obj, PyObject *params, PyObject **argitems)
return obj;
}

static PyObject *
ga_getitem(PyObject *self, PyObject *item)
PyObject *
_Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObject *item)
{
gaobject *alias = (gaobject *)self;
// do a lookup for __parameters__ so it gets populated (if not already)
if (alias->parameters == NULL) {
alias->parameters = make_parameters(alias->args);
if (alias->parameters == NULL) {
return NULL;
}
}
Py_ssize_t nparams = PyTuple_GET_SIZE(alias->parameters);
Py_ssize_t nparams = PyTuple_GET_SIZE(parameters);
if (nparams == 0) {
return PyErr_Format(PyExc_TypeError,
"There are no type variables left in %R",
Expand All @@ -320,32 +312,32 @@ ga_getitem(PyObject *self, PyObject *item)
nitems > nparams ? "many" : "few",
self);
}
/* Replace all type variables (specified by alias->parameters)
/* Replace all type variables (specified by parameters)
with corresponding values specified by argitems.
t = list[T]; t[int] -> newargs = [int]
t = dict[str, T]; t[int] -> newargs = [str, int]
t = dict[T, list[S]]; t[str, int] -> newargs = [str, list[int]]
*/
Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
PyObject *newargs = PyTuple_New(nargs);
if (newargs == NULL) {
return NULL;
}
for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
PyObject *arg = PyTuple_GET_ITEM(args, iarg);
int typevar = is_typevar(arg);
if (typevar < 0) {
Py_DECREF(newargs);
return NULL;
}
if (typevar) {
Py_ssize_t iparam = tuple_index(alias->parameters, nparams, arg);
Py_ssize_t iparam = tuple_index(parameters, nparams, arg);
assert(iparam >= 0);
arg = argitems[iparam];
Py_INCREF(arg);
}
else {
arg = subs_tvars(arg, alias->parameters, argitems);
arg = subs_tvars(arg, parameters, argitems);
if (arg == NULL) {
Py_DECREF(newargs);
return NULL;
Expand All @@ -354,6 +346,26 @@ ga_getitem(PyObject *self, PyObject *item)
PyTuple_SET_ITEM(newargs, iarg, arg);
}

return newargs;
}

static PyObject *
ga_getitem(PyObject *self, PyObject *item)
{
gaobject *alias = (gaobject *)self;
// Populate __parameters__ if needed.
if (alias->parameters == NULL) {
alias->parameters = _Py_make_parameters(alias->args);
if (alias->parameters == NULL) {
return NULL;
}
}

PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
if (newargs == NULL) {
return NULL;
}

PyObject *res = Py_GenericAlias(alias->origin, newargs);

Py_DECREF(newargs);
Expand Down Expand Up @@ -550,7 +562,7 @@ ga_parameters(PyObject *self, void *unused)
{
gaobject *alias = (gaobject *)self;
if (alias->parameters == NULL) {
alias->parameters = make_parameters(alias->args);
alias->parameters = _Py_make_parameters(alias->args);
if (alias->parameters == NULL) {
return NULL;
}
Expand Down