summaryrefslogtreecommitdiffstats
path: root/Objects/genericaliasobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-05-05 17:16:06 (GMT)
committerGitHub <noreply@github.com>2022-05-05 17:16:06 (GMT)
commit1ed8d035f1edfaec34016b9f8d615df9e9fe9414 (patch)
tree627f74c12cf8a38ab7c8061449ff309a48b75725 /Objects/genericaliasobject.c
parent354ab7a5c812bf103f7aed5405f02fc81fda5b58 (diff)
downloadcpython-1ed8d035f1edfaec34016b9f8d615df9e9fe9414.zip
cpython-1ed8d035f1edfaec34016b9f8d615df9e9fe9414.tar.gz
cpython-1ed8d035f1edfaec34016b9f8d615df9e9fe9414.tar.bz2
gh-87390: Fix starred tuple equality and pickling (GH-92337)
Diffstat (limited to 'Objects/genericaliasobject.c')
-rw-r--r--Objects/genericaliasobject.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index c6ed161..5eeb1db 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -567,6 +567,9 @@ ga_richcompare(PyObject *a, PyObject *b, int op)
gaobject *aa = (gaobject *)a;
gaobject *bb = (gaobject *)b;
+ if (aa->starred != bb->starred) {
+ Py_RETURN_FALSE;
+ }
int eq = PyObject_RichCompareBool(aa->origin, bb->origin, Py_EQ);
if (eq < 0) {
return NULL;
@@ -604,6 +607,16 @@ static PyObject *
ga_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
{
gaobject *alias = (gaobject *)self;
+ if (alias->starred) {
+ PyObject *tmp = Py_GenericAlias(alias->origin, alias->args);
+ if (tmp != NULL) {
+ Py_SETREF(tmp, PyObject_GetIter(tmp));
+ }
+ if (tmp == NULL) {
+ return NULL;
+ }
+ return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(next)), tmp);
+ }
return Py_BuildValue("O(OO)", Py_TYPE(alias),
alias->origin, alias->args);
}
@@ -775,6 +788,18 @@ ga_iter_clear(PyObject *self) {
return 0;
}
+static PyObject *
+ga_iter_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
+{
+ gaiterobject *gi = (gaiterobject *)self;
+ return Py_BuildValue("N(O)", _PyEval_GetBuiltin(&_Py_ID(iter)), gi->obj);
+}
+
+static PyMethodDef ga_iter_methods[] = {
+ {"__reduce__", ga_iter_reduce, METH_NOARGS},
+ {0}
+};
+
// gh-91632: _Py_GenericAliasIterType is exported to be cleared
// in _PyTypes_FiniTypes.
PyTypeObject _Py_GenericAliasIterType = {
@@ -784,6 +809,7 @@ PyTypeObject _Py_GenericAliasIterType = {
.tp_iter = PyObject_SelfIter,
.tp_iternext = (iternextfunc)ga_iternext,
.tp_traverse = (traverseproc)ga_iter_traverse,
+ .tp_methods = ga_iter_methods,
.tp_dealloc = (destructor)ga_iter_dealloc,
.tp_clear = (inquiry)ga_iter_clear,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,