bpo-41780: Fix __dir__ of types.GenericAlias (GH-22262)

Automerge-Triggered-By: @gvanrossum
This commit is contained in:
Batuhan Taskaya 2020-09-16 00:58:32 +03:00 committed by GitHub
parent ac0333e1e1
commit 2e87774df1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View file

@ -487,11 +487,50 @@ ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
alias->origin, alias->args);
}
static PyObject *
ga_dir(PyObject *self, PyObject *Py_UNUSED(ignored))
{
gaobject *alias = (gaobject *)self;
PyObject *dir = PyObject_Dir(alias->origin);
if (dir == NULL) {
return NULL;
}
PyObject *dir_entry = NULL;
for (const char * const *p = attr_exceptions; ; p++) {
if (*p == NULL) {
break;
}
else {
dir_entry = PyUnicode_FromString(*p);
if (dir_entry == NULL) {
goto error;
}
int contains = PySequence_Contains(dir, dir_entry);
if (contains < 0) {
goto error;
}
if (contains == 0 && PyList_Append(dir, dir_entry) < 0) {
goto error;
}
Py_CLEAR(dir_entry);
}
}
return dir;
error:
Py_DECREF(dir);
Py_XDECREF(dir_entry);
return NULL;
}
static PyMethodDef ga_methods[] = {
{"__mro_entries__", ga_mro_entries, METH_O},
{"__instancecheck__", ga_instancecheck, METH_O},
{"__subclasscheck__", ga_subclasscheck, METH_O},
{"__reduce__", ga_reduce, METH_NOARGS},
{"__dir__", ga_dir, METH_NOARGS},
{0}
};